Skip to content

Instantly share code, notes, and snippets.

@mjg123
Created August 13, 2020 15:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mjg123/20b69f2edfcbfaa4acaef0def3aa816a to your computer and use it in GitHub Desktop.
Save mjg123/20b69f2edfcbfaa4acaef0def3aa816a to your computer and use it in GitHub Desktop.
GH API extra changes
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.codec.binary.Base64;
import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpRequest.BodyPublishers;
import java.net.http.HttpResponse.BodyHandlers;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
public class GitHubAPITest {
private static final String authorization = "Bearer 82de106cc3feb660b5eb134ed25a652e1c2fe92f";
private static final String baseUrl = "https://api.github.com/repos/mjg123/GH_API_TEST";
private static final ObjectMapper objectMapper = new ObjectMapper();
private String get(String path) throws IOException, InterruptedException {
var request = HttpRequest.newBuilder().uri(URI.create(baseUrl + path))
.setHeader("Authorization", authorization)
.GET()
.build();
var response = HttpClient.newHttpClient().send(request, BodyHandlers.ofString());
return response.body();
}
private String delete(String path) throws IOException, InterruptedException {
var request = HttpRequest.newBuilder().uri(URI.create(baseUrl + path))
.setHeader("Authorization", authorization)
.DELETE()
.build();
var response = HttpClient.newHttpClient().send(request, BodyHandlers.ofString());
return response.body();
}
private String post(String path, String body) throws IOException, InterruptedException {
var request = HttpRequest.newBuilder().uri(URI.create(baseUrl + path))
.setHeader("Authorization", authorization)
.POST(BodyPublishers.ofString(body))
.build();
var response = HttpClient.newHttpClient().send(request, BodyHandlers.ofString());
return response.body();
}
private String put(String path, String body) throws IOException, InterruptedException {
var request = HttpRequest.newBuilder().uri(URI.create(baseUrl + path))
.setHeader("Authorization", authorization)
.PUT(BodyPublishers.ofString(body))
.build();
var response = HttpClient.newHttpClient().send(request, BodyHandlers.ofString());
return response.body();
}
private String getResourceFile(String filename) throws IOException {
var fileStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(filename);
return new String(Objects.requireNonNull(fileStream).readAllBytes(), StandardCharsets.UTF_8);
}
private String getMasterBranchSHA() throws IOException, InterruptedException {
var body = get("/git/refs/heads");
var sha = objectMapper.readTree(body)
.get(0)
.get("object")
.get("sha")
.asText();
return sha;
}
private String createBranch(String sha) throws IOException, InterruptedException {
var createBranchMap = Map.of(
"ref", "refs/heads/new-branch",
"sha", sha);
var requestBody = objectMapper.writeValueAsString(createBranchMap);
return post("/git/refs", requestBody);
}
private String createFile() throws IOException, InterruptedException {
var fileToAdd = getResourceFile("new_file.txt");
var byteArray = Base64.encodeBase64(fileToAdd.getBytes());
var encodedString = new String(byteArray);
var createMap = Map.of(
"message", "New file added",
"content", encodedString,
"branch", "new-branch");
var requestBody = objectMapper.writeValueAsString(createMap);
return put("/contents/new_file.txt", requestBody);
}
private String createPullRequest() throws IOException, InterruptedException {
var createPullRequestMap = Map.of(
"title", "test-pull-request",
"head", "new-branch",
"base", "master");
var requestBody = objectMapper.writeValueAsString(createPullRequestMap);
return post("/pulls", requestBody);
}
private String getPullNumber(String pullRequestResponse) throws JsonProcessingException {
return objectMapper.readTree(pullRequestResponse)
.get("number")
.asText();
}
private String mergePullRequest(String pullNumber) throws IOException, InterruptedException {
var mergeMap = Map.of(
"commit_message", "Merging pull request");
var requestBody = objectMapper.writeValueAsString(mergeMap);
var url = String.format("/pulls/%s/merge", pullNumber);
return put(url, requestBody);
}
private String deleteBranch() throws IOException, InterruptedException {
return delete("/git/refs/heads/new-branch");
}
private void executeExample() throws IOException, InterruptedException {
var masterSHA = getMasterBranchSHA();
createBranch(masterSHA);
createFile();
var pullRequestResponse = createPullRequest();
var pullNumber = getPullNumber(pullRequestResponse);
mergePullRequest(pullNumber);
deleteBranch();
}
public static void main(String[] args) throws IOException, InterruptedException {
new GitHubAPITest().executeExample();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment