DEV Community

Cover image for Automating Jira Ticket Numbers in Commit Messages with Git
Ibrahima Sow
Ibrahima Sow

Posted on

Automating Jira Ticket Numbers in Commit Messages with Git

This solution assumes you're using jira as your project management tool, Git as your version control system, and includes a bash script to accomplish this.

When working on software development projects that use Jira for project management and Git for version control, it's essential to maintain a clear association between code changes and their respective jira tickets. One way to streamline this process is to automatically include the jira ticket number in commit messages when the branch name contains the ticket number. In this post, we'll explore how to achieve this by using a Git pre-commit hook and a bash script.

Before we proceed, ensure you have the following prerequisites in place:

  1. Git: A distributed version control system.
  2. Jira: A project management tool.
  3. Bash: A command-line shell.

Through the post, we assume that our jira project initials are "TY". You can modify this to match your specific project initials.
To get started, create a new bash script and name it pre-commit. Here's an example of a bash script that accomplishes the desired functionality:
we first retrieve the commit message that user inputted.

#!/bin/bash

COMMIT_MSG_FILE=$1
COMMIT_MESSAGE=$(cat $COMMIT_MSG_FILE)
Enter fullscreen mode Exit fullscreen mode

then we get ticket number from the branch name.

JIRA_TICKET_NUMBER=$(git rev-parse --abbrev-ref HEAD | grep -Eo '((TY|ty)-[0-9]{4})' | tr "[:lower:]" "[:upper:]")
Enter fullscreen mode Exit fullscreen mode

and finally we check if user didn't already insert ticket number in commit message,if yes we don't need to continue and if not we update the commit message with the ticket number.

if [[ $JIRA_TICKET_NUMBER == "[]" || "$COMMIT_MESSAGE" == "$JIRA_TICKET_NUMBER"* ]]; then
  exit 0;
fi

echo "$JIRA_TICKET_NUMBER $COMMIT_MESSAGE" > $COMMIT_MSG_FILE
Enter fullscreen mode Exit fullscreen mode

To set up the pre-commit hook, follow these steps:

  1. Open a terminal or command prompt.
  2. Navigate to the root directory of your Git repository.
  3. Create a new directory named .git/hooks if it doesn't already exist.
  4. Move the pre-commit script to the .git/hooks directory.
  5. Make the script executable by running chmod +x .git/hooks/pre-commit

Once the pre-commit hook is set up, it will be triggered before each commit. It checks if a jira ticket number is present in the branch name and if the commit message already contains the ticket number. If either condition is true, the script exits without making any changes to the commit message. Otherwise, it prepends the jira ticket number between brackets and a space to the commit message.

For example, if your branch name is TY-1234-fix-bug or fix/ty-1234 or something else with ticket number and the original commit message is Update README file, the resulting commit message will be [TY-1234] Update README file.

final code:

#!/bin/bash

COMMIT_MSG_FILE=$1
COMMIT_MESSAGE=$(cat $COMMIT_MSG_FILE)
JIRA_TICKET_NUMBER=$(git rev-parse --abbrev-ref HEAD | grep -Eo '((TY|ty)-[0-9]{4})' | tr "[:lower:]" "[:upper:]")

if [[ $JIRA_TICKET_NUMBER == "[]" || "$COMMIT_MESSAGE" == "$JIRA_TICKET_NUMBER"* ]]; then
  exit 0;
fi

echo "$JIRA_TICKET_NUMBER $COMMIT_MESSAGE" > $COMMIT_MSG_FILE
Enter fullscreen mode Exit fullscreen mode

By automatically including the jira ticket number in commit messages, you enhance traceability and make it easier to associate code changes with their corresponding jira tickets.

That's it! You have now set up a Git pre-commit hook that automatically adds jira ticket numbers to commit messages when the branch name contains the ticket number. This helps maintain consistency and improves the organization of your project.

Enjoy it!

Top comments (0)