3

I know there are few plugins in Bitbucket like YACC , etc that you can directly install in Bitbucket and that becomes available for the repositories to enable jira pre-receive commit hook. Is there any similar thing for Github. All i am looking at is what is the best and most feasible solution to enable some sort of a hook in Github that enforces that any commit made needs to have a valid jira issue key. It would be even better if there is any that also does the same for branch creation but not something important for me at the moment.

Please help with the steps on how to enable the same in Github enterprise.

Ashley
  • 1,447
  • 3
  • 26
  • 52
  • Maybe not directly answering your specific need, but [this quite similar question](https://stackoverflow.com/questions/55006426/ensure-github-pr-squash-merge-commit-comments-contain-issue-id) has been posted recently. In case it might be useful. – Romain Valeri Mar 08 '19 at 10:54
  • Thanks! I tried this on my local first by creating a commit-msg.sh file that has this script and saved it under by git repo folder/.git/hooks. But commits work locally as they used to without validating any jira regex. Is there a way i can enforce that? – – Ashley Mar 08 '19 at 16:37
  • This is indeed a local solution, and in my case the only "enforcement" has been communicating and organizing inside our team. As a sidenote, the hook is not expected with a `.sh` extension, just `commit-msg`. – Romain Valeri Mar 08 '19 at 16:41
  • Thanks for getting back. I tried even without that .sh extension but it doesn't work. Please help with the correct steps on how to enforce a hook for any commit in local system. – Ashley Mar 08 '19 at 17:27
  • Hmm, strange, I don't remember any additional steps, after having written this file. Have you double-checked the 1) path? (`.git/hooks`) 2) file name? (`commit-msg`) 3) contents? Sorry to ask, but those common mistakes are so frequent, and happen to everyone... out of that, I can't yet figure what the problem could be. – Romain Valeri Mar 08 '19 at 17:52
  • Appreciate the help here. But yes this is all i did. Created a project repo , initialized with git, placed a file commit-msg with your script. Then tried testing it by making a change in a project file followed by normal commit message. it didn't error out. Is there anything i'm missing in the whole process where i just need to test this hook in my local git repository. – Ashley Mar 08 '19 at 19:08
  • I can't really say past this point, sorry, I guess we'll have to try to figure out by a bit of testing and reading on the subject... what can be the problem? – Romain Valeri Mar 08 '19 at 19:15
  • So as per you, if i use that script exactly as it is, then any commit in my local for that git repo should do a check for that hook. do you think is it because we need to enforce policy somehow.i 'm not familiar with that at all, hence the ask. It looks to me at the moment that git hooks are not getting enforced at all, as if git is totally ignoring that. is it something i need to also set in my local repo .gitconfig or may be global .gitconfig – Ashley Mar 08 '19 at 19:25
  • I'm trying to remind what element I could have left behind as a given, but for now, no, I don't see anything else since this is all local. Frustrating :-/ – Romain Valeri Mar 08 '19 at 19:27
  • Ok by now i've tried almost everythinh including reinstalling latest git on my windows machine. Making sure the git bin is on path. Also tried replacing the shebang with actual bash or sh path. Still no results. Not sure how i can test this simple hook in my windows local.any further suggestions? – Ashley Mar 09 '19 at 01:38
  • I guess it's time for a separate question. I hope it'll get answered quickly. – Romain Valeri Mar 09 '19 at 01:54
  • Ok one question here. How does that regex expression validate that its a valid jira issue key. i mean we haven't defined any jira url etc. – Ashley Mar 09 '19 at 02:01

4 Answers4

3

This feature is available using a centralized solution for server-side git-hooks like Datree.io. It has built-in policies like:

  1. Detect and prevent merging of secrets.
  2. Enforce Jira ticket integration - mention ticket number in pull request name / commit message / branch name
  3. Link commit message to a Jira ticket
  4. Link pull request title to a Jira ticket

Disclaimer: I am one of Datrees founders

Shimon Tolts
  • 1,602
  • 14
  • 15
1

This is not possible with Github.

Only Github Enterprise has support for pre-receive hooks and even has an example for doing JIRA issue enforcement in commit messages - https://github.com/github/platform-samples/blob/master/pre-receive-hooks/require-jira-issue.sh

Alternatively, you can have your team members setup local pre-push or pre-commit hooks that will do the enforcement.

manojlds
  • 290,304
  • 63
  • 469
  • 417
  • Thanks! I tried this on my local first by creating a commit-msg.sh file that has this script and saved it under by git repo folder/.git/hooks. But commits work locally as they used to without validating any jira regex. Is there a way i can enforce that? – Ashley Mar 08 '19 at 14:31
  • @Ashley Make sure your `pre-commit` file is executable. – Ashwanth Kumar May 11 '19 at 09:13
  • I see, commit-msg under .git/hook, https://gist.github.com/veewee/939539fef7953bd11cdb. Can this be configure somehow globally so its available for each one's .git folder ? – dsi Feb 16 '21 at 16:44
0

If you have CI set up to do checks on a branch, then I've done a branch pipeline as below as a way to achieve this:

       - npm ci
       - npm run lint
       - npm test
       # == Check that all commits since the base branch (dev) contain ACME- for ticket reference ==
       # Fetch the merge base for our project
       - git fetch origin dev:dev
       - >
         echo "-- Checking ACME- prefix on commits --"
         && [[ `git log --oneline dev.. | grep -v 'ACME-' | wc -l` -eq 0 ]]
         && echo "Passed"
         || (echo "Failed for the following commits:"
         && git log --oneline dev.. | grep -v 'ACME-'
         && [[ 0 -eq 1 ]])

Naturally you can avoid the || && gymnastics if you pull this out into a shell script file.

NealeU
  • 1,264
  • 11
  • 23
0

If you mean GitHub Enterprise you can verify commit details against Jira information with similar tools you already mentioned on the Jira side.

In case of the hosted GitHub version, your choice would be the local hooks, as it was already mentioned. (The most effective by the way is using both server-side and local hooks) If unsure about how to construct and install your local hook script this tool has a "wizard" that guides you through some steps and spits out the script at the end.

Levente
  • 1
  • 1