您好,登錄后才能下訂單哦!
1.攔截方式
強制進行code review,有兩種方式:
將倉庫托管在phabricator上,通過herald的方式來進行
在代碼托管服務器上增加hook來實現
由于我們的代碼托管到gitlab上,所有我們采用第二種方式進行code review
2.gitlab server端添加hook
gitlab添加hook的方式有兩種:
局部添加,作用于當前這個倉庫
全局添加,作用于全部倉庫
2.1 局部配置
cd /srv/gitlab/data/git-data/repositories/root/pipeline-example-go.git ###gitlab serve端進入到具體的倉庫路徑下
mkdir custom_hooks #創建自定義hook目錄
touch pre-receive #創建pre-receive 文件
chmod 755 pre-receive #修改文件權限
pre-receive 鉤子,在有人用 git push 向倉庫推送代碼時被執行,其內容如下:
#!/usr/bin/env python
import sys,os
import fileinput
import re
import json
import requests
def has_been_reviewed(start_commit, end_commit):
cmd = 'git rev-list %s...%s' % (start_commit, end_commit,)
Flag = False
commits = os.popen(cmd).readlines()
pattern = re.compile(r'Differential Revision: (.*)')
for commit in commits:
cmd = 'git rev-list --format=' + '%s%b ' + '--max-count=1 %s' % commit
res = os.popen(cmd).readlines()[-2]
match_str = pattern.match(res)
if not match_str:
print("Please use 'arc diff' to commit")
continue
http_url = match_str.group(1)
url = "https://xxx/api/differential.query?api.token=*****"
info = json.loads(requests.get(url).text)
for i in info['result']:
if i['uri'] != http_url: continue
if i['statusName'] == 'Accepted':
Flag = True
else:
print("Current Status: %s, Need Review and Accepted" % i['statusName'])
break
if Flag:
sys.exit(0)
else:
sys.exit(1)
if __name__ == "__main__":
for line in fileinput.input():
args = line.split(' ')
start_commit = args[0]
end_commit = args[1]
if start_commit != '0000000000000000000000000000000000000000' and end_commit != '0000000000000000000000000000000000000000':
has_been_reviewed(start_commit, end_commit)
代碼解釋:
1.git rev-list --format=%s%b --max-count=1 ${commit_id}
#獲取git commit 的提交信息,默認git commit -m 后的信息,但是使用arc diff 之后,arc diff的信息會覆蓋之前的git commit 的內容
2.requests.get("https://***/api/differential.query?api.token=***")
#通過ph的api接口獲取到所有的differential 信息,其中token 可通過https://***/conduit/login/ 獲取
3.#獲取differential的信息之后,選取uri為當前提交的revision,若狀態Accepted,表示代碼review通過,退出程序,返回狀態碼0,表示不攔截
4.#若獲取狀態不為Accepted,則返回狀態碼非0,表示執行失敗,攔截git push請求
***
2.2 全局配置
1.開啟gitlab的自定義hook參數
vim /etc/gitlab/gitlab.rb #配置如下
gitlab_shell['custom_hooks_dir'] = "/opt/gitlab/embedded/service/gitlab-shell/hooks/custom_hooks"
#取消這行注釋,默認是注釋
2.mkdir -p /opt/gitlab/embedded/service/gitlab-shell/hooks/custom_hooks/pre-receive.d # 創建目錄
3.touch /opt/gitlab/embedded/service/gitlab-shell/hooks/custom_hooks/pre-receive.d/pre-receive #創建文件pre-receive
4.chmod 755 /opt/gitlab/embedded/service/gitlab-shell/hooks/custom_hooks/pre-receive.d/pre-receive
5.pre-receive #文件內容如上
6.gitlab-ctl reconfigure #重新加載gitlab的配置, 使配置生效
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。