Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jira issue number in git commit message

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?

like image 645
meijuh Avatar asked Jul 06 '12 15:07

meijuh


People also ask

How do I add a Jira ticket number to a git commit?

To manually associate a git commit to a Jira issue, access the Change commit issues feature from the following locations: Project page > Git Commits > click View Full Commit. Issue page > Git Commits tab > click View Full Commit. Git menu > View all repositories > select a repository with git commits.

How do you reference an issue in a commit message?

You just need to include #xxx in your commit message to reference an issue without closing it. With the new GitHub issues 2.0, you can use these synonyms to reference an issue and close it (in your commit message).

How do I reference a commit in Jira?

Open a Jira issue then go to the Git Commits tab. In this tab, you will see commits, files changed, links to external repository, commit author and more. git commit message to link the commit to this issue. The git commit will get associated with the Jira issue if the commit message includes the exact issue ID.


3 Answers

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.

like image 168
Freerobots Avatar answered Oct 19 '22 21:10

Freerobots


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
like image 34
user2350849 Avatar answered Oct 19 '22 23:10

user2350849


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

like image 1
Arafangion Avatar answered Oct 19 '22 22:10

Arafangion