Ad

How to add Jira ticket id in commit messages - Jira Bitbucket

How to add Jira ticket id in commit messages - Jira Bitbucket

|
109

Jira Software is an Atlassian tool for managing issues, tasks and so on that can be related to their other product BitBucket. It is now very common among developers to make their commit message informative to their branch and Jira ticket ID. Recently I was asked the same in my company so that one can figure out exactly for which task/issue the commits were made, and in what branch. (Later one is not that important.) Well, in this article, I will show you how you can do it without writing commit-id and it auto-appends before the commit message.

Git hooks and the prepare-commit-msg

git hooks prepare-commit-msg
Photo by Debby Hudson on Unsplash

Git hooks are the custom scripts that run based on some git operation.  I don't want to go deep down into this and just discuss the relevant one.

There's a hook called prepare-commit-msg, it runs when you commit your codes with some message, and its output is saved as your commit message. So we will just use this hook to append our Jira ticket ID, just before it gets saved as your commit message, easy right? So let's do it.

First You have to find out the source of the Jira ticket ID, from where our logic can take it and insert it in the prefix. What I and a lot of devs use is from their branch. When you create your branch from Jira software, it automatically names it using the ticket title and number. Below are the steps to get the hook to append the Jira ID from the branch name into your commit.

Steps to Update prepare-commit-msg

  1. First, go to .git/hooks directory. It would be in your project directory and hidden (as it starts with a ".")
    cd .git/hooks
  2. Now create the file if not exist prepare-commit-msg 
  3. Now open the file in any editor and paste the following script, which basically extracts the JIRA ID from your branch name and appends it to your commit message.
    #!/bin/sh
    
    # Extract the name of the current branch
    BRANCH=$(git symbolic-ref --short HEAD)
    
    # Extract the JIRA ticket number from the branch name
    JIRA=$(echo $BRANCH | grep -oE '[A-Z]+-[0-9]+')
    
    # Prepend the JIRA ticket to the commit message, if jira ticket is found
    if [ ! -z "$JIRA_TICKET" ]; then
        COMMIT_MSG=$(cat $1)
        echo "$JIRA_TICKET $COMMIT_MSG" > $1
    fi
  4. At last, make the file executable 
    chmod +x prepare-commit-msg
  5. There's no step 5, all done!!

Just start committing codes and get appended Jira ID in each of your commit.

FAQ

Q: What if my branch name doesn’t follow the Jira ticket naming convention?
A: This script assumes your branch names include the Jira ticket ID as you would've created it via the Jira itself. If your branches are named differently, you’ll just need to modify the regular expression I used in the script or extract the Jira ID from another source, such as a commit message template or a custom input and you're good to go.

Q: Can this hook be used with merge commits?
A: The prepare-commit-msg hook doesn’t run for merge commits by default. You’d need additional scripting if you want to include the Jira ID in merge commits as well, if you want to know, the comment section is all open 😉.

Q: How do I remove the Jira ticket ID from a commit message if needed?
A: You can manually edit the commit message in your editor after the hook runs, before finalizing the commit. Just remove the Jira ticket ID from the message if it’s not required.

Q: Can I implement this across multiple repositories?
A: Yes, you can use a Git template directory to include this hook for all new repositories or distribute the script to existing repositories using a tool like Ansible or a simple shell script. But it will not be passed to your fellow developer's system, each dev has to setup the same in their system.

Q: Is there a way to make the Jira ID optional in commit messages?
A: You can modify the script to prompt the user for confirmation before appending the Jira ID, giving you the flexibility to include or omit it based on the commit's context.

 

Conclusion

By configuring the prepare-commit-msg Git hook, you can save time and avoid errors by automatically adding Jira ticket IDs to your commit messages. This practice not only streamlines your workflow but also makes it easier to track changes across your project, ensuring that every commit is linked to a specific Jira issue and it'll be easier to blame the other developers 🤓

Ad


Comments

© 2024 Garbage Valuegarbage value logo