问题
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?
回答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:
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.
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.
回答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
回答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...
回答4:
There is an add-on for that: Commit Policy Plugin for JIRA!
It not only checks if the JIRA issue key is "formally" included in the message, but also checks if the corresponding issue(s) matches a JQL query. Using this, you have a multitude of possibilities, to allow checking only against certain issue types, issues in a certain statuses, issues in the current Scrum sprint, issues targeting the next version and so on.
As a bonus, it works both with your original (Subversion) and target version control system (Git), making your work controlled even during the transition period.
You can install the hook script to the blessed repo, and to any forks. Unfortunately, hook scripts are not cloned when cloning a repo with Git, but we are currently investigating workarounds for this.
Full docs: http://www.midori-global.com/products/jira-commit-policy-plugin/documentation/
Disclaimer: this is a commercial and supported add-on for JIRA, and I'm a developer working on it.
回答5:
If you are using default hooks in .git folder then the changes you make in there wouldn't be indexed that simply means they can't be checked out or cloned.
you can move your commit-message hook in a different folder named as 'hooks' and commit it so that it would overwrite the default hooks from .git.
We show a message box as an error if commit doesn't contain an issue number so that user can still go ahead if it doesn't need to have an issue tracker number (works in cases of patch / hotfix)
回答6:
Git hook is not copied when a repository is cloned. We recommend using husky. This can help in publishing git hooks to everyone who clones the repo.
To mandate git commit to include a jira id reference. I have configured that in a repo - husky-jira-demo. Hope this can address your requirement.
来源:https://stackoverflow.com/questions/11365554/jira-issue-number-in-git-commit-message