有没有办法通过 API for Bitbucket 获取已提交代码的数量并在程序中使用它?我不想显示任何消息或任何内容,只显示一个部分中所有程序员提交的代码的数量
benzo
问问题
7580 次
2 回答
2
我假设“提交的代码”是指“变更集”。从bitbucket REST api 文档:
获取与存储库关联的更改集列表。默认情况下,此调用返回 15 个最近的变更集。它还返回计数,即存储库上变更集的总数。私有存储库需要调用者进行身份验证。
使用以下网址(将“用户名”和“存储库”更改为您自己的):
https://bitbucket.org/api/1.0/repositories/username/repository/changesets?limit=0
对于我的测试回购,这将返回:
{"count": 2, "start": null, "limit": 0, "changesets": []}
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();
}
}
于 2014-06-24T19:38:44.413 回答
0
我刚刚从 Atlassian 论坛为 API v2.0 扩展了这个脚本,该脚本适用于 2021 年。您可以从以下网址使用它:https ://github.com/bc67da8d/bitbucket-commit-counter
于 2021-02-04T15:08:47.497 回答