Jira issue number in git commit message

39,520

Solution 1

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.

Solution 2

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

Solution 3

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...

Share:
39,520
meijuh
Author by

meijuh

Updated on July 08, 2020

Comments

  • meijuh
    meijuh almost 4 years

    At our company we are moving from svn to git. For issue tracking we use JIRA from Atlassian.

    Now we want to enforce that every commit message contains an issue number (just like we did with svn).

    We have found the commit-msg hook which we use to reject a commit if it does not contain an issue number.

    JIRA uses Fisheye to scan the git repo. If a commit message contains an issue number then the changes are shown under that issue.

    The problem is that a hook is not copied when a git repository is cloned. So issue numbers in the commit messages are not enforced. That means that when a new commit is pushed upstream Jira may not list the changes under an issue.

    The question is; are we using Git somehow in the wrong way and is there any way to really enforce an issue number in the commit message? Or does any one have simply have a script/hook (other than the commit-msg hook) that accomplishes this?