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 - 简书

相关推荐
nhfc992 分钟前
Gitlab备份且提交Windows服务器数据
服务器·windows·gitlab
Knight_AL2 小时前
Jenkins 配置 GitLab 认证并实现自动化部署
自动化·gitlab·jenkins
危笑ioi1 天前
docker部署jenkins/gitlab/nexus3/nginx配置端口转发与ssl
nginx·docker·gitlab·jenkins·ssl
木易 士心3 天前
GitLab 安装指南
git·gitlab
Linux运维技术栈3 天前
GitLab社区版备份优化:3M包为何是独立完整备份?
运维·git·gitlab
z.q.xiao4 天前
【镜像模式】WSL如何访问windows内网服务
linux·网络·windows·gitlab·wsl·dns
github.com/starRTC5 天前
Claude Code中英文系列教程17:将Claude Code集成在GitLab工作流里面
git·gitlab·github
阿凡达蘑菇灯6 天前
git安装--gitlab操作
git·gitlab
凉云生烟6 天前
cpolar助力Grafana告别局域网束缚!让数据监控触手可及
服务器·网络·信息可视化·gitlab·内网穿透
shimly1234566 天前
为公司服务器配置 gitlab CI/CD
gitlab