git commit 消息中的 Jira 问题编号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11365554/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Jira issue number in git commit message
提问by meijuh
At our company we are moving from svn to git. For issue tracking we use JIRA from Atlassian.
在我们公司,我们正在从 svn 迁移到 git。对于问题跟踪,我们使用 Atlassian 的 JIRA。
Now we want to enforce that every commit message contains an issue number (just like we did with svn).
现在我们要强制每个提交消息都包含一个问题编号(就像我们对 svn 所做的那样)。
We have found the commit-msg hook which we use to reject a commit if it does not contain an issue number.
我们找到了 commit-msg 钩子,如果它不包含问题编号,我们可以使用它来拒绝提交。
JIRA uses Fisheye to scan the git repo. If a commit message contains an issue number then the changes are shown under that issue.
JIRA 使用 Fisheye 扫描 git repo。如果提交消息包含问题编号,则更改将显示在该问题下。
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.
问题是在克隆 git 存储库时不会复制钩子。所以提交消息中的问题编号没有被强制执行。这意味着当新提交被推送到上游时,Jira 可能不会列出问题下的更改。
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?
问题是; 我们是否以某种方式以错误的方式使用 Git,有什么方法可以真正在提交消息中强制执行问题编号?或者是否有人只是有一个脚本/钩子(除了 commit-msg 钩子)来实现这一点?
回答by Freerobots
I used git-jira-hookand 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.
我使用了git-jira-hook并根据我的需要对其进行了修改,这也适用于您。根据您的需要,只需删除它登录 Jira 的部分,以检查提交消息中正则表达式的 jira 问题编号是否有效。如果你不喜欢 python(git-jira-hook 是用 python 编写的)并且更喜欢 bash,你应该能够根据你的需要调整每个 repo 的 .git/hooks 目录中的示例脚本。
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.
至于实现对每个人都适用的东西,您希望使用 git-jira-hook 作为上游存储库上的“更新”挂钩。这将阻止包含缺少正确 jira 问题引用的提交消息的推送。由于在提交时(而不是在推送时)获取有关丢失问题引用的反馈更方便,因此您需要让您的开发人员安装 git-jira-hook 作为他们的 commit-msg 钩子。稍后我将解释如何在全局范围内完成此操作。
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.
Private repo commit-msg hook:我修改了 git-jira-hook 以检查我们使用的表示法中的 jira 问题引用。然后,我通过电子邮件向每个人发送了钩子,并说明了如何全局安装钩子,如这个 SO 问题中所述。如果您全局安装钩子,那么它将在所有未来的克隆中使用,并且可以使用 git init 轻松应用于已经克隆的存储库。
上游 repo 更新钩子:我使用了已经修改过的 git-jira-hook 脚本并将其安装在我们的每个 repo 中。我无法在上游存储库上使用交互式身份验证位(符号链接它),所以我创建了一个受限权限的 Jira 用户并将他们的身份验证硬编码到脚本中。
回答by user2350849
If you are using npm you can use https://github.com/typicode/huskywith https://github.com/marionebl/commitlint
如果您使用的是 npm,您可以使用https://github.com/typicode/husky和https://github.com/marionebl/commitlint
Create file: commitlint.config.js
创建文件:commitlint.config.js
module.exports = {
rules: {
'references-empty': [2, 'never']
},
parserPreset: {
parserOpts: {
issuePrefixes: ['REF-']
}
}};
and add config for the hook in package.json
并在 package.json 中为钩子添加配置
commit-msg: commitlint -E HUSKY_GIT_PARAMS
commit-msg: commitlint -E HUSKY_GIT_PARAMS
回答by Arafangion
You can have server-side hooks as well, pre-receive-hook or something, however this is not obvious if you're used to github.
你也可以有服务器端钩子,预接收钩子或其他东西,但是如果你习惯了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...
如果做不到这一点,我可能会考虑提供一个“安装挂钩”构建选项(作为 rake 任务、make 任务或其他任何东西),尽管这会让我觉得有点“脏”,因为现在我的构建与版本控制系统相关联...
回答by Ferenc Kiss
There is an add-on for that: Commit Policy Plugin for JIRA!
有一个附加组件:为 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.
它不仅会检查 JIRA 问题键是否“正式”包含在消息中,还会检查相应的问题是否与 JQL 查询匹配。使用它,您有多种可能性,允许仅检查特定问题类型、特定状态中的问题、当前 Scrum 冲刺中的问题、针对下一个版本的问题等等。
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.
作为奖励,它适用于您的原始版本( Subversion) 和目标版本控制系统 ( Git),即使在过渡期间也能控制您的工作。
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.
您可以将钩子脚本安装到受祝福的仓库和任何分支。不幸的是,使用 Git 克隆存储库时不会克隆钩子脚本,但我们目前正在为此研究解决方法。
Full docs: http://www.midori-global.com/products/jira-commit-policy-plugin/documentation/
完整文档: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.
免责声明:这是 JIRA 的商业和受支持的附加组件,我是一名开发人员。
回答by Sumedh Tambe
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.
如果您在 .git 文件夹中使用默认挂钩,那么您在其中所做的更改将不会被索引,这仅意味着它们无法被检出或克隆。
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.
您可以将提交消息挂钩移动到名为“hooks”的不同文件夹中并提交,以便覆盖 .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)
如果提交不包含问题编号,我们将显示一个消息框作为错误,以便用户在不需要问题跟踪器编号的情况下仍然可以继续(在补丁/修补程序的情况下有效)
回答by Samar Panda
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.
克隆存储库时不会复制 Git 挂钩。我们建议使用husky。这有助于向克隆 repo 的每个人发布 git hook。
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.
强制 git commit 包含 jira id 引用。我已经在 repo- husky-jira-demo 中配置了它。希望这可以满足您的要求。