一、apiurl及身份验证
Github apiurl:https://api.github.com
Github Enterprise Server apiurl:http(s)://hostname/api/v3
二、几个常用API的调用
1.get获取repo、branch
/**
* 获取所有repo
* @param apiurl
* @param accesstoken
* @return
*/
public String getRepos(String apiurl, String accesstoken){
String url = apiurl+"/user/repos";
Map<String,String> headers = new HashMap<>();
headers.put("Content-Type","application/json");
HttpResponse response = HttpRequest.get(url)
.headerMap(headers, false)
.bearerAuth(accesstoken)
.timeout(5 * 60 * 1000)
.execute();
return response.body();
}
/**
* 获取用户信息
* @param apiurl
* @param accesstoken
* @return
*/
public String getUser(String apiurl, String accesstoken){
String url = apiurl+"/user";
Map<String,String> headers = new HashMap<>();
headers.put("Content-Type","application/json");
HttpResponse response = HttpRequest.get(url)
.headerMap(headers, false)
.bearerAuth(accesstoken)
.timeout(5 * 60 * 1000)
.execute();
return response.body();
}
/**
* 获取指定branch
* @param apiurl
* @param accesstoken
* @param username
* @param repo
* @param branch
* @return
*/
public String getBranch(String apiurl, String accesstoken, String username, String repo, String branch){
// /repos/{owner}/{repo}/branches/{branch}
String url = apiurl+"/repos/"+username+"/"+repo+"/branches/"+branch;
Map<String,String> headers = new HashMap<>();
headers.put("Content-Type","application/json");
HttpResponse response = HttpRequest.get(url)
.headerMap(headers, false)
.bearerAuth(accesstoken)
.timeout(5 * 60 * 1000)
.execute();
return response.body();
}
/**
* 获取知道repo
* @param apiurl
* @param accesstoken
* @param username
* @param repo
* @return
*/
public String getRepo(String apiurl, String accesstoken, String username, String repo){
// /repos/{owner}/{repo}/branches/{branch}
String url = apiurl+"/repos/"+username+"/"+repo;
Map<String,String> headers = new HashMap<>();
headers.put("Content-Type","application/json");
HttpResponse response = HttpRequest.get(url)
.headerMap(headers, false)
.bearerAuth(accesstoken)
.timeout(5 * 60 * 1000)
.execute();
return response.body();
}
2.新建Repository
/**
* 新建Repository
*/
public String createRepo(String apiurl, String accesstoken, String name){
String url = apiurl + "/user/repos";
cn.hutool.json.JSONObject json = new cn.hutool.json.JSONObject();
json.set("name",name);
json.set("description","");
json.set("private",false);
json.set("auto_init",true); // 初始化仓库
Map<String,String> headers = new HashMap<>();
headers.put("Content-Type","application/json");
HttpResponse response = HttpRequest.post(url)
.headerMap(headers, false)
.bearerAuth(accesstoken)
.body(String.valueOf(json))
.timeout(5 * 60 * 1000)
.execute();
return response.body();
}
3.新建branch
github api 中没有直接创建branch的接口。
/**
* 新建branch
*/
public String createBranch(String apiurl, String accesstoken, String username, String repo, String branch){
HttpResponse response = HttpRequest.get(apiurl+"/repos/"+username+"/"+repo+"/commits")
.bearerAuth(accesstoken)
.timeout(5 * 60 * 1000)
.execute();
JsonArray commits = new JsonParser().parse(response.body()).getAsJsonArray();
if (commits == null || commits.size() == 0) {
System.out.println("仓库为空");
return null;
}
// 获取到仓库的第一个提交的SHA值
JsonElement firstCommit = commits.get(0);
String sha = firstCommit.getAsJsonObject().get("sha").getAsString();
String ref = "refs/heads/"+branch;
response = HttpRequest.post(apiurl+"/repos/"+username+"/"+repo+"/git/refs")
.bearerAuth(accesstoken) // token
.body("{" +
"\"ref\":\"" + ref + "\"," +
"\"sha\":\"" + sha + "\"" +
"}")
.timeout(5 * 60 * 1000)
.execute();
return response.body();
}
4.上传文件
v3版本,在请求头中添加 application/vnd.github.v3+json。
/**
* 上传文件
*/
public String uploadFile(String apiurl, String accesstoken, String username, String repo, String branch, MultipartFile file) {
// https://api.github.com/repos/{用户名}/{仓库名}/{path}
String url = apiurl+"/repos/"+username+"/"+repo+"/contents/"+file.getOriginalFilename();
Map<String,String> headers = new HashMap<>();
headers.put("Accept","application/vnd.github.v3+json");
headers.put("Content-Type","application/json");
cn.hutool.json.JSONObject json = new cn.hutool.json.JSONObject();
json.set("message","");
json.set("branch",branch);
try {
json.set("content", Base64.getEncoder().encodeToString(file.getBytes()));
} catch (IOException e) {
e.printStackTrace();
}
HttpResponse response = HttpRequest.put(url)
.headerMap(headers, false)
.bearerAuth(accesstoken)
.body(String.valueOf(json))
.timeout(5 * 60 * 1000)
.execute();
return response.body();
}