使用uwolfer gerrit-rest-java-client獲取Gerrit資訊

weixin_34148340發表於2019-02-11

使用uwolfer gerrit-rest-java-client獲取Gerrit資訊

使用Gerrit來做程式碼管理工具的話,難免要呼叫Gerrit的API。

Gerrit rest api

我們來看個例子,體會下gerrit rest api的互動過程:

請求如下, changes是API,q字串想必gerrit的使用者是熟悉的,n是限定個數:

GET /changes/?q=status:open+is:watched&n=2 HTTP/1.0

響應資訊例:

  HTTP/1.1 200 OK
  Content-Disposition: attachment
  Content-Type: application/json; charset=UTF-8

  )]}'
  [
    {
      "id": "demo~master~Idaf5e098d70898b7119f6f4af5a6c13343d64b57",
      "project": "demo",
      "branch": "master",
      "change_id": "Idaf5e098d70898b7119f6f4af5a6c13343d64b57",
      "subject": "One change",
      "status": "NEW",
      "created": "2012-07-17 07:18:30.854000000",
      "updated": "2012-07-17 07:19:27.766000000",
      "mergeable": true,
      "insertions": 26,
      "deletions": 10,
      "_number": 1756,
      "owner": {
        "name": "John Doe"
      },
    },
    {
      "id": "demo~master~I09c8041b5867d5b33170316e2abc34b79bbb8501",
      "project": "demo",
      "branch": "master",
      "change_id": "I09c8041b5867d5b33170316e2abc34b79bbb8501",
      "subject": "Another change",
      "status": "NEW",
      "created": "2012-07-17 07:18:30.884000000",
      "updated": "2012-07-17 07:18:30.885000000",
      "mergeable": true,
      "insertions": 12,
      "deletions": 18,
      "_number": 1757,
      "owner": {
        "name": "John Doe"
      },
      "_more_changes": true
    }
  ]

gerrit的網站上有Rest API的指南和文件。指南:https://gerrit-review.googlesource.com/Documentation/dev-rest-api.html,文件:https://gerrit-review.googlesource.com/Documentation/rest-api.html

uwolfer gerrit rest java client

我們要程式設計呼叫gerrit api時發現,光是pojo物件就得寫好多。這樣例行公事的工作當然是交給開源庫來實現最划算了。
比如我們可以使用 uwolfer gerrit rest java client.

引入依賴

大家的後端工程應該是使用maven管理的,我們就先引入maven依賴:

<dependencies>
    <dependency>
        <groupId>com.urswolfer.gerrit.client.rest</groupId>
        <artifactId>gerrit-rest-java-client</artifactId>
        <version>0.8.16</version>
    </dependency>
</dependencies>

鑑權

下一步我們就可以呼叫gerrit-rest-java-client封裝的API來呼叫gerrit rest api了。
首先要鑑權,記得gerrit的設定中有一項是HTTP password麼,這裡就該它發揮作用了。

        GerritRestApiFactory gerritRestApiFactory = new GerritRestApiFactory();
        GerritAuthData.Basic authData = new GerritAuthData.Basic("http://gerrit網址", "使用者名稱", "HTTP password");
        GerritApi gerritApi = gerritRestApiFactory.create(authData);

訪問gerrit資料

鑑權成功之後,我們就可以通過gerritApi為所欲為了。

例1,查詢10個status為merged的change:

            List<ChangeInfo> changes = gerritApi.changes().query("status:merged").withLimit(10).get();

            for (ChangeInfo ci : changes) {
                System.out.println("Change ID:"+ci.changeId);
                System.out.println("Project:"+ci.project);
                System.out.println("Branch:"+ci.branch);
                System.out.println("Subject:"+ci.subject);
                System.out.println("=======================");
            }

執行例:

Change ID:I5c490fba0f109824ae5c5cd91e7222787da9f41d
Project:xxx
Branch:yyy
Subject:zzz

例2,遍歷當前gerrit下都有些什麼工程

            List<ProjectInfo> projects = gerritApi.projects().list().get();

            for (ProjectInfo pi: projects){
                System.out.println(pi.name);
                System.out.println(pi.description);
                System.out.println("~~~~~~~~~~~~~~");
            }

輸出例:

code/device/asus/fugu
null
~~~~~~~~~~~~~~
code/device/asus/fugu-kernel
null
~~~~~~~~~~~~~~
code/device/common
null
~~~~~~~~~~~~~~
code/device/coolpad/common
null
~~~~~~~~~~~~~~

完整程式碼

下面是上面兩例的完整程式碼:
pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>cn.alios.basic.test</groupId>
    <artifactId>TestGerrit3</artifactId>
    <version>1.0.0-SNAPSHOT</version>
<dependencies>
    <dependency>
        <groupId>com.urswolfer.gerrit.client.rest</groupId>
        <artifactId>gerrit-rest-java-client</artifactId>
        <version>0.8.16</version>
    </dependency>
</dependencies>

</project>

Java程式碼:

package cn.alios.basic.tools;

import com.google.gerrit.extensions.api.GerritApi;
import com.google.gerrit.extensions.common.ChangeInfo;
import com.google.gerrit.extensions.common.ProjectInfo;
import com.google.gerrit.extensions.restapi.RestApiException;
import com.urswolfer.gerrit.client.rest.GerritAuthData;
import com.urswolfer.gerrit.client.rest.GerritRestApiFactory;

import java.util.List;

public class TestGerrit {
    public static void main(String[] args) {
        GerritRestApiFactory gerritRestApiFactory = new GerritRestApiFactory();
        GerritAuthData.Basic authData = new GerritAuthData.Basic("http://gerrit.com", "user", "HttpPassword");
        GerritApi gerritApi = gerritRestApiFactory.create(authData);
        try {
            List<ChangeInfo> changes = gerritApi.changes().query("status:merged").withLimit(10).get();

            for (ChangeInfo ci : changes) {
                System.out.println("Change ID:"+ci.changeId);
                System.out.println("Project:"+ci.project);
                System.out.println("Branch:"+ci.branch);
                System.out.println("Subject:"+ci.subject);
                System.out.println("=======================");
            }

            List<ProjectInfo> projects = gerritApi.projects().list().get();

            for (ProjectInfo pi: projects){
                System.out.println(pi.name);
                System.out.println(pi.description);
                System.out.println("~~~~~~~~~~~~~~");
            }

        } catch (RestApiException e) {
            e.printStackTrace();
        }
    }
}

相關文章