automatically append JIRA issue ID into commit message

xion_admin July 7, 2017

I wonder whether it is possibel to automatically append the JIRA issue ID to every commit message automatically?

All our branch names include the issue id with the issue type name.

Is there a way to automatically append it to a commit message when commiting?

We are using script runner, maybe there is a way to do it with it?

 

4 answers

2 votes
Aron Gombas _Midori_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 7, 2017

By the time the commit is created with the original commit message, it gets its hash. If you changed the commit message afterward (even programatically), that'd result in a new hash which could eventually break things.

So, I'd suggest you'd rather require committers to enter the issue key in the commit message (a'priori).

xion_admin July 7, 2017

But this kind of thing get forgotten, it has to be done automatically.

Maybe via a commit pre hook? Any idea how to do this?

Aron Gombas _Midori_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 7, 2017

What version control system are you using?

xion_admin July 11, 2017

Bitbucket with git

Aron Gombas _Midori_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 11, 2017

Now that I know the environment, I added a new answer.

Adrien Horgnies March 13, 2018

You can use the git prepare-commit-msg hook. It's a script that get executed before you write the commit message. I've written such a script. It works for branches named such as "feature/ABC-123-description" and will add "ABC-123" to the commit message. And the developer has all the liability to modify the commit message afterwards. You can set up such a hook at repository level or globally on a machine. That means you must set up the script on every developer machine though. 

#!/bin/bash

# get current branch
branchName=`git rev-parse --abbrev-ref HEAD`

# search jira issue id in a pattern such a "feature/ABC-123-description"
jiraId=$(echo $branchName | sed -nr 's,[a-z]+/([A-Z]+-[0-9]+)-.+,\1,p')

# only prepare commit message if pattern matched and jiraId was found
if [[ ! -z $jiraId ]]; then
# $1 is the name of the file containing the commit message
sed -i.bak -e "1s/^/\n\n$jiraId\n/" $1
fi

Like # people like this
rubens21 August 22, 2020

Thank you so much, Adrien!

 

Just a small improvement.

Since the project code may have numbers, I used `[A-Z0-9]` instead of `[A-Z]` to match the project code.

 

jiraId=$(echo $branchName | sed -nr 's,[a-z]+/([A-Z0-9]+-[0-9]+)-.+,\1,p')
Lefthandmedia January 25, 2022

I'm getting an error in source tree when using this pre-commit hook.

the file SED tries to open IS present and the comment is there.

git -c diff.mnemonicprefix=false -c core.quotepath=false --no-optional-locks commit -q -F C:\Users\Ralph\AppData\Local\Temp\ppjj2kg3.xax
sed: no input files

 on Windows10  , GIT2.30.2 Sourcetree 3.4.7

Brian Khuu November 16, 2022

Thanks Adrien! Aside from forgetting to switch from #!/bin/sh to #!/bin/bash I was able to get your hook going. I've also added a bit of a change to also extract the jira ticket title as well, which may be of interest to some people here as well.

I also adopted rubens21's suggestion as well.

#!/bin/bash
COMMIT_MSG_FILE=$1
COMMIT_SOURCE=$2
SHA1=$3

## Automatically append jira issue ticket to every commit
## Ref: https://community.atlassian.com/t5/Bitbucket-questions/automatically-append-JIRA-issue-ID-into-commit-message/qaq-p/605991

# get current branch
branchName=`git rev-parse --abbrev-ref HEAD`

# search jira issue id and title in a pattern such a "feature/ABC-123-description"
jiraId=$(echo $branchName| sed -nr 's,[a-z]+/([A-Z0-9]+-[0-9]+)-.+,\1,p')
jiraTitle=$(echo $branchName| sed -nr 's,[a-z]+/[A-Z0-9]+-[0-9]+-(.+),\1,p' | sed 's/-/ /g')

# only prepare commit message if pattern matched and jiraId was found
if [[ ! -z $jiraId ]]; then
# $1 is the name of the file containing the commit message
sed -i.bak -e "1s/^/\n\n\[$jiraId\] \: $jiraTitle\n/"$1
fi
1 vote
Milind Shakya January 9, 2019

I created a precommit hook that automatically prepends jira ticket id to your commits, if your branch has the ticket info. Works fine so far.

 

https://github.com/milin/giticket

0 votes
Phill Pafford October 11, 2023

updated to work on OSX and Linux and only add the Jira Ticket, not the Jira Title

Gist here: https://gist.github.com/phillpafford/02437d61a90a8d5f8cd520944a7b618c

#!/usr/bin/env bash

## Automatically append jira issue ticket to every commit
## Ref: https://community.atlassian.com/t5/Bitbucket-questions/automatically-append-JIRA-issue-ID-into-commit-message/qaq-p/605991

set -e

# get current branch
BRANCH_NAME=$(git rev-parse --abbrev-ref HEAD 2>/dev/null)

# search jira issue id in a pattern such a "feature/ABC-123-description"
JIRA_ISSUE=$(echo $BRANCH_NAME| sed -nr 's,[a-z]+/([A-Z0-9]+-[0-9]+)-.+,\1,p')

# only prepare commit message if pattern matched and JIRA_ISSUE was found
if [[ ! -z $JIRA_ISSUE ]]; then
# $1 is the name of the file containing the commit message
## OSX and Linux compatable
sed -i '' -e "1s/$/\n\n\[$JIRA_ISSUE\]/" $1
fi

 

0 votes
Aron Gombas _Midori_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 11, 2017

Commit Policy is an add-on for Bitbucket that enforces this via a custom hook.

See the full documentation here.

xion_admin July 11, 2017

does it enforce that the user to insert the issue ID in the commit message?

Or does it automatically append the issue ID into the commit?

The later is prefered.

Aron Gombas _Midori_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 11, 2017
xion_admin July 25, 2017

I finally used following script:

http://monksealsoftware.com/add-jira-issue-number-to-git-commit-message/

to solve my problem

Max August 24, 2018

Any chance to re-upload it? it's 404 :( 

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events