Skip to content

Instantly share code, notes, and snippets.

@tiagotele
Last active November 22, 2025 14:13
Show Gist options
  • Select an option

  • Save tiagotele/cdf01d81252788e203e8415140a882d2 to your computer and use it in GitHub Desktop.

Select an option

Save tiagotele/cdf01d81252788e203e8415140a882d2 to your computer and use it in GitHub Desktop.
Jira Ticket Extraction For branches like /ABCD-1234/<BRANCH_NAME> It'll prepopulate commit msg like this: ABCD-1234
#!/bin/bash
COMMIT_MSG_FILE=$1
COMMIT_SOURCE=$2
COMMIT_SHA=$3
# Get the current branch name
branchName=$(git rev-parse --abbrev-ref HEAD)
# Use a regex to extract the Jira issue ID (e.g., PROJ-123, including alphanumeric project keys)
# The pattern matches "PROJ-123" within common branch naming conventions
jiraId=$(echo "$branchName" | sed -nr 's/.*\/([A-Z0-9]+-[0-9]+)\/.*/\1/p')
if [ "$COMMIT_SOURCE" = "commit" ]; then
# If the commit is an amend, do not modify the commit message unless the author wants to
exit 0
else
# For new commits, proceed to modify the commit message
# If a Jira ID was found, prepend it to the commit message file ($1)
if [[ ! -z "$jiraId" ]]; then
# $1 is the name of the file containing the commit message
echo "$jiraId | latest changes. $(cat "$1") " > "$1"
else
echo "WARNING:NO JIRA ID FOUND IN BRANCH NAME. IT CAN BLOCK YOUR PULL REQUESTS! $(cat "$1") " > "$1"
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment