GitHub API实战(包含Graphql API)

来源:互联网 发布:淘宝网商贷 编辑:程序博客网 时间:2024/06/18 16:44

GitHub API

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

1 Github API V3

  1. get diff by commit sha
    /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

Accept, application/vnd.github.VERSION.diff

we can get all the diff info from this commit directly.

2 Git Graphql API V4

  1. get code blame by file line
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            }          }        }      }    }  }}
  1. get class commits info (no numbers limited)
    Use V3 to get commits can be limited by the number V3 can return. This API can return with no number limites. In the example below I only get access to the front 100. And if you want to get to more. You need to set the start parameters to the next index and then search. It will then return you the next 100.
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                }              }            }          }        }      }    }  }}