These days, we need to implement a tool to help our team get access to Github service so that we can analysis some data.
When searching github API, we find something interesting and useful.
Github API has developed to 2 main version since now.
- Github API V3
- Github Graphql API V4
/repos/owner:/project:/commits/sha:
public static String getDiffByCommit(String commitSHA,String project) {
String restApi = "repos/bizx/"+project+"/commits/"+commitSHA;
String url = BASEURL+restApi + "?access_token=" + AUTH;
String result = "";
OkHttpClient okHttpClient = new OkHttpClient.Builder().
connectTimeout(10, TimeUnit.SECONDS)
.readTimeout(20, TimeUnit.SECONDS).build();
Request request = new Request.Builder()
.url(url)
.header("Accept","application/vnd.github.VERSION.diff")
.build();
Call call = okHttpClient.newCall(request);
try {
Response response = call.execute();
result = (response.body().string());
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
by set header to
we can get all the diff info from this commit directly.Accept, application/vnd.github.VERSION.diff
public static String getBlameCommitByCodePath(String projectName, String path, int line){
String jsonQuery = String.format("{\n" +
"\"query\":\"query {repository(owner:\\\"bizx\\\", name:\\\"%s\\\") { object(expression: \\\"master\\\"){ ... on Commit { blame(path: \\\"%s\\\"){ ranges{ startingLine endingLine commit{ commitUrl }}}}}}}\"\n" +
"}",projectName, path);
String re = githubGraphqlRequestPost(jsonQuery);
logger.debug("getBlameCommitByCodePath API :"+re);
JsonObject jsonObject = (JsonObject) new JsonParser().parse(re);
JsonArray ranges = jsonObject.getAsJsonObject("data").getAsJsonObject("repository").getAsJsonObject("object").getAsJsonObject("blame").getAsJsonArray("ranges");
int pos = -1;
for( int i = 0; i< ranges.size();i++){
JsonObject range = (JsonObject) ranges.get(i);
int startingLine = range.get("startingLine").getAsInt();
int endingLine = range.get("endingLine").getAsInt();
if(line >= startingLine && line <= endingLine) {
pos = i;
break;
}
}
JsonObject range = (JsonObject) ranges.get(pos);
String[] gitCommitUrl = range.getAsJsonObject("commit").get("commitUrl").getAsString().split("/");
return gitCommitUrl[gitCommitUrl.length-1];
}
The query is like this :
query {
repository(owner:"bizx", name:"{project}") {
object(expression: "master"){
... on Commit {
blame(path: {projectPath}){
ranges{
startingLine
endingLine
commit{
commitUrl
}
}
}
}
}
}
}
public static GitCommitGraphBean getClassCommitsInfo(String projectName,String path,String number){
String jsonQuery = String.format("{\n" +
"\"query\":\"query {repository(owner:\\\"bizx\\\", name:\\\"%s\\\") { ref(qualifiedName: \\\"master\\\"){ target{... on Commit { history(first:%s, path:\\\"%s\\\"){ edges{ node{ message author{ name date } }}}}}}}}\"\n" +
"}",projectName,number,path);
String re = githubGraphqlRequestPost(jsonQuery);
logger.debug("GitCommit Graph API :"+re);
return (GitCommitGraphBean)JsonUtil.stringToObject(re, GitCommitGraphBean.class);
}
The query is like this :
query {
repository(owner:\"bizx\", name:"{project}") {
ref(qualifiedName: "master"){
target{
... on Commit {
history(first:100, path:"{path}"){
edges{
node{
message
author{
name
date
}
}
}
}
}
}
}
}
}