3

How can I ensure commit comments from a github PR squash merge contains required text?

We use Jira + Github and if a use enters Jira issue IDs in commit comments those a commits become linked to the Jira issues.

When users merge a PR from their private feature branch, they usually squash and merge which means the user can manually summarize the intent of the single commit from that branch to a larger integration branch or master.

If a user forgets to enter them when they merge the PR, it becomes painful to edit the commit and add the text to the message.

Is there a status check or commit message formatter that I can use on the PR to ensure there must be something that looks like a Jira ID? (AAA-111)

Peter Kahn
  • 12,364
  • 20
  • 77
  • 135
  • I can't recall if it should be a commit hook or a CI job. Either way: Help your users out by adding a checklist to a [pull request template](https://help.github.com/en/articles/creating-a-pull-request-template-for-your-repository). When you do figure it out, create the appropriate amount [of strictness](https://help.github.com/en/articles/types-of-required-status-checks) to the check. – mbb Mar 05 '19 at 15:50
  • Hmm, I do have a template and I didn't realize I could add requirements for final squash merge commit message in it. – Peter Kahn Mar 05 '19 at 20:49
  • It's worth noting Jira/GitHub came out with an app that does some additional metadata management as of Oct '18. Check it out: https://github.com/marketplace/jira-software-github – mbb Mar 06 '19 at 14:55
  • Very interesting. I think we might already have that tool. However, if still doesn't help me with this want to block clicking the squash merge button if the text doesn't contain some expected value – Peter Kahn Mar 07 '19 at 14:21

2 Answers2

5

The other approach* around this is to ensure that commit messages include the branch name, which in JIRA are most likely derived from the issue title.

To achieve this, coworkers have to include in their .git/hooks directory a file named commit-msg with the following contents :

#!/bin/bash
current_branch="$(git rev-parse --abbrev-ref HEAD)"
tmp=$(mktemp) || exit
echo "$current_branch $(cat "$1")" > "$tmp"
mv "$tmp" "$1"

Then when someone is committing on the feature branch ABC-1234-customers-cant-log-in, a commit command like this :

git commit -m "Awesome changes"

...will actually produce the following commit message :

ABC-1234-customers-cant-log-in Awesome changes

...and JIRA will then automatically detect it and link the commit to the issue, thus ensuring a reliable recap of commits on the JIRA issue page.

We've put this policy in place in my team and it has several advantages :

  • no more forgetting JIRA issue numbers in commit messages
  • gain of time
  • slightly easier to parse commit messages when you need it

(As a sidenote, just in case, this automated beahviour can also be disactivated on demand, with git commit -n)

* (note that these two solutions are not mutually exclusive. In fact, you could very well want both, Adil's for remote repo integrity, and this one for local use efficiency)

Romain Valeri
  • 19,645
  • 3
  • 36
  • 61
  • 1
    A fine solution for the non squash in github pr case. Sorry, I forgot to mention that aspect which is key to this problem. – Peter Kahn Mar 05 '19 at 20:52
2

If you're using GitHub Enterprise, you can set up a pre-receive hook to reject commits that don't have a JIRA ticket number in them.

Check out the example here:

#!/bin/bash
#
# check commit messages for JIRA issue numbers formatted as [JIRA-<issue number>]

REGEX="\[JIRA\-[0-9]*\]"

ERROR_MSG="[POLICY] The commit doesn't reference a JIRA issue"

while read OLDREV NEWREV REFNAME ; do
  for COMMIT in `git rev-list $OLDREV..$NEWREV`;
  do
    MESSAGE=`git cat-file commit $COMMIT | sed '1,/^$/d'`
    if ! echo $MESSAGE | grep -iqE "$REGEX"; then
      echo "$ERROR_MSG: $MESSAGE" >&2
      exit 1
    fi
  done
done
exit 0
Adil B
  • 14,635
  • 11
  • 60
  • 78
  • A fine solution for the non squash in github pr case. Sorry, I forgot to mention that aspect which is key to this problem. – Peter Kahn Mar 05 '19 at 20:52
  • i am trying to enforce in my local git repository the same jira pre-receive commit hook by saving a file called commit-msg.sh with the exact same script under my repo folder/.git/hooks followed by git init. Then making a change in any source code files. Now when i commit it still doesn't invalidate it without a jira key. So looks like its doing nothing in my local. How to enforce this in my local? – Ashley Mar 08 '19 at 16:16