gitlab配置webhook限制提交注释

一、打开gitlab相关配置项

vim /etc/gitlab/gitlab.rb

gitlab_shell['custom_hooks_dir'] = "/etc/gitlab/custom_hooks"

二、创建相关文件夹

mkdir -p /etc/gitlab/custom_hooks

mkdir -p /etc/gitlab/custom_hooks/post-receive.d

mkdir -p /etc/gitlab/custom_hooks/pre-receive.d

mkdir -p /etc/gitlab/custom_hooks/update.d

#将相关脚本放入对应目录

复制代码
#!/usr/bin/env  python3
import sys
import re
import subprocess
#import redis
refname = sys.argv[1]
oldrev  = sys.argv[2]
newrev  = sys.argv[3]
 
 
 
regex='([a-zA-Z0-9]+-\\d+)\\,\\s+.{10,}'
 
 
def check_message_format():
    cmd='git rev-list '+oldrev+'..'+newrev
    print(cmd)
    p = subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE)
    l= p.stdout.readlines()
    for rev in l:
        cmdsub="git cat-file commit "+rev.decode().strip('''\n''')+"  | sed '1,/^$/d'"
        p1 = subprocess.Popen(cmdsub,shell=True,stdout=subprocess.PIPE)
        l1 = p1.stdout.readline()
        print(l1)
        result = re.match('([a-zA-Z0-9]+-\\d+)\\,\\s+.{10,}',l1.decode())
        if result == None:
             print("your commit msg should like : TEST-123,  fix the wrong caution")
             sys.exit(1)
    sys.exit(0)
check_message_format()

三、使配置生效

gitlab-ctl reconfigure

效果:

一、提交代码格式为"jira项目问题编号, 注释信息",例如

git commit -m "SMGJ-120, 测试提交代码"

#注释信息可以中文也可以英文,但是标红处的字符不得少于10个字符。

由于注释信息为6个字,所以被拦截,并且在报错中也给出样例"remote: your commit msg should like : TEST-123, fix the wrong caution"。

二、修改注释,重新提交代码

使用git commit --amend

修改注释信息为"SMGJ-120, 测试提交代码测试提交代码"

三、重新推送代码

直接推送代码,完成。

golang实现gitlab commit注释校验hook - 简书

相关推荐
一念一花一世界4 小时前
DevOps实战(4) - 使用Arbess+GitLab+SourceFare实现Java项目自动化部署
gitlab·tiklab·arbess·开源cicd工具·sourcefare
赵孝正5 小时前
GitLab 分支管理与 Push 问题全解析
大数据·elasticsearch·gitlab
wjs04021 小时前
Git常用的命令
java·git·gitlab
至善迎风1 天前
版本管理系统与平台(权威资料核对、深入解析、行业选型与国产平台补充)
git·gitee·gitlab·github·svm
高旭的旭1 天前
Gitlab 配置自定义 clone 地址
svn·ssh·gitlab·code
一念一花一世界1 天前
DevOps实战(3) - 使用Arbess+GitLab+Hadess实现Java项目自动化部署
gitlab·jenkins·tiklab·arbess·开源cicd工具
faimi2 天前
🚀程序员必收藏!最全Git命令手册:解决90%团队协作难题
前端·gitlab
rivercoder2 天前
Gitea:轻量级的自托管Git服务
git·gitlab·gitea
运维开发王义杰4 天前
信息安全:GitLab与AWS OIDC集成的深度解析,IAM信任策略中的条件配置
云计算·gitlab·aws
运维开发王义杰4 天前
GitLab CI: 告别EC2 Instance Profile,拥抱OIDC
ci/cd·gitlab