使用uwolfer gerrit-rest-java-client獲取Gerrit資訊
使用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();
}
}
}
相關文章
- 【Python】獲取機器使用資訊Python
- SAP ABAP使用CDS獲取系統資訊
- 微信小程式 獲取使用者資訊微信小程式
- 使用Python獲取ECS相關資訊Python
- mac獲取cpu資訊Mac
- 獲取資訊版本1
- 微信小程式獲取使用者資訊方法微信小程式
- 使用PHP獲取影像檔案的EXIF資訊PHP
- 使用 Python 獲取 Linux 系統資訊PythonLinux
- Oracle使用普通變數獲取遊標資訊Oracle變數
- 【RAC】使用crsctl命令獲取Oracle CRS版本資訊Oracle
- .NET Core如何全域性獲取使用者資訊?
- 使用URLConnection物件獲取網路資源資訊物件
- 使用和風天氣介面獲取天氣資訊
- 微博app端使用者資訊獲取方法探究APP
- 連線使用者獲取許可權資訊
- C# url資訊獲取C#
- 常用OS獲取資訊命令
- GetDiskSerial DLL獲取硬碟資訊硬碟
- 根據微信code獲取換取使用者登入態資訊
- Python Web 框架 Django 如何使用jwt獲取使用者資訊PythonWeb框架DjangoJWT
- 微信網頁授權並獲取使用者資訊網頁
- 微信小程式 getUserProfile 獲取使用者資訊微信小程式
- Node.js 微信小程式獲取使用者資訊Node.js微信小程式
- 蘋果iOS有後門 能獲取使用者資訊蘋果iOS
- Oracle使用行型別變數獲取遊標資訊Oracle型別變數
- SAP中關於使用者IP資訊的獲取
- Request.ServerVariables,C#獲取伺服器資訊,C#獲取訪問資訊ServerC#伺服器
- ipmitool獲取伺服器資訊MIT伺服器
- 獲取.crt證書的資訊
- iOS獲取SIM卡資訊iOS
- Android系統資訊獲取Android
- javascript 獲取瀏覽器資訊JavaScript瀏覽器
- 分析索引快速獲取索引資訊索引
- 微信小程式授權登入獲取使用者資訊微信小程式
- 使用 C# 獲取 Kubernetes 叢集資源資訊C#
- jProcesses:使用Java獲取跨平臺程式的詳細資訊Java
- flowable 重構流程編輯器獲取使用者資訊