更新推送校验hooks
更新pre-commit
服务器层校验,需要找到push操作触发的钩子pre-commit,例如文件位置:
/gitlab/embedded/service/gitlab-shell/hooks/pre-receive.d/pre-commit
加入更新hooks校验逻辑,使用正则表达式校验提交注释格式,要求使用定义的规范格式,同时忽略合并类型Merge注释内容。该方法仅能做简单校验,需回收普通用户Merge操作权限。
#!/bin/bash
# echo "开始提交信息检查..."
# 从标准输入获取本次提交的commit id及分支的信息
read normalInput
ARR=($normalInput)
parentCommitId=${ARR[0]}
currentCommitId=${ARR[1]}
branch=${ARR[2]}
#echo "$parentCommitId"
#echo "$currentCommitId"
if [ "$parentCommitId" == "0000000000000000000000000000000000000000" ]; then
exit 0
fi
if [ "$currentCommitId" == "0000000000000000000000000000000000000000" ]; then
exit 0
fi
allMsg=$(git log $currentCommitId -1)
#echo "全部信息: $allMsg"
if [[ $allMsg =~ '^commit ' ]]; then
exit 0
fi
echo "提交分支:$branch"
# 获取coomit的信息,用户,邮箱,msg等
user=$(git log --pretty=format:"%an" $currentCommitId -1)
echo "提交用户:$user"
commitDate=$(git log --pretty=format:"%cd" $currentCommitId -1)
echo "提交日期:$commitDate"
msg=$(git log --pretty=format:"%s" $currentCommitId -1)
msg="$(echo -e "${msg}" | awk '{$1=$1};1')"
#msg="$(echo -e "${msg}" | tr -d '[:space:]')"
echo "提交注释:$msg"
# exit 0
# if the msg is merge then skip it
mergePattern='^Merge '
if [[ $msg =~ $mergePattern ]]; then
echo "push Merge success"
exit 0
fi
commitPattern='^(feat|fix|test|refactor|docs|style|chore|revert|build|perf):(task|req)-([0-9]*):[^\n]{1,200}$'
#echo "提交注释:$msg"
#移除空格空行缩进等,规避99%未知校验不通过的异常
msg=$(tr -d ' \n\t\r' <<< "$msg")
if [[ $msg =~ $commitPattern ]]; then
echo "提交成功"
exit 0
else
echo "提交失败"
echo "提交格式不规范,必须以 [feat,fix,test,refactor,docs,style,chore,revert,build,perf] 开头"
echo "提交注释长度不超过200,且包含task-id/req-id, 格式如 [feat:task-100:ex mesg...]"
钩子中可以获取的信息列表
git log --pretty=format:'%H' -n 1
%H: commit hash
%h: 缩短的commit hash
%T: tree hash
%t: 缩短的 tree hash
%P: parent hashes
%p: 缩短的 parent hashes
%an: 作者名字
%aN: mailmap的作者名字 (.mailmap对应,详情参照[git-shortlog(1)](http://linux.die.net/man/1/git-shortlog)或者[git-blame(1)](http://linux.die.net/man/1/git-blame))
%ae: 作者邮箱
%aE: 作者邮箱 (.mailmap对应,详情参照[git-shortlog(1)](http://linux.die.net/man/1/git-shortlog)或者[git-blame(1)](http://linux.die.net/man/1/git-blame))
%ad: 日期 (--date= 制定的格式)
%aD: 日期, RFC2822格式
%ar: 日期, 相对格式(1 day ago)
%at: 日期, UNIX timestamp
%ai: 日期, ISO 8601 格式
%cn: 提交者名字
%cN: 提交者名字 (.mailmap对应,详情参照[git-shortlog(1)](http://linux.die.net/man/1/git-shortlog)或者[git-blame(1)](http://linux.die.net/man/1/git-blame))
%ce: 提交者 email
%cE: 提交者 email (.mailmap对应,详情参照[git-shortlog(1)](http://linux.die.net/man/1/git-shortlog)或者[git-blame(1)](http://linux.die.net/man/1/git-blame))
%cd: 提交日期 (--date= 制定的格式)
%cD: 提交日期, RFC2822格式
%cr: 提交日期, 相对格式(1 day ago)
%ct: 提交日期, UNIX timestamp
%ci: 提交日期, ISO 8601 格式
%d: ref名称
%e: encoding
%s: commit信息标题
%f: sanitized subject line, suitable for a filename
%b: commit信息内容
%N: commit notes
%gD: reflog selector, e.g., refs/stash@{1}
%gd: shortened reflog selector, e.g., stash@{1}
%gs: reflog subject
%Cred: 切换到红色
%Cgreen: 切换到绿色
%Cblue: 切换到蓝色
%Creset: 重设颜色
%C(...): 制定颜色, as described in color.branch.* config option
%m: left, right or boundary mark
%n: 换行
%%: a raw %
%x00: print a byte from a hex code
%w([[,[,]]]): switch line wrapping, like the -w option of git-shortlog(1).
提交注释格式说明
示例格式头部
[feat,fix,test,refactor,docs,style,chore,revert,build,perf] 开头,续接 req-[禅道需求id]或task-[禅道任务id],续接注释说明。
分号":"和连接符"-"使用英文符号,最后注释说明在200字符以内。
示例格式正例
fix:task-119:修复开票sdk提示报错异常
feat:req-201:加入开票错误远程同步逻辑
id=0时表示无关联需求/任务的特殊提交
定义提交注释规范(自定义)
注释前缀
feat 新功能
fix 修复
docs 文档变更
style 代码格式
refactor 重构
perf 性能优化
test 增加测试
revert 回退
build 打包
chore 构建过程或辅助工具变动
关联id
task表示禅道或其他需求产品管理系统的任务,req表示需求,如task-100表示id为100的任务等等。
注意:当任务或者需求id未知或特殊提交无需关联id,使用task-000、req-000提交,需做相关校验。
关联id可通过在hooks中,发起请求校验,请求服务具备产品需求管理系统的数据访问权限,用以校验id的正确性。
例:
#!/bin/bash
# 获取提交信息注释消息
msg=$(git log -1 --pretty=%B)
# 构建请求的 JSON 数据
data="{\"msg\":\"$msg\"}"
# 发送 POST 请求并将返回内容保存到 tips 变量
tips=$(curl -s -X POST -H "Content-Type: application/json" -d "$data" http://xxx.com/check)
# 输出提示消息
echo "$tips"
自此完整的达到了校验目的。