Gitlab per-recive预处理配置

背景:通过git提交规约对开发者的提交进行限制,为了后期对开发者提交动作进行分析与相关数据的报表展示。

 

Gitlab提供的钩子

          1.1 单一仓库

              通过此模式,我们可以将自定义的预处理代码部署到指定的仓库。

 

          1.2 激活方式

                源码安装的配置目录,通常如下:

             /home/git/repositories/<group>/<project>.git

 

                官方源安装的配置目录,通常如下:

             /var/opt/gitlab/git-data/repositories/<group>/<project>.git

 

                在项目目录下创建custom_hooks目录:

                   mkdir -pv /var/opt/gitlab/git-data/repositories/<group>/<project>.git/custom_hooks

 

                 新建一个名为"pre-receive"的开发文件,这个是官方规定。接下来可以进行自定义编码。

               

          2.1 全局

              通过此模式,我们可以将自定义的预处理代码应用到整个仓库。

 

      2.2 激活方式:

              通过编辑/etc/gitlab/gitlab.rb配置文件,增加gitlab_shell['custom_hooks_dir']参数来配置自定义配置文件存放位置,如下是我的配置:

              gitlab_shell['custom_hooks_dir'] = "/opt/gitlab/embedded/service/gitlab-shell/hooks/custom_hooks_dir"

 

              刷新Gitlab配置(重启库操作,生产环境需要评估操作时间)

          gitlab-ctl reconfigure

          gitlab-ctl restart

 

        配置更新成功后,需要在自定义钩子的目录下创建其中一个文件夹pre-receive.dpost-receive.dupdate.d  这个是官方的规定:

                mkdir -pv /opt/gitlab/embedded/service/gitlab-shell/hooks/custom_hooks_dir/pre-receive.d

 

              新建一个名为"pre-receive"的开发文件,这个是官方规定。接下来可以进行自定义编码。

             

二、实现DEMO

#!/usr/bin/env python
# -*- coding:utf-8 -*-
import sys
import re
import subprocess
import json

match_pattern=['feat', 'fix', 'docs', 'style', 'refactor', 'perf', 'test', 'build', 'ci', 'chore', 'revert', 'merge']
regex = re.compile("(" + "|".join(match_pattern) + ")")
line = sys.stdin.read()
(base, commit, ref) = line.strip().split()
new_branch_push = re.match(r'[^1-9A-Za-z]+', base)
branch_deleted = re.match(r'[^1-9A-Za-z]+', commit)
contains_commit_msg = False
if not new_branch_push:
    revs = base + "..." + commit
    #proc = subprocess.Popen(['git', 'rev-list','--oneline','--first-parent', revs], stdout=subprocess.PIPE)
    proc = subprocess.Popen(['git', 'log', '--first-parent', revs, '--pretty=format:{"author":"%cn","subject":"%s"}'], stdout=subprocess.PIPE)
    lines = proc.stdout.readlines()
    if lines:
        for line in lines:
            rev = json.loads(str(line))
            print(rev)
            try:
                subject_type, subject_message = rev['subject'].split(':', 1)
            except:
                contains_commit_msg = False
                break
            subject_message = subject_message.strip()
            print subject_message
            match = regex.search(subject_type.lower())
            if match:
                contains_commit_msg = True
if contains_commit_msg or new_branch_push or branch_deleted:
    print "Commit success"
    exit(0)
else:
    print('\033[1;31;40m')     #下一目标输出背景为黑色,颜色红色高亮显示
    print('*' * 50)
    print('\033[7;31m不合规的提交格式!')  #字体颜色红色反白处理
    print('\033[7;31m参考示例: git commit -m "feat 666: New FEAT"')  #字体颜色红色反白处理
    print('\033[7;31m更多请参考:https://git.com.cn/docs/SA\033[1;31;40m') 
    print('*' * 50)
    print('\033[0m')
    exit(1)
View Code

 

参考文档:

官方说明(请参考自己使用的版本说明书): https://docs.gitlab.com/ee/administration/server_hooks.html

为你的 GitLab 增加提交信息检测 _: https://mritd.com/2018/05/11/add-commit-message-style-check-to-your-gitlab/

posted @ 2021-03-17 19:27  九个问题  阅读(1007)  评论(0编辑  收藏  举报