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