GitLab server integrates CheckStyle to realize automatic code review

Posted by falcon8253 on Fri, 24 Dec 2021 18:24:48 +0100

1. General

For Git, static code detection through Hook can be divided into two directions:

1> With the help of client side hook.

This method corresponds to ${project_root} / Git / hooks / pre commit script implementation.

2> With the help of server side hook.

This method is divided into two aspects: global configuration (described in detail below) and configuration of individual items. This method corresponds to the configuration of Git server
/var/opt/gitlab/git-data/repositories/<group>/<project>. git/custom_ Hooks / pre receive script.

In contrast, among the two implementation methods, the second method is a little more obvious. It does not require any local operation by R & D personnel. Here is a detailed explanation of the global configuration of gitlab.

3> Key components used include:

checkstyle8.17+python2.7.5+jdk1.8

python version cannot exceed 3.0

The minimum version of Jdk is 1.8

2. Operation steps

2.1: upload files

Log in to Gitlab service

1. Create a file path and execute the command:

mkdir -p /data/gitlab.checkstyle

2. Enter the file

cd /data/gitlab.checkstyle

3. Set checkstyle XML and checkstyle-8.17-all Upload the jar file to gitlab checkstyle

4. Verify whether it is correct

Executable commands:

java -jar /data/gitlab.checkstyle/checkstyle-8.17-all.jar -c/data/gitlab.checkstyle/checkstyle.xml /data/gitlab.checkstyle/TestController.java

If something similar is output, it means that the environment is correct.

TestController.java files can be uploaded by themselves.

checkstyle.xml files can be found on the Internet by themselves. You can use Google or Alibaba.

2.1. Configure gitlab global hook

1. Create a custom global hook directory

cd /opt/gitlab/embedded/service/gitlab-shell/

mkdir custom_hooks

2. Specify the global custom hook directory

Modify / etc / gitlab / gitlab Configuration item in Rb configuration file: gitlab_shell['custom_hooks_dir '] contents are:

/opt/gitlab/embedded/service/gitlab-shell/custom_hooks

The original content is:

The revised content is:
/opt/gitlab/embedded/service/gitlab-shell/custom_hooks

vi /etc/gitlab/gitlab.rb

3. Make custom content effective

After the operation is completed, execute the command to make the configuration effective:

sudo gitlab-ctl reconfigure

2.3 upload code specification check script

1. Enter the global custom hook Directory:

cd /opt/gitlab/embedded/service/gitlab-shell/custom_hooks

2. Create pre receive D folder

mkdir pre-receive.d

cd pre-receive.d

3. Upload the pre receive file in the attachment to pre receive D file directory and give executable permissions

chmod +777 pre-receive

4. The contents of the pre receive script are:

#!/usr/bin/python
#coding=utf-8
import os
import sys
import subprocess
import tempfile
import shutil
__author__ = "lance"
class Trigger(object):
   def __init__(self):
       '''
       Initialization file list information, submitter information, submission time,Branch of current operation
       '''
       self.pushAuthor = ""
       self.pushTime = ""
       self.fileList = []
       self.ref = ""
   def __getGitInfo(self):
       '''
       '''
       self.oldObject, self.newObject, self.ref = sys.stdin.readline().strip().split(' ')
   def __getPushInfo(self):
       '''
       git show Command acquisition push Author, time, and list of documents
       The path of the file is a relative path relative to the root directory of the version library
       '''
       rev = subprocess.Popen('git rev-list '+self.newObject,shell=True,stdout=subprocess.PIPE)
       revList = rev.stdout.readlines()
       revList = [x.strip() for x in revList]
       #Find self. From last submission How many submissions are there after oldobject, that is, the object list submitted by this push
       indexOld = revList.index(self.oldObject)
       pushList = revList[:indexOld]
       pushList.reverse()
       # temp file
       tempdir = tempfile.mkdtemp('git_hook')
       #Loop through the list of files submitted each time
       for pObject in pushList:
           p = subprocess.Popen('git show '+pObject,shell=True,stdout=subprocess.PIPE)
           pipe = p.stdout.readlines()
           pipe = [x.strip() for x in pipe]
           #print("===>",pipe)   
           #Verify java files
           file = pipe[6].strip("diff").strip()
           if not file.lower().endswith('.java'):
              continue
       filename = file.split('/')[-1]
       #git get Tree
       content_hash = pipe[7].strip("index").strip()[9:16]
       content_p = subprocess.Popen('git cat-file -p '+content_hash,shell=True,stdout=subprocess.PIPE)
       cpipe = content_p.stdout.readlines()
       #print(cpipe)
       with open(os.path.join(tempdir, filename), 'w+') as fp:
            fp.writelines(cpipe)
       #self.handler_checkstyle(tempdir+"/"+content_hash+'.java')      
       # checkstyle    
       self.handler_checkstyle(tempdir)
   def getGitPushInfo(self):
       self.__getGitInfo()
       self.__getPushInfo()

 # Processing java files 
   def handler_checkstyle(self, file):
    try:
       cmd = r'java -jar /data/gitlab.checkstyle/lib/checkstyle-8.17-all.jar -c /data/gitlab.checkstyle/style/checkstyle.xml '+file+'/'
       #print(cmd)
       result = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
       rpipe = result.stdout.readlines()
       if len(rpipe)>2:
           print(rpipe)
       exit(1)
    finally:
       shutil.rmtree(file)
       #pass          

if __name__ == "__main__":
   #print("argv: ", sys.argv)
   t = Trigger()
   t.getGitPushInfo()    
   exit(0)

3. Verification

Output exception prompt for non-conforming content:

If you want to know the steps to check a project separately under separate configuration and global configuration, please contact us.

The above is all.

Welcome to WeChat's official account of 10W+:

 

Technical difficulties are welcome to consult. If necessary, add me wechat: 1106915848.

Stars don't ask passers-by, time pays off those who have a heart

Topics: Java Linux git