Created
August 13, 2020 14:59
-
-
Save mjg123/5df5fd867d00a37c32d56592dc88d682 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.HttpResponse; | |
import java.net.http.HttpResponse.BodyHandlers; | |
import java.net.http.HttpRequest.BodyPublishers; | |
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 static String get(final String path) throws IOException, InterruptedException { | |
var builder = HttpRequest.newBuilder().uri(URI.create(baseUrl + path)); | |
builder.setHeader("Authorization", authorization); | |
builder = builder.GET(); | |
final HttpRequest request = builder.build(); | |
final HttpResponse<String> response = HttpClient.newHttpClient().send(request, BodyHandlers.ofString()); | |
return response.body(); | |
} | |
private static String delete(final String path) throws IOException, InterruptedException { | |
var builder = HttpRequest.newBuilder().uri(URI.create(baseUrl + path)); | |
builder.setHeader("Authorization", authorization); | |
builder = builder.DELETE(); | |
final HttpRequest request = builder.build(); | |
final HttpResponse<String> response = HttpClient.newHttpClient().send(request, BodyHandlers.ofString()); | |
return response.body(); | |
} | |
private static String post(final String path, final String body) throws IOException, InterruptedException { | |
var builder = HttpRequest.newBuilder().uri(URI.create(baseUrl + path)); | |
builder = builder.setHeader("Authorization", authorization); | |
builder = builder.POST(BodyPublishers.ofString(body)); | |
final HttpRequest request = builder.build(); | |
final HttpResponse<String> response = HttpClient.newHttpClient().send(request, BodyHandlers.ofString()); | |
return response.body(); | |
} | |
private static String put(final String path, final String body) throws IOException, InterruptedException { | |
var builder = HttpRequest.newBuilder().uri(URI.create(baseUrl + path)); | |
builder = builder.setHeader("Authorization", authorization); | |
builder = builder.PUT(BodyPublishers.ofString(body)); | |
final HttpRequest request = builder.build(); | |
final HttpResponse<String> response = HttpClient.newHttpClient().send(request, BodyHandlers.ofString()); | |
return response.body(); | |
} | |
private static String getResourceFile(final String filename) throws IOException { | |
final var fileStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(filename); | |
return new String(Objects.requireNonNull(fileStream).readAllBytes(), StandardCharsets.UTF_8); | |
} | |
private static String getMasterBranchSHA() throws IOException, InterruptedException { | |
final String body = get("/git/refs/heads"); | |
var sha = new ObjectMapper().readTree(body) | |
.get(0) | |
.get("object") | |
.get("sha") | |
.asText(); | |
return sha; | |
} | |
private static String createBranch(final String sha) throws IOException, InterruptedException { | |
final Map<String, String> createBranchMap = new HashMap<>(); | |
createBranchMap.put("ref", "refs/heads/new-branch"); | |
createBranchMap.put("sha", sha); | |
final var objectMapper = new ObjectMapper(); | |
final String requestBody = objectMapper.writeValueAsString(createBranchMap); | |
return post("/git/refs", requestBody); | |
} | |
private static String createFile() throws IOException, InterruptedException { | |
final var fileToAdd = getResourceFile("new_file.txt"); | |
final var byteArray = Base64.encodeBase64(fileToAdd.getBytes()); | |
final var encodedString = new String(byteArray); | |
final Map<String, String> createMap = new HashMap<>(); | |
createMap.put("message", "New file added"); | |
createMap.put("content", encodedString); | |
createMap.put("branch", "new-branch"); | |
final var objectMapper = new ObjectMapper(); | |
final String requestBody = objectMapper.writeValueAsString(createMap); | |
return put("/contents/new_file.txt", requestBody); | |
} | |
private static String createPullRequest() throws IOException, InterruptedException { | |
final Map<String, String> createPullRequestMap = new HashMap<>(); | |
createPullRequestMap.put("title", "test-pull-request"); | |
createPullRequestMap.put("head", "new-branch"); | |
createPullRequestMap.put("base", "master"); | |
final var objectMapper = new ObjectMapper(); | |
final String requestBody = objectMapper.writeValueAsString(createPullRequestMap); | |
return post("/pulls", requestBody); | |
} | |
private static String getPullNumber(final String pullRequestResponse) throws JsonProcessingException { | |
final var objectMapper = new ObjectMapper(); | |
return objectMapper.readTree(pullRequestResponse) | |
.get("number") | |
.asText(); | |
} | |
private static String mergePullRequest(final String pullNumber) | |
throws IOException, InterruptedException { | |
final Map<String, String> MERGE_MAP = new HashMap<>(); | |
MERGE_MAP.put("commit_message", "Merging pull request"); | |
final var objectMapper = new ObjectMapper(); | |
final String requestBody = objectMapper.writeValueAsString(MERGE_MAP); | |
final String url = String.format("/pulls/%s/merge", pullNumber); | |
return put(url, requestBody); | |
} | |
private static String deleteBranch() throws IOException, InterruptedException { | |
return delete("/git/refs/heads/new-branch"); | |
} | |
private static void executeExample() throws IOException, InterruptedException { | |
final String masterSHA = getMasterBranchSHA(); | |
createBranch(masterSHA); | |
createFile(); | |
final String pullRequestResponse = createPullRequest(); | |
final String pullNumber = getPullNumber(pullRequestResponse); | |
mergePullRequest(pullNumber); | |
deleteBranch(); | |
} | |
public static void main(final String[] args) throws IOException, InterruptedException { | |
executeExample(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment