LoginSignup
2
0

More than 3 years have passed since last update.

GitLabでMerge時に自動でIssueをCloseされないようにするためのgitHook

Last updated at Posted at 2019-11-14

なぜ?

不具合チケットの管理にgitlabのissueを使ったら、closeしてないのに勝手にcloseしてしまう問題が頻発しました。
調べると、去年(2018年)のアップデートで追加された機能が原因らしいです。
参考
https://qiita.com/kyogom/items/710dbcdc3bc9cdbd0dfc

closefix のような文字と合わせてissueNoをコミットメッセージに入れると、Merge時に自動でcloseされてしまう模様。。

どうすればいい?

https://docs.gitlab.com/ee/user/project/issues/managing_issues.html#closing-issues-automatically
公式Docによると、サーバ内の gitlab.rbgitlab.yml のファイルを修正すればいいとありますが、
そうすると全projectに影響が出てしまいそうなので、コミット時にメッセージに制限を加えることにしました。

gitHook/commit-msg を使って、コミット時にメッセージをチェックします。
下記fileをrepositoryをcloneした.git\hooks\に置きます。

commit-msg
#!/bin/sh
############################
#自動でissueがクローズされてしまう用語を含む場合、コミットできないようにする
#This is a script that prevents you from committing if it contains a term that automatically closes the issue
# see https://docs.gitlab.com/ee/administration/issue_closing_pattern.html
############################
#issue closing pattern
format="(Close|Closes|Closed|Closing|close|closes|closed|closing|Fix|Fixes|Fixed|Fixing|fix|fixes|fixed|fixing|Resolve|Resolves|Resolved|Resolving|resolve|resolves|resolved|resolving|Implement|Implements|Implemented|Implementing|implement|implements|implemented|implementing) ?#[0-9]"

if grep -E "${format}" $1 >/dev/null; then
  echo "========================="
  echo "以下の単語はコミットメッセージに含むことができません(Merge時に自動でissueがcloseされるからです)"
  echo "The following words cannot be included in a commit message: Please remove it. (Because the issue is automatically closed when merging)"
  echo "Close, Closes, Closed, Closing, close, closes, closed, closing"
  echo "Fix, Fixes, Fixed, Fixing, fix, fixes, fixed, fixing"
  echo "Resolve, Resolves, Resolved, Resolving, resolve, resolves, resolved, resolving"
  echo "Implement, Implements, Implemented, Implementing, implement, implements, implemented, implementing"
  echo "========================="
  exit 1
else
  echo "[githook] commit format ok"
  exit 0
fi

注意点

このgitHookScriptは完全にCloseを防止するものではありません。
たとえばissueNoの書き方を下記のようにすると、上記のgitHookScriptをすり抜けてしまいます。
group/project#123
https://gitlab.example.com/group/project/issues/123

また、公式Docに自動Closeされるメッセージの正規表現が公開されているのですが(下記)、正規表現の書き方がだいぶ異なりますので、カバーしきれていない書き方があるかもしれません。

((?:[Cc]los(?:e[sd]?|ing)|[Ff]ix(?:e[sd]|ing)?|[Rr]esolv(?:e[sd]?|ing)|[Ii]mplement(?:s|ed|ing)?)(:?) +(?:(?:issues? +)?%{issue_ref}(?:(?:, *| +and +)?)|([A-Z][A-Z0-9_]+-\d+))+)

そのため、利用する場合は、あくまで簡易的なチェック機能としてご利用ください。

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0