Gerrit 3.1.0 Code Review (代碼審查)

Code Review

Base on Gerrit-3.1.0, Gitlab, Jenkins

Gerrit

Install

  1. download 安裝包 gerrit*.war;
  2. Strongly recommended 添加系統(tǒng) 用戶 gerrit;
# 添加用戶
user add gerrit
# 切換用戶
su gerrit
  1. 安裝
java -jar gerrit*.war init -d ~/gerrit_site

Attention

  1. 為方便進(jìn)行多用戶管理 鑒權(quán)方式改為 http
[auth]
        type = HTTP
  1. 應(yīng)用反向代理
Behind reverse proxy           [y/N]? y
  1. canonicalWebUrl 對(duì)外訪問(wèn)地址
  2. listenUrl = proxy-http://*:8081/ http 監(jiān)聽(tīng)地址

配置文件

[gerrit]
        basePath = git
        canonicalWebUrl = http://190.168.1.27:8081/
        serverId = ba92321d-bb2c-4c13-9d0c-7a4c2e43fce8
[container]
        javaOptions = "-Dflogger.backend_factory=com.google.common.flogger.backend.log4j.Log4jBackendFactory#getInstance"
        javaOptions = "-Dflogger.logging_context=com.google.gerrit.server.logging.LoggingContext#getInstance"
        user = gerrit
        javaHome = /usr/java/jdk1.8.0_201-amd64/jre
[index]
        type = lucene
[auth]
        type = HTTP
[receive]
        enableSignedPush = true
[sendemail]
        smtpServer = smtp.126.com
        smtpServerPort = 465
        smtpEncryption = SSL
        smtpUser = xxx@126.com
        from = xxx@126.com
[sshd]
        listenAddress = *:29418
[httpd]
        listenUrl = proxy-http://*:8081/
[cache]
        directory = cache
[plugins]
        # allow install plugin
        allowRemoteAdmin = true

安裝官方參考 Install

設(shè)置

Nginx 反向代理配置

server {
     listen 81;
     server_name www.example.com;

     auth_basic "Welcomme to Gerrit Code Review Site!";
     auth_basic_user_file  /home/gerrit/gerrit_site/etc/gerrit.password;

     location / {
        proxy_pass  http://127.0.0.1:8081/;
        #proxy_set_header X-Forwarded-For $remote_addr;
        #proxy_set_header Host $host;
     }
   }
  • proxy_pass 對(duì)應(yīng) gerrit 配置中 listenUrl屬性, like this, "proxy-http://*:8081/"

添加用戶

htpasswd -cb gerrit_site/etc/gerrit.password gerrit gerrit

Attention:

  • 默認(rèn) 添加的第一個(gè)為 管理員;
  • 只有在登錄之后,用戶信息才會(huì)被寫入數(shù)據(jù)庫(kù)。

注冊(cè)郵箱

后續(xù) Push, Review, Verify, Submit, 接收通知 等都需要郵箱的支持。

  1. 登錄 -> 進(jìn)入 Settings -> Email Addresses;
  2. 輸入郵箱地址 xxx@xxx.com, Click “Send VERIFICATION” button 發(fā)送驗(yàn)證郵件;
  3. 接收郵件 訪問(wèn) 驗(yàn)證鏈接;
  4. done.
Settings_Gerrit_email png.png

配置ssh 免密登錄

避免 Pull, Push 時(shí)頻繁輸入密碼。

配置 gerrit 服務(wù)端到 gitlab 的 ssh 免密登錄
  1. 為 gitlab 添加同名管理員賬戶 gerrit;
  2. 為 gerrit 服務(wù)端 系統(tǒng)用戶 gerrit 生成 rsa 密鑰;
# 為 gerrit 用戶生成 ssh 密鑰
ssh-keygen -t rsa -C gerrit
  1. 將公鑰添加到 gitlab 系統(tǒng) gerrit 管理員賬戶 下;
  2. done.

登錄 gitlab -> 進(jìn)入 Settings -> SSH keys;

gerrit_add_gitlab_pubkey.png
配置 開(kāi)發(fā)端到 gerrit 的 ssh 免密登錄

可選。ssh 方式 用。

  1. 確保開(kāi)發(fā)端具有與 gerrit 注冊(cè)賬戶同名的用戶, like dev1;
  2. 在開(kāi)發(fā)端生成 該用戶 dev1 的 rsa 密鑰;
# 為 dev1 用戶生成 ssh 密鑰
ssh-keygen -t rsa -C dev1
  1. 將公鑰添加到 gerrit 系統(tǒng) dev1 賬戶 下;

登錄 gerrit -> 進(jìn)入 Settings -> SSH keys;

gerrit_add_ssh_pub_key.png

從 gerrit 系統(tǒng)獲取 http credentials

可選。http 方式 Pull, Push 時(shí)用

  1. 登錄 gerrit -> 進(jìn)入 Settings -> HTTP Credentials;
  2. 點(diǎn)擊 "GENERATE NEW PASSWORD" Button 獲取 password;
  3. done.


    gerrit_http_credentials.png

添加項(xiàng)目

BROWSE -> Repositories -> CREATE NEW;


image.png

若使用外部倉(cāng)庫(kù),以 Gitlab 為例。

  1. 在 gerrit UI 創(chuàng)建倉(cāng)庫(kù) repo-demo(倉(cāng)庫(kù)名稱須與 gitlab 中的項(xiàng)目名稱相同)
  2. 去 gerrit 倉(cāng)庫(kù)目錄下刪除 repo-demo, 從 Gitlab 克隆同名項(xiàng)目替換之 加上 --bare 參數(shù);
git clone --bare ssh://git@xxx.xxx.xxx/gerrit/repo-demo.git

代碼審查流程

  1. Coder 提交代碼,實(shí)際代碼到了 gerrit 對(duì)應(yīng)倉(cāng)庫(kù)的暫存區(qū)(staging area), 供 Review 和 Verify;
  2. Review, Verify -> Passed -> Submit, 代碼到了 gerrit 對(duì)應(yīng)倉(cāng)庫(kù)的 master 分支;
  3. done.

參考文檔 代碼審查流程

開(kāi)發(fā)端 Pull 代碼

倉(cāng)庫(kù)地址在倉(cāng)庫(kù)詳情頁(yè)。

Attention: Pull 時(shí)選擇 "Clone with commit-msg hook" 方式以便 Commit 時(shí)通過(guò) commit-msg (.git/hooks/commit-msg) 生成 Change-Id

  • Http 方式;
  • ssh 方式;
gerrit 內(nèi)建分支

推送之前有必要講一下 推送涉及到的分支

  • refs/for/*
  • refs/*
refs/for/*

這個(gè)Reference 是用于 Review 的。

The refs/for/ prefix is used to map the Gerrit concept of "Pushing for Review" to the git protocol. For the git client it looks like every push goes to the same branch, e.g. refs/for/master but in fact for each commit that is pushed to this ref Gerrit creates a new branch under the refs/changes/ namespace. In addition Gerrit creates an open change.

refs/*

配置 Verified 權(quán)限的時(shí)候 要針對(duì)這個(gè) Reference 進(jìn)行配置。

開(kāi)發(fā)端 Push 代碼

Attention: 確保 commit footer 有 Change-Id 信息。

Change-Id 可從通過(guò) commit-msg (.git/hooks/commit-msg) 生成。

Strongly recommended:
只需確保項(xiàng)目目錄(.git/hooks/)下有 commit-msg 腳本,再配合對(duì)應(yīng) 的 gerrit 插件即可在 Commit 時(shí)完成 Change-Id 的自動(dòng)生成。

Idea, Eclipse 都有相應(yīng)的 gerrit 插件。

Change-Id

用于標(biāo)識(shí) commit。

Parent:     65c50d48 (Update Dockerfile base image to 0.1.2)
Author:     xxxx <xxxxx@qq.com>
AuthorDate: 2019-12-12 17:41:51 +0800
Commit:     xxxx <xxxxx@qq.com>
CommitDate: 2019-12-12 17:41:51 +0800

add readme.md;

Change-Id: Iba58fbc6a1a440a9d3e2e48bbd72f898ba3cabc0

開(kāi)啟權(quán)限

進(jìn)入對(duì)應(yīng)的項(xiàng)目 -> 選擇菜單 "Access" 選項(xiàng)

  • 開(kāi)啟 Verify 權(quán)限

    為 "refs/*" Reference 添加 Label Verified 相關(guān)權(quán)限,并賦權(quán)給相應(yīng)的 Group, 否則無(wú)權(quán)限進(jìn)行 Verify。

    gerrit_verified_config.png
  • 繞過(guò)審核直接提交

  1. 開(kāi)啟相應(yīng)人員的 Submit 權(quán)限;

  2. 提交時(shí)在相應(yīng)分支后加上 %submit;

    Idea gerrit 插件中 Push 時(shí)勾選 Submit Change 選項(xiàng)即可。

參考文檔 Gerrit-訪問(wèn)控制

Review& Verified

YOUR -> Changes -> 選擇對(duì)應(yīng)的 Change -> REPLY;

gerrit_reply.png
  1. 添加接收者,抄送者,評(píng)論,并對(duì) Code-Review, Verified(若有權(quán)限) 打分。

  2. 若 Code-Review Verified 對(duì)應(yīng)的分?jǐn)?shù)均符合 Sumbmit 要求,
    則 當(dāng)前Change 狀態(tài)變?yōu)?Ready to submit, 放出 Sumbim 按鈕。

  3. 點(diǎn)擊 Submit 按鈕 完成 變更往 gerrit 倉(cāng)庫(kù) master 分支的提交。

Replication

與外部 git 倉(cāng)庫(kù)同步

config path: gerrit_home/etc/replication.config

target 有兩種連接方式:

  • ssh
  • http
[remote "gitlab_domain"]
# http 方式
#  url = https://username:password@gitlab_domain/xxxx/${name}.git
# ssh 方式
  url = ssh://git@gitlab_domain/xxxx/${name}.git
  push = +refs/heads/*:refs/heads/*
  push = +refs/tags/*:refs/tags/*
  push = +refs/changes/*:refs/changes/*
  timtout = 30
  threads = 3

關(guān)閉 Gitlab 的提交權(quán)限

后續(xù)開(kāi)發(fā)端提交代碼走 gerrit, 不再走 gitlab。

Jenkins Gerrit trigger TODO

  • 為 Jenkins 添加 插件 Gerrit trigger
  • 添加 Gerrit Server
  • 為 Job 配置 Gerrit trigger

涉及的問(wèn)題

  1. send email 失敗

安裝完之后,記得加入 from 屬性!

[sendemail]
        smtpServer = smtp.126.com
        smtpServerPort = 465
        smtpEncryption = SSL
        smtpUser = xxx@126.com
        from = xxx@126.com
  1. 開(kāi)發(fā)端 Pull Code 無(wú)權(quán)限

因 canonicalWebUrl 配置錯(cuò)誤導(dǎo)致的 gerrit 倉(cāng)庫(kù)地址生成錯(cuò)誤,
配置文件檢查

  1. 開(kāi)發(fā)端 Push Code 無(wú)權(quán)限

push 時(shí)的密碼是 http credentials, 不是賬戶密碼!

  1. 開(kāi)發(fā)端 Push 被拒(缺少Change-Id)

去 gerrit 對(duì)應(yīng)的 repository 里(.git/hooks/)拿 commit-msg,
通過(guò) commit-msg 生成 Change-Id, 可借助插件在 commit 時(shí)自動(dòng)生成。

  1. You don't have permission to verify

為對(duì)應(yīng)項(xiàng)目的 "refs/*" Reference 添加 Label Verified 相關(guān)權(quán)限,并賦權(quán)給相應(yīng)的 Group

  1. replication Error

配置文件有誤

  • 格式不對(duì);
  • 目標(biāo)倉(cāng)庫(kù)地址錯(cuò)誤;
  • 目標(biāo)倉(cāng)庫(kù)地址鑒權(quán)信息(ssh/account)錯(cuò)誤;
  • ...
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容