问题
Is there a way to fetch the number of committed codes through the API for Bitbucket and use it in a program? I dont want to show any message or anything just the number of the committed code by all the programmers in a section
回答1:
I assume by 'comitted codes' you mean 'changesets'. From the bitbucket REST api documentation:
Gets a list of change sets associated with a repository. By default, this call returns the 15 most recent changesets. It also returns the count which is the total number of changesets on the repository. Private repositories require the caller to authenticate.
Use the following url (change 'username' and 'repository' to your own):
https://bitbucket.org/api/1.0/repositories/username/repository/changesets?limit=0
For my test repo this returns:
{"count": 2, "start": null, "limit": 0, "changesets": []}
Count is the total number of changesets. 'limit=0' means you will only get the 'count' back and no individual 'changeset' details.
Edit:
To get commits across all repositories would require some scripting. I used this java program to get all commits for a user across all repositories:
Requires these jars on the classpath
- commons-logging-1.1.3.jar,
- json-simple.1.1.1.jar,
- org.apache.httpcomponents.httpclient_4.3.4.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();
}
}
回答2:
I just extended this script from Atlassian forum for API v2.0 that works on 2021. You can use it from: https://github.com/bc67da8d/bitbucket-commit-counter
来源:https://stackoverflow.com/questions/24393276/how-to-fetch-the-number-of-commits-at-bitbucket-using-its-api