GitHub API实战(包含Graphql API)

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
                }
              }
            }
          }
        }
      }
    }
  }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值