1、安装 gerrit 的hooks
插件
2、安装完成后,点击Plugins
—Installed
,确认插件安装成功,点击hooks
插件名称,跳转至http://<Gerrit地址>/plugins/hooks/Documentation/config.md
有关于插件配置的介绍。


3、hooks
的目录放置,默认安装 gerrit 的时候是没有hooks
目录的,需要手动创建一个文件夹,命名为hooks
,建议放在 gerrit 安装目录下。
4、本文目标实现在提交之前对commit msg
进行校验,如果其中包含的jira issue
已经关闭则不允许进行提交。在hooks
目录下创建commit-received
。
#!/bin/sh
# 获取最近一次提交的信息
LAST_COMMIT=$(git log -1 --pretty=format:"%s")
# 正则表达式匹配JIRA issue键,比如PROJ-123
ISSUE_KEY_REGEX="[A-Z]+-[0-9]+"
# 使用grep获取包含JIRA issue键的提交信息
MATCHED_ISSUES=$(echo "$LAST_COMMIT" | egrep -o "$ISSUE_KEY_REGEX")
# 检查JIRA服务器的API,获取issue的状态
# 替换下面的URL和CREDENTIALS为实际的JIRA API端点和凭证
JIRA_API_URL="http://10.x.x.x:8090/rest/api/2/issue/"
CREDENTIALS="admin:Admin"
# 检查每个issue的状态
for ISSUE in $MATCHED_ISSUES
do
ISSUE_STATUS=$(curl -u $CREDENTIALS $JIRA_API_URL$ISSUE | jq -r '.fields.status.name')
if [ "$ISSUE_STATUS" = "已关闭" ]; then
echo "ERROR: Issue $ISSUE is closed. You cannot commit against a closed issue."
exit 1
fi
done
exit 0
参考:
https://gerrit-googlesource.proxy.ustclug.org/plugins/hooks/
https://www.cnblogs.com/lihonglin/articles/15396429.html
网友评论