有没有办法通过Bitbucket的API获取已提交代码的数量并在程序中使用它?我不想在一节
中显示所有程序员提交的任何消息或任何内容答案 0 :(得分:2)
我假设''comitted codes'你的意思是'变更集'。 来自bitbucket REST api documentation:
获取与a关联的更改集列表 库。默认情况下,此调用将返回最近的15个 变更。 它还返回计数,即总数 存储库上的变更集。私有存储库需要调用者 进行身份验证。
使用以下网址(将'username'和'repository'更改为您自己的网址):
https://bitbucket.org/api/1.0/repositories/username/repository/changesets?limit=0
对于我的测试回购,这将返回:
{“count”:2,“start”:null,“limit”:0,“changesets”:[]}
Count是变更集的总数。 'limit = 0'表示您只会返回“计数”而没有单独的“变更集”详细信息。
修改强>
要在所有存储库中提交提交,需要一些脚本。我使用这个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();
}
}
答案 1 :(得分:0)
我刚刚从 Atlassian 论坛为适用于 2021 年的 API v2.0 扩展了这个脚本。 您可以从以下位置使用它:https://github.com/bc67da8d/bitbucket-commit-counter