TABLE OF CONTENTS

Automatically Add JIRA Tickets To Your Commits

Author Sagi Liba
Sagi Liba on Nov 4, 2020
2 min 🕐

It’s time to stop manually writing your JIRA tickets inside each commit message. To do so we will briefly go over Git hooks, and the following bash script I wrote to add the JIRA ticket automatically.

Git hooks

Git hooks are scripts that run automatically when particular events occur in your Git repository, they allow you to hook into Git’s internal behavior.

Your Git hooks can be found inside the .git/hooks folder at your project’s directory. If you cannot see the .git directory you can manually enter it by writing the full path:

jira-tickets-git-hooks

You can also enable viewing hidden directories and files for your computer by changing your file explorer options:

Open Windows Search -> File Explorer Options – > View tab -> Hidden files and folders -> Show hidden files, folders and drives.

Windows 10:

hidden-directories

When you enter a new project’s Git hooks directory all the hooks will not be enabled, instead you will see a .sample file extension for each hook that prevents them from running.

sample-hooks

To activate the hook you have to remove the .sample extension, if you are creating a new script or removing the extension did not work, you need to make sure that the hook is executable by running:

PREPARE-COMMIT-MESSAGE IS THE HOOK NAME

Copy
chmod +x prepare-commit-msg

Adding The JIRA Ticket Automatically

Now that we’ve covered the basics of hooks we can look at the following script I wrote, which takes the JIRA ticket from your branch name and adds it directly to your commit message.

You should put the following code inside prepare-commit-msg Git hook. Replace the entire hook’s content with this script.

Copy
#!/bin/bash
branches_list() {
git branch
}
checked_out_branch() {
sed -e '/^[^*]/d'
}
get_jira_ticket() {
grep -e 'TICKET_NAME\+-[0-9]\+' -o
}
get_ticket_from_branch_name() {
branches_list | checked_out_branch | get_jira_ticket
}
MESSAGE="$(cat $1)"
TICKET=`get_ticket_from_branch_name`
if [ "$TICKET" ]
then
echo "[$TICKET] $MESSAGE" > $1
fi

I won’t get into explaining the above bash script because the code is pretty straight forward.

The only part that you should change is the ticket name and match it to your organization’s ticket name.

Copy
...
get_jira_ticket() {
grep -e 'TICKET_NAME+-[0-9]+' -o // Change TICKET_NAME
}
...

The final part is to remove the .sample extension from prepare-commit-msg Git hook and test it out by writing a new commit.

The script will extract the ticket name and number in the following format: TICKET_NAME-1234

It will take it from the branch name and add it to your commit message automatically.

The underscore of TICKET_NAME is not mandatory, you can write any name you want instead.

If the script does not find the appropriate format of a name separated by a dash and followed by your ticket number, it will write your commit message without any changes.

Make sure only one ticket number is found in the branch name, more than one will break the above behavior.

*  *  *

I hope you enjoyed this article 🙂

© 2020-present Sagi Liba. All Rights Reserved