Jira issue number in git commit message Jira issue number in git commit message git git

Jira issue number in git commit message


I used git-jira-hook and modified it to my needs, which should also work for you. For your needs, just remove the parts where it logs into Jira to check if the jira issue number regexed from the commit message is valid. If you don't like python (git-jira-hook is written in python) and prefer bash, you should be able to adapt the example scripts in each repo's .git/hooks dir to your needs.

As to implementing something that will work for everyone, you want to use git-jira-hook as the 'update' hook on your upstream repos. This will block pushes that contain commit messages that lack proper jira issue references. Since it is more convenient to get feedback about missing issue references at commit time (rather than at push time), you'll need to get your developers to install git-jira-hook as their commit-msg hook. I'll explain later how this can be done globally.

Here is how I have solved this issue:

  1. Private repo commit-msg hook:I modified git-jira-hook to check for jira issue references in the notation that we use. Then, I emailed out the hook with instructions to everyone explaining how to install the hook globally, as is explained in this SO question. If you install the hook globally then it will be used in all future clones, and can be easily applied to already cloned repos using git init.

  2. Upstream repo update hook:I used the already modified git-jira-hook script and installed it in each of our repos. I couldn't get the interactive authentication bits working on the upstream repo (symlinked it), so I instead created a restricted permissions Jira user and hard coded their authentication into the script.


If you are using npm you can use https://github.com/typicode/husky with https://github.com/marionebl/commitlint

Create file: commitlint.config.js

module.exports = {rules: {    'references-empty': [2, 'never']},parserPreset: {    parserOpts: {        issuePrefixes: ['REF-']    }}};

and add config for the hook in package.json

commit-msg: commitlint -E HUSKY_GIT_PARAMS


You can have server-side hooks as well, pre-receive-hook or something, however this is not obvious if you're used to github.

Failing that, I might consider providing an 'install-hooks' build option (as a rake task, make task, or whatever), although that would make me feel a bit 'dirty' because now my build is tied to the version control system...