Bitbucket の API を介してコミットされたコードの数を取得し、プログラムで使用する方法はありますか? メッセージや、セクション内のすべてのプログラマーによってコミットされたコードの数だけを表示したくありません
2 に答える
「コミットされたコード」とは、「変更セット」を意味すると思います。bitbucket REST APIのドキュメントから:
リポジトリに関連付けられた変更セットのリストを取得します。デフォルトでは、この呼び出しは最新の 15 個の変更セットを返します。また、リポジトリ上の変更セットの総数であるカウントも返します。プライベート リポジトリでは、呼び出し元が認証を受ける必要があります。
次の URL を使用します (「ユーザー名」と「リポジトリ」を独自のものに変更します)。
https://bitbucket.org/api/1.0/repositories/username/repository/changesets?limit=0
私のテストレポでは、これは次を返します:
{"カウント": 2、"開始": null、"制限": 0、"変更セット": []}
Count は変更セットの総数です。'limit=0' は、'count' のみが返され、個々の 'changeset' の詳細は返されないことを意味します。
編集:
すべてのリポジトリでコミットを取得するには、いくつかのスクリプトが必要になります。この Java プログラムを使用して、すべてのリポジトリでユーザーのすべてのコミットを取得しました。
クラスパスにこれらの jar が必要です
-
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashSet;
import java.util.Set;
import org.apache.commons.codec.binary.Base64;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
public class BitBucket {
public static void main(String[] args) throws Exception {
String username = "YOUR_USERNAME";
String password = "YOUR_PASSWORD";
String url = "https://bitbucket.org/api/2.0/repositories/"+username;
HttpClient client = new DefaultHttpClient();
JSONParser parser = new JSONParser();
Object obj = parser.parse(processRequest(url, username, password, client));
JSONObject jsonObject = (JSONObject) obj;
JSONArray array = (JSONArray) jsonObject.get("values");
Set<String> repoNames = new HashSet<>();
for(int i = 0; i < array.size(); i++){
repoNames.add(((JSONObject) array.get(i)).get("name").toString());
}
long commitCount = 0;
for(String repoName : repoNames){
String repoUrl = "https://bitbucket.org/api/1.0/repositories/"+username + "/" + repoName.toLowerCase() + "/changesets?limit=0";
Object commitobj = parser.parse(processRequest(repoUrl, username, password, client));
commitCount += (Long) ((JSONObject) commitobj).get("count");
}
System.out.println("Total Commit Count across "+repoNames.size() +" repos for user "+username+" = " + commitCount);
}
private static String getBasicAuthenticationEncoding(String username, String password) {
String userPassword = username + ":" + password;
return new String(Base64.encodeBase64(userPassword.getBytes()));
}
public static String processRequest(String url, String username, String password, HttpClient client) throws ClientProtocolException, IOException{
HttpGet request = new HttpGet(url);
request.addHeader("Authorization", "Basic " + getBasicAuthenticationEncoding(username, password));
HttpResponse response = client.execute(request);
System.out.println("\nSending 'GET' request to URL : " + url);
System.out.println("Response Code : " +
response.getStatusLine().getStatusCode());
BufferedReader rd = new BufferedReader(
new InputStreamReader(response.getEntity().getContent()));
StringBuffer result = new StringBuffer();
String line = "";
while ((line = rd.readLine()) != null) {
result.append(line);
}
return result.toString();
}
}