用于验证提交消息的 Gerrit 服务器挂钩

Gerrit server hook to validate the commit message

我正在使用 Gerrit-2.16.17 作为代码审查工具,并且需要配置服务器端挂钩以在 Gerrit 收到提交以进行代码审查时验证提交消息 (refs/for/branch)

我们需要遵循以下custom commit message。如果假设以下内容不存在于我们的提交消息中。在创建审查请求之前,需要自动拒绝 Gerrit。

Project Name: 
Product Version: 
Issue Number:
Testing Status:
Developer Name:
Location:  

根据这个 Document 来验证传入的提交,在服务器端 $GERRIT_SITE/hooks/commit-received 需要创建自定义挂钩。是否有任何前脚本可用于验证我们的提交消息?需要帮助 configure/enforce 服务器端的这个钩子。

这里有一个 commit-received 钩子的例子:

#!/usr/bin/env bash

echo $@

echo Project: 
echo Refname: 
echo Uploader: 
echo Uploader-username: 

if [[  = "test" ]]
then
  exit 0
else
  exit -1
fi

以下是成功推送到 test 存储库的示例:

➜  test git:(master) git push origin HEAD:refs/for/master
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Writing objects: 100% (3/3), 290 bytes | 290.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
remote: Processing changes: refs: 1, updated: 1, done
remote: commit b2f0e7d: --project test --refname refs/heads/master --uploader Administrator <admin@example.com> --uploader-username admin --oldrev 9d4768e749448cbf10c9e4a1297f4ac206ea9e84 --newrev b2f0e7d920f35719cfa4a9bb5c5d2d54d6dad768 --cmdref refs/for/master
remote: Project: test
remote: Refname: refs/heads/master
remote: Uploader: Administrator <admin@example.com>
remote: Uploader-username: admin
remote:
remote: SUCCESS
remote:
remote:   http://localhost:8080/c/test/+/41 Testina
remote:
To ssh://localhost:29418/test
 * [new reference]   HEAD -> refs/for/master

shouldfail 存储库推送失败:

➜  shouldfail git:(master) git push origin HEAD:refs/for/master
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Writing objects: 100% (3/3), 274 bytes | 274.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
remote: Processing changes: refs: 1, done
To ssh://localhost:29418/shouldfail
 ! [remote rejected] HEAD -> refs/for/master (commit 9addd6c: --project shouldfail --refname refs/heads/master --uploader Administrator <admin@example.com> --uploader-username admin --oldrev 6fcf316d435687e9138f4d63e228bdeafd40daae --newrev 9addd6c796fce09d187d90251b32b152530e8a4b --cmdref refs/for/master
Project: shouldfail
Refname: refs/heads/master
Uploader: Administrator <admin@example.com>
Uploader-username: admin)
error: failed to push some refs to 'ssh://localhost:29418/shouldfail'

我在 bash 中编写了示例,但您可以使用任何脚本语言。

请注意,您将在挂钩服务器端打印的所有内容都将显示在客户端。