Using uwolfer Gerrit rest java client to get Gerrit information

Keywords: Java REST Maven Google

When using Gerrit to do code management tools, it is inevitable to call Gerrit's API.

Gerrit rest api

Let's take a look at an example and experience the interaction process of the gerrit rest api:

The requests are as follows: changes is an API, the q string must be familiar to gerrit users, and n is the number of restrictions:

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

Example of response information:

  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 has a guide and documentation for the Rest API on its website. Guide: https://gerrit-review.googlesource.com/Documentation/dev-rest-api.html Document: https://gerrit-review.googlesource.com/Documentation/rest-api.html

uwolfer gerrit rest java client

When we want to call gerrit api programmatically, we find that pojo object alone has to write a lot. Such routine work is, of course, the most cost-effective thing to do.
For example, we can use uwolfer gerrit rest java client

Introducing dependency

Your backend project should be managed by maven. We will introduce maven dependency first:

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

authentication

Next, we can call the gerrit rest api encapsulated by Gerrit rest java client.
First of all, authentication. Remember that one of gerrit's settings is HTTP password. It's time for it to work here.

        GerritRestApiFactory gerritRestApiFactory = new GerritRestApiFactory();
        GerritAuthData.Basic authData = new GerritAuthData.Basic("http://gerrit website "," user name "," HTTP password ");
        GerritApi gerritApi = gerritRestApiFactory.create(authData);

Accessing gerrit data

After the authentication is successful, we can do whatever we want through Gerrit API.

For example 1, query 10 change s whose status is merged:

            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("=======================");
            }

Operation example:

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

For example 2, traverse the projects under the current 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("~~~~~~~~~~~~~~");
            }

Output example:

code/device/asus/fugu
null

code/device/asus/fugu-kernel
null

code/device/common
null

code/device/coolpad/common
null

Complete code

Here are the complete codes of the above two examples:
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 code:

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();
        }
    }
}

Posted by Reef on Thu, 05 Dec 2019 19:32:55 -0800