Skip to content

Instantly share code, notes, and snippets.

@alandotcom
Created August 26, 2013 15:20
Show Gist options
  • Select an option

  • Save alandotcom/6342617 to your computer and use it in GitHub Desktop.

Select an option

Save alandotcom/6342617 to your computer and use it in GitHub Desktop.
A commit-msg shell function to insert hyper-links to JIRA issues directly into the commits that are related. We use Gerrit for code-review, so this also takes into account that a changeID may exist in the commit message. Adding the link is simply a matter of putting the JIRA issue tag in the commit message header surrounded by brackets. Multiple…
#!bin/bash
MSG="$1"
# Add JIRA hyperlinks to the end of the commmit message
# Using ggrep (gnu grep) and
add_JiraUrl() {
# Get all the JIRA numbers from the commit heading e.g., [AIP-2024][APPS-2000]
JIRA=$(cat $MSG | head -n 1 | ggrep -Po '(?<=\[)[A-Z]*[\-]\d*(?=\])')
# Customize URL to your JIRA
URL="https://climate.jira.com/browse"
# We use Gerrit so find the changeId if it exists
changeIdLine=$(grep -n 'Change-Id' $MSG | cut -d : -f 1)
# Append the URL(s) to the commit msg
for issue in $(echo $JIRA)
do
issueUrl="$URL/$issue"
# Skip URLs that have been added
if grep $issueUrl $MSG >/dev/null
then
continue
fi
# Append before Change-Id if it exists
if [[ $changeIdLine ]]
then
# append URL before change ID
let lineBefore=changeIdLine-2
gsed "$lineBefore a $issueUrl" $MSG > $msg.tmp
mv $msg.tmp $MSG
let changeIdLine+=1
continue
fi
# Append URL to the end otherwise
echo $issueUrl >> $MSG
done
}
add_JiraUrl
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment