[DEVOPS] normalized submission log with GitLab global Hook

Keywords: Python git svn Code Style DevOps

Check the submission log specification in GitLab.

1. Preface

Because of its own business characteristics and the growing popularity of Git, the leadership finally made up its mind to carry out relevant pilot projects. So we have some precipitation on SVN in the past few years (e.g [DEVOPS] implement SVN submission log specification with SvnChecker )Accordingly, it also needs to be copied to Git. This is the first check on the commit log specification.

With the Server-side pre commit hook provided by GitLab, we hope to achieve:

  1. The log format must meet the agreed format, otherwise the submission fails.
  2. The Zentao Id involved in the submission log must be real, otherwise the submission fails.

This article is intended to introduce the solution, so the introductory content ends here and goes directly to the topic.

2. Pre commit hook implementation

The relevant Python implementation codes are as follows:

import re
from urllib import urlopen
import json
import os
import sys
import subprocess

def _formatErrorInfo(errorInfo):
    return ("There is a problem with the log format: " + errorInfo) #.decode("utf-8").encode("gbk")

# Following the principle of "running fast in small steps", we decided to pilot in a small scale
#	Non pilot projects will not be inspected
# Before the GitLab callback Hook, some Git related information will be inserted into the environment variable. We can read it in python in the following ways to assist us in some of our requirements
if not "joint3.0" in os.getenv("GL_PROJECT_PATH"):

#Format: "oldref newref branch"
line = sys.stdin.read()
(base, commit, ref) = line.strip().split()
# The new branch will take 00000 as the commit Id, but this is not exactly the case encountered by the author, so this judgment is cancelled here
new_branch_push = False # re.match(r'[^1-9]+', base)
# The commit Id of the deleted branch is 00000, which is also removed here
branch_deleted = False # re.match(r'[^1-9]+', commit)

contains_commit_msg = False
MSG_TIP = ""

if not new_branch_push:
    revs = base + "..." + commit
    proc = subprocess.Popen(['git', 'rev-list','--oneline','--first-parent', revs], stdout=subprocess.PIPE)
    lines = proc.stdout.readlines()

    if lines:
        #for line in lines:
        (msg, flag) = _analysisCommitMsg(lines)
        if flag == 0 :
          contains_commit_msg = True
          MSG_TIP = msg

if contains_commit_msg or new_branch_push or branch_deleted:
    print MSG_TIP

For the verification of the existence of Zentao ID, please also refer to the previously written by the author [DEVOPS] implement SVN submission log specification with SvnChecker .

Install the above pre commit hook

The GitLab version used by the author is CE 12.9.4. Readers please confirm the version difference first.

  1. Modify the configuration item in the / etc/gitlab/gitlab.rb configuration file: gitlab_shell[‘custom_hooks_dir’] = “/opt/gitlab/embedded/service/gitlab-shell/hooks” . The simplest operation in this step is to enable this line of configuration. Remove the line before the configuration #.

  2. Execute the following command to recompile GitLab for the configuration to take effect.

    # Note that the following two steps take a long time. Don't be impatient.
    sudo gitlab-ctl reconfigure
    sudo gitlab-ctl restart
  3. In custom_hooks_dir directory (our custom)_ The folder pre receive. D can be created under the hooks directory. The rules involved are as follows:
    a. Any file can be created under the folder, and gitlab will actively call it in the corresponding hook period
    b. Files with file names ending in ~ are ignored

  4. Copy the pre commit file written in the second step to the folder pre receive. D and empower it (this step is very important. If you find that the script is not called correctly, remember to check the permissions).

4. LInks

  1. GitLab - Server hooks
  2. How do I require specific text in a git commit message?
  3. python script implements git commit hooks hook
  4. Gitlab checks the checkstyle code based on git hooks
  5. [DEVOPS] implement SVN submission log specification with SvnChecker

Posted by monezz on Wed, 01 Sep 2021 17:19:16 -0700