#! /bin/bash

# Find comments related to the ticket potentially indicated by the branch name. The script never
# fails, it only warns the caller there are comments that are potentially worth checking.

# Testing notes:
# Different users have different structures in their development trees. When making changes to this
# script, please test a few different scenarios including: When there are multiple populated
# build directories, and when the Git repository is checkout out with the Jira ticket in the name
# of the repo, and the current branch name matches the Jira ticket as well.

# This script is not supposed to work outside of main WT repo.

. `dirname -- ${BASH_SOURCE[0]}`/common_functions.sh
cd_dist

# Retrieve the current branch name.
branch_name=$(git rev-parse --abbrev-ref HEAD 2>&1 || echo "BRANCH_NOT_FOUND")

# We expect the following syntax (case insensitive): wt-<digits>[-<alphanum>].
wt_ticket_regex="(wt|WT|wT|Wt)-[0-9]+(-[a-zA-Z0-9-]+)?"
if [[ ! $branch_name =~ ^$wt_ticket_regex ]]; then
    exit 0
fi

# Get what could be the ticket id.
ticket_id=$(echo "$branch_name" | cut -d "-" -f-2)

# Check for comments related to the ticket. git grep searches only tracked files, so build
# directories and temporary files are excluded automatically. Note: newly created files that are
# not yet added to git will be missed, but they can be checked once they are tracked.
if git -C .. grep -Iin "$ticket_id" > /dev/null 2>&1; then
    echo "There are comments mentioning $ticket_id in the code, please check if they need to be resolved:"
    git -C .. grep -Iin "$ticket_id"
fi

exit 0
