#!/bin/sh
#
# Validate branch name and append the ticket ID (from branch name) to commit messages.
# Modified version of: https://medium.com/@adrian.garcia.estaun/add-the-ticket-id-to-your-commit-messages-automatically-2debfa0fbe9d
#
MISSING_ISSUE_NUMBER_MESSAGE="Skip auto issue referencing. Branch name does not start with a ticket ID. Format: ticketID-branch-description. E.g.: '1234-update-tests'"
# Gets the commit message received as parameter and the current branch name.
COMMIT_MSG_FILE="$1"
COMMIT_SOURCE="$2"
# Skip merge commits
if [ "$COMMIT_SOURCE" = merge ]; then
exit 0
fi
message=$(cat "$COMMIT_MSG_FILE")
branchName=$(git symbolic-ref --short HEAD)
# Get the last part of the branch name. feature/foo/42-bugfix -> 42-bugfix
normalizedBranchName=$(echo "$branchName" | awk -F'/' '{print $NF}')
# Branch name starts with a ticket ID
if echo "$normalizedBranchName" | grep -qE '^[0-9]+[-_]'; then
# Get ticketID from branch name. Works with `-` or `_` as separator. 123-foobar -> 123
ticketID=$(echo "$normalizedBranchName" | sed -nE 's,([0-9]+)[_-].+,\1,p')
if [ -z "$ticketID" ]; then
echo "$MISSING_ISSUE_NUMBER_MESSAGE"
exit 0
fi
# Message already contains the ticket ID
if echo "$message" | grep -qE '#[0-9]+'; then
exit 0
fi
output="$message"$'\n'$'\n'"Refs: #$ticketID"
echo "$output" > "$1"
else
echo "$MISSING_ISSUE_NUMBER_MESSAGE"
exit 0
fi