Gerrit 接入门禁

一、背景
目前gerrit没有接入门禁检查,希望能通过门禁自动检查commit message 合规性
二、操作步骤(不需要重启系统)
2.1 添加hooks(根据需要调整)
hooks文件: /data/gerrit/gerrit_application/hooks/commit-received

#!/bin/bash

# 从脚本参数中获取必要的信息
project_name=$2
refname=$4
uploader=$6
uploader_username=$8
oldrev=${10}
newrev=${12}
cmdref=${14}
#echo ${project_name}
#echo $newrev
# 切换到项目的git仓库目录
cd "/data/gerrit/gerrit_application/git/${project_name}.git"

# 使用git log来获取最新的commit message
# 由于newrev指向最新的commit,我们使用^来获取它的父提交,
# 然后比较这两个提交来获取最新的commit message
commit_msg=$(git log --pretty=format:%B ${oldrev}..${newrev})

# 输出commit message
commit_msg=$(echo "$commit_msg" | head -n 1 | xargs)
#echo "Commit Message: $commit_msg"
# 你可能还需要在这里添加更多的逻辑,例如验证commit message的格式,
# 或者将message写入日志文件等

# 调用Python脚本并传递commit_msg作为参数
python3 /data/gerrit/gerrit_application/script/check_commit_msg.py "$commit_msg"

# 检查上一个命令的退出状态码
if [ $? -eq 0 ]; then
    echo ""
else
    #echo "       commit message: $commit_msg 格式检查不通过,请尝试检查下列事项:
    #   commit message 格式是 类型(模块):单空格[jira id]单空格Description
    #   例如: feat(isp): [cr_id_skip] Test"
    exit 1
fi

2.2 添加脚本(根据需要调整)
脚本文件:/data/gerrit/gerrit_application/script/check_commit_msg.py

import sys
import re

def check_commit_message(commit_msg):
    # 定义正则表达式规则,这里以你提供的规则为例
    pattern = r'^(feat|fix|bugfix|hotfix|docs|style|refactor|perf|test|chore)\(.*\): \[[a-zA-Z][a-zA-Z0-9_]+-[1-9][0-9]*\] [A-Z].*'
    pattern1 = r'^(feat|fix|bugfix|hotfix|docs|style|refactor|perf|test|chore)\(.*\): \[cr_id_skip\] [A-Z].*'
    pattern2 = r'([^\s|]+(?:\|[^\s|]+)*)\s+(?:([^\s|]+(?:\|[^\s|]+)*)\s*)?(feat|fix|docs|style|refactor|perf|test|chore) \[[a-zA-Z][a-zA-Z0-9_]+-[1-9][0-9]*\] [A-Z].*'
    pattern3 = r'([^\s|]+(?:\|[^\s|]+)*)\s+(?:([^\s|]+(?:\|[^\s|]+)*)\s*)?(feat|fix|docs|style|refactor|perf|test|chore) \[cr_id_skip\] [A-Z].*'
    
    # 使用正则表达式匹配提交信息
    if re.match(pattern, commit_msg) or re.match(pattern1, commit_msg) or re.match(pattern2, commit_msg) or re.match(pattern3, commit_msg):
        pass
        # 在这里执行所需的操作,例如创建文件、修改数据等
        # 例如:with open('somefile.txt', 'w') as f: f.write('Some operation based on commit message.')
    else:
        print(f"Error: commit message: {commit_msg} 格式检查不通过,请尝试检查下列事项:\n       commit message 格式是 类型(模块):单空格[jira id]单空格Description \n       例如: feat(isp): [cr_id_skip] Test")
        sys.exit(1)  # 退出代码1表示有错误发生

if __name__ == "__main__":
    if len(sys.argv) != 2:
        print(len(sys.argv))
        print("Usage: python check_commit_msg.py <commit_msg>")
        sys.exit(1)

    commit_msg = sys.argv[1]
    check_commit_message(commit_msg)

2.3 参考文档
https://stackoverflow.com/questions/69433254/gerrit-server-hook-to-validate-the-commit-message
https://gerrit.googlesource.com/plugins/hooks/+/HEAD/src/main/resources/Documentation/hooks.md

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值