Skip to content

Instantly share code, notes, and snippets.

@henrik242
Last active March 30, 2016 07:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save henrik242/958389b6c52e3fa50bf9 to your computer and use it in GitHub Desktop.
Save henrik242/958389b6c52e3fa50bf9 to your computer and use it in GitHub Desktop.
Git hook to automatically create JIRA issues if not present in commit message
#!/usr/bin/env bash
#
# How to install:
#
# cp commit-msg your-project/.git/hooks/
# chmod 755 your-project/.git/hooks/commit-msg
#
# Then update the USER, PASS, PROJECT and JIRAURL variables in your-project/.git/hooks/commit-msg
#
# How to delete mis-created issues:
# curl -u username:password -X DELETE https://jira.example.com/rest/api/2/issue/JIRAKEY-9999999
#
# Ref. https://gist.github.com/henrik242/958389b6c52e3fa50bf9
#
USER=myJiraUserName
PASS=myJiraPassword
PROJECT=JIRAKEY
JIRAURL=https://jira.example.com
LABELS='"some-label","some-other-label"'
COMMIT=$1
if grep -q ${PROJECT}- $COMMIT; then
echo
echo "Commit message already has issue number. Good!"
echo
exit 0
fi
RESULT=/tmp/commit-msg-result-$(date +%s)
TITLE=$(perl -0777 -pe 's/\n.*$//mg; s/"/\\"/g' $COMMIT)
SUMMARY=$(perl -0777 -pe 's/\n/\\n/g; s/\\n#.*$//; s/"/\\"/g' $COMMIT)
curl -s -u "$USER:$PASS" --data "{
\"fields\": {
\"project\":{\"key\":\"$PROJECT\"},
\"summary\":\"$TITLE\",
\"description\":\"$SUMMARY\",
\"issuetype\":{\"name\":\"Task\"},
\"labels\": [$LABELS]
}
}" -H "Content-Type: application/json" $JIRAURL/rest/api/2/issue/ > $RESULT
if ! grep -q ${PROJECT}- $RESULT; then
echo "Aborting: Could not create issue number!" 1>&2
cat $RESULT 1>&2
rm $RESULT
exit 1
fi
NEWCOMMIT=/tmp/commit-msg-new-$(date +%s)
ISSUE=$(cut -d\" -f8 $RESULT)
echo -n "$ISSUE " > $NEWCOMMIT
cat $COMMIT >> $NEWCOMMIT
mv $NEWCOMMIT $COMMIT
rm $RESULT
echo
echo "Created issue $JIRAURL/browse/$ISSUE"
echo
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment