您好,登錄后才能下訂單哦!
1.攔截方式
強(qiáng)制進(jìn)行code review,有兩種方式:
將倉庫托管在phabricator上,通過herald的方式來進(jìn)行
在代碼托管服務(wù)器上增加hook來實(shí)現(xiàn)
由于我們的代碼托管到gitlab上,所有我們采用第二種方式進(jìn)行code review
2.gitlab server端添加hook
gitlab添加hook的方式有兩種:
局部添加,作用于當(dāng)前這個(gè)倉庫
全局添加,作用于全部倉庫
2.1 局部配置
cd /srv/gitlab/data/git-data/repositories/root/pipeline-example-go.git ###gitlab serve端進(jìn)入到具體的倉庫路徑下
mkdir custom_hooks #創(chuàng)建自定義hook目錄
touch pre-receive #創(chuàng)建pre-receive 文件
chmod 755 pre-receive #修改文件權(quán)限
pre-receive 鉤子,在有人用 git push 向倉庫推送代碼時(shí)被執(zhí)行,其內(nèi)容如下:
#!/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 的提交信息,默認(rèn)git commit -m 后的信息,但是使用arc diff 之后,arc diff的信息會覆蓋之前的git commit 的內(nèi)容
2.requests.get("https://***/api/differential.query?api.token=***")
#通過ph的api接口獲取到所有的differential 信息,其中token 可通過https://***/conduit/login/ 獲取
3.#獲取differential的信息之后,選取uri為當(dāng)前提交的revision,若狀態(tài)Accepted,表示代碼review通過,退出程序,返回狀態(tài)碼0,表示不攔截
4.#若獲取狀態(tài)不為Accepted,則返回狀態(tài)碼非0,表示執(zhí)行失敗,攔截git push請求
***
2.2 全局配置
1.開啟gitlab的自定義hook參數(shù)
vim /etc/gitlab/gitlab.rb #配置如下
gitlab_shell['custom_hooks_dir'] = "/opt/gitlab/embedded/service/gitlab-shell/hooks/custom_hooks"
#取消這行注釋,默認(rèn)是注釋
2.mkdir -p /opt/gitlab/embedded/service/gitlab-shell/hooks/custom_hooks/pre-receive.d # 創(chuàng)建目錄
3.touch /opt/gitlab/embedded/service/gitlab-shell/hooks/custom_hooks/pre-receive.d/pre-receive #創(chuàng)建文件pre-receive
4.chmod 755 /opt/gitlab/embedded/service/gitlab-shell/hooks/custom_hooks/pre-receive.d/pre-receive
5.pre-receive #文件內(nèi)容如上
6.gitlab-ctl reconfigure #重新加載gitlab的配置, 使配置生效
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。