Gerrit代碼評審流程
作者:尹正傑
版權聲明:原創作品,謝絕轉載!否則將追究法律責任。
一.代碼評審流程(如下圖所示)
第一步:貢獻者報建一個提交,並通過之前從gerrit下載的commit-msg hook分配一個新的全局chang-id。 (提交並不代表着說代碼被推送) 第二步:貢獻者推送代碼到gerrit評審分支,為評審創建了一個新的更改。 Gerrit並且會分配一個唯一的URL,以便別人使用這個URL進行訪問和檢查。 第三步: 這一步是可選的。持續集成構建並驗證更改:jenkins獲取gerrit代碼發生改變,並自動觸發檢查。然后把結果反饋給gerrit。要么是積極的(+1),或者是消極的。就是負分。 第四步: 貢獻者邀請一個或多個審查人員進行代碼審核並提供他們的反饋結果。別人可以自發地添加他們自己的評審意見。 第五步: 在評分的步驟中:所有被邀請人員和項目權限管理人員,都可以對代碼進行評價。結果是"-2"到"+2"之間 第六步: 根據定義的規則,來校驗評審結果。按默認規則來說,如果是-2分,直接否決代碼成果。需要開發員進行重新開發和提交。如果是+2分,可以合並。但如果有一個-2分就不能提交合並。 第七步:如果根據第六步的規則審核未過。開發人員需要返工。 第八步: 如果評審反饋是負面的,則可以放棄更改。這樣其它人也不會花費更多的精力來評審它了。被放棄的更改不會從git存儲庫中刪除。 第九步:如果評審反饋是正面的,那么可以提交更改,提交會自動觸發合並步驟。 第十步:如果合並中出現沖突,那么則必須代碼必須重新更改,驗證步驟返回第二步、
二.添加主題(topic)的兩種方式
1>.本地提交代碼(下面演示的為我客戶端完整提交到Gerrit的完整操作)
[root@node203.yinzhengjie.org.cn ~]# git clone "ssh://jason@node201.yinzhengjie.org.cn:29418/yinzhengjie-code" Cloning into 'yinzhengjie-code'... remote: Counting objects: 2, done remote: Finding sources: 100% (2/2) remote: Total 2 (delta 0), reused 0 (delta 0) Receiving objects: 100% (2/2), done. [root@node203.yinzhengjie.org.cn ~]# [root@node203.yinzhengjie.org.cn ~]# [root@node203.yinzhengjie.org.cn ~]# cd yinzhengjie-code/ [root@node203.yinzhengjie.org.cn ~/yinzhengjie-code]# [root@node203.yinzhengjie.org.cn ~/yinzhengjie-code]# [root@node203.yinzhengjie.org.cn ~/yinzhengjie-code]# git config user.name "jason" [root@node203.yinzhengjie.org.cn ~/yinzhengjie-code]# git config user.email "y1053419035@qq.com" [root@node203.yinzhengjie.org.cn ~/yinzhengjie-code]# [root@node203.yinzhengjie.org.cn ~/yinzhengjie-code]# curl -Lo .git/hooks/commit-msg http://172.30.1.201:8080/tools/hooks/commit-msg % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 4781 100 4781 0 0 71145 0 --:--:-- --:--:-- --:--:-- 71358 [root@node203.yinzhengjie.org.cn ~/yinzhengjie-code]# [root@node203.yinzhengjie.org.cn ~/yinzhengjie-code]# ll .git/hooks/commit-msg -rw-r--r-- 1 root root 4781 Jun 19 04:23 .git/hooks/commit-msg [root@node203.yinzhengjie.org.cn ~/yinzhengjie-code]# [root@node203.yinzhengjie.org.cn ~/yinzhengjie-code]# chmod +x .git/hooks/commit-msg [root@node203.yinzhengjie.org.cn ~/yinzhengjie-code]# [root@node203.yinzhengjie.org.cn ~/yinzhengjie-code]# ll .git/hooks/commit-msg -rwxr-xr-x 1 root root 4781 Jun 19 04:23 .git/hooks/commit-msg [root@node203.yinzhengjie.org.cn ~/yinzhengjie-code]# [root@node203.yinzhengjie.org.cn ~/yinzhengjie-code]# echo "My first code review" > code-review-sample.txt [root@node203.yinzhengjie.org.cn ~/yinzhengjie-code]# [root@node203.yinzhengjie.org.cn ~/yinzhengjie-code]# git add code-review-sample.txt [root@node203.yinzhengjie.org.cn ~/yinzhengjie-code]# [root@node203.yinzhengjie.org.cn ~/yinzhengjie-code]# git commit -m "First Change for review" [master d0b962a] First Change for review 1 file changed, 1 insertion(+) create mode 100644 code-review-sample.txt [root@node203.yinzhengjie.org.cn ~/yinzhengjie-code]# [root@node203.yinzhengjie.org.cn ~/yinzhengjie-code]# git push origin HEAD:refs/for/master Counting objects: 3, done. Delta compression using up to 4 threads. Compressing objects: 100% (2/2), done. Writing objects: 100% (3/3), 321 bytes | 0 bytes/s, done. Total 3 (delta 0), reused 0 (delta 0) remote: Processing changes: new: 1, done remote: remote: New Changes: remote: http://172.30.1.201:8080/#/c/yinzhengjie-code/+/1 First Change for review remote: To ssh://node201.yinzhengjie.org.cn:29418/yinzhengjie-code * [new branch] HEAD -> refs/for/master [root@node203.yinzhengjie.org.cn ~/yinzhengjie-code]# [root@node203.yinzhengjie.org.cn ~/yinzhengjie-code]#
2>.查看Gerrit的Web UI
3>.查看詳細的用戶提交信息
4>.添加主題有兩種方式
在將代碼push到Gerrit時添加相應的參數來指定主題,我上面測試是沒有添加的,因此只能通過圖形界面進行添加,如果通過命令行添加,可在push時使用以下命令: "git push origin HEAD:refs/for/master%topic=first-topic"
5>.主題修改成功
三.分配審核者(Reviewers)的兩種方式
1>.查看接收者(由於我們在提交的時候並沒有在命令行中指定添加的審核者,因此點開提交信息發現並沒有審核者信息)
2>.在Web UI中添加審核者信息
在將代碼push到Gerrit時添加相應的參數指定主題和審核者信息,我上面測試是沒有添加的,因此只能通過圖形界面進行添加,如果通過命令行添加,可在push時使用以下命令: "git push origin HEAD:refs/for/master%topic=first-topic,r=yinzhengjie <y1053419035@qq.com>"
3>.點擊"add"添加評審者

[root@node201.yinzhengjie.org.cn ~]# cat /yinzhengjie/softwares/gerrit/soft/etc/gerrit.config [gerrit] basePath = git serverId = d3dfa4e4-9c2c-4203-abfc-10bca465fa65 canonicalWebUrl = http://172.30.1.201:8080 ui = polygerrit enableGwtUi = true [database] type = mysql hostname = node201.yinzhengjie.org.cn port = 3306 database = gerrit username = gerrit [index] type = LUCENE [auth] type = ldap gitBasicAuthPolicy = HTTP [receive] enableSignedPush = false [sendemail] smtpServer = smtp.qq.com smtpServerPort = 465 smtpEncryption = SSL smtpUser = y1053419035@qq.com from = y1053419035@qq.com [container] user = gerrit javaHome = /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.212.b04-0.el7_6.x86_64/jre [sshd] listenAddress = node201.yinzhengjie.org.cn:29418 [httpd] listenUrl = http://node201.yinzhengjie.org.cn:8080/ [cache] directory = cache [ldap] server = ldap://node202.yinzhengjie.org.cn:389 username = cn=Manager,dc=yinzhengjie,dc=org,dc=cn accountBase = ou=People,dc=yinzhengjie,dc=org,dc=cn groupBase = ou=Group,dc=yinzhengjie,dc=org,dc=cn [root@node201.yinzhengjie.org.cn ~]# [root@node201.yinzhengjie.org.cn ~]#
四.評審者進行評審級命令行的追加修改
1>.評審者收到鏈接后需要登陸Gerrit平台
2>.查看評審信息(需要注意的是,盡管不是評審者本人,也可以看到如下圖所示的消息喲~)
3>.review代碼
4>.對局部代碼進行評論操作
5>.評論代碼成功
6>.將代碼下載到本地
[root@node203.yinzhengjie.org.cn ~]# git clone "ssh://jason@node201.yinzhengjie.org.cn:29418/yinzhengjie-code" Cloning into 'yinzhengjie-code'... remote: Counting objects: 2, done remote: Finding sources: 100% (2/2) remote: Total 2 (delta 0), reused 0 (delta 0) Receiving objects: 100% (2/2), done. [root@node203.yinzhengjie.org.cn ~]# [root@node203.yinzhengjie.org.cn ~]# cd yinzhengjie-code/ [root@node203.yinzhengjie.org.cn ~/yinzhengjie-code]# [root@node203.yinzhengjie.org.cn ~/yinzhengjie-code]# git fetch "http://172.30.1.201:8080/yinzhengjie-code" refs/changes/01/1/1 && git checkout FETCH_HEAD remote: Counting objects: 4, done remote: Finding sources: 100% (3/3) remote: Total 3 (delta 0), reused 3 (delta 0) Unpacking objects: 100% (3/3), done. From http://172.30.1.201:8080/yinzhengjie-code * branch refs/changes/01/1/1 -> FETCH_HEAD Note: checking out 'FETCH_HEAD'. You are in 'detached HEAD' state. You can look around, make experimental changes and commit them, and you can discard any commits you make in this state without impacting any branches by performing another checkout. If you want to create a new branch to retain commits you create, you may do so (now or later) by using -b with the checkout command again. Example: git checkout -b <new-branch-name> HEAD is now at c1985ca... First Change for review [root@node203.yinzhengjie.org.cn ~/yinzhengjie-code]# [root@node203.yinzhengjie.org.cn ~/yinzhengjie-code]# ll total 4 -rw-r--r-- 1 root root 21 Jun 19 06:37 code-review-sample.txt [root@node203.yinzhengjie.org.cn ~/yinzhengjie-code]# [root@node203.yinzhengjie.org.cn ~/yinzhengjie-code]# echo "Change amended" >> a.txt [root@node203.yinzhengjie.org.cn ~/yinzhengjie-code]# [root@node203.yinzhengjie.org.cn ~/yinzhengjie-code]# git commit --amend #隨機編輯文件內容 [root@node203.yinzhengjie.org.cn ~/yinzhengjie-code]# git push origin HEAD:refs/for/master Counting objects: 3, done. Delta compression using up to 4 threads. Compressing objects: 100% (2/2), done. Writing objects: 100% (3/3), 357 bytes | 0 bytes/s, done. Total 3 (delta 0), reused 0 (delta 0) remote: Processing changes: updated: 1, done remote: (I) 6770e65: no files changed, message updated remote: remote: Updated Changes: remote: http://172.30.1.201:8080/#/c/yinzhengjie-code/+/1 SECOND Change for review remote: To ssh://node201.yinzhengjie.org.cn:29418/yinzhengjie-code * [new branch] HEAD -> refs/for/master [root@node203.yinzhengjie.org.cn ~/yinzhengjie-code]#
7>.我們發現有對應的Changes
8>.我們可以對給代碼打分