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
- 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 towe can get all the diff info from this commit directly.Accept, application/vnd.github.VERSION.diff
2 Git Graphql API V4
- 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 } } } } } }}
- 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 0
- GitHub API实战(包含Graphql API)
- graphql学习(二)Restful API的初步认识
- 安息吧,REST API,GraphQL 长存
- 【第1080期】安息吧 REST API,GraphQL 长存
- 【React Native实战教程】GitHub Trending API数据的获取
- Github API v3 -Overview
- Github API v3-OAuth
- Github API JSON
- github的api调用
- RESTful-github.api 介绍
- GitHub API Push接口
- Windows API (包含了所有Windows API)
- Windows API (包含了所有Windows API)
- Windows API (包含了所有Windows API)
- Windows API (包含了所有Windows API)
- windbg实战寻找加密API(一)
- Android实战--天气预报(API+JSON解析)
- ZooKeeper实战(四)-ZooKeeper API使用
- jsp是如何实现Cookie的
- MySQL总结(二)
- this
- python linecache读取过程
- 机器学习基础1
- GitHub API实战(包含Graphql API)
- Git 如何查看和修改用户名、邮箱?
- POJ-3694 NetWork
- 从键盘上输入3个整型数据
- luoguP3384 树链剖分(链+子树)
- C语言关于数组知识点总结
- 教你如何让VisualStudio的HelpViewer帮助文档独立运行
- python爬取网页信息
- Batting Practice LightOJ