From ff81b3106511d957677d961cb6fde303c9e0a22f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pablo=20Pastor=20Mart=C3=ADn?= Date: Thu, 13 Jul 2023 11:43:46 +0100 Subject: [PATCH] Adding support for getting a PR's patch. This also adds a new request method that accepts extra headers and doesn't try to parse JSON --- .../github/v3/clients/GitHubClient.java | 15 ++++++++ .../github/v3/clients/PullRequestClient.java | 21 +++++++++++ .../v3/clients/PullRequestClientTest.java | 35 +++++++++++++++++++ .../com/spotify/github/v3/clients/patch.txt | 18 ++++++++++ 4 files changed, 89 insertions(+) create mode 100644 src/test/resources/com/spotify/github/v3/clients/patch.txt diff --git a/src/main/java/com/spotify/github/v3/clients/GitHubClient.java b/src/main/java/com/spotify/github/v3/clients/GitHubClient.java index 0bd9283a..43f489d9 100644 --- a/src/main/java/com/spotify/github/v3/clients/GitHubClient.java +++ b/src/main/java/com/spotify/github/v3/clients/GitHubClient.java @@ -391,6 +391,21 @@ CompletableFuture request(final String path) { return call(request); } + /** + * Make an http GET request for the given path on the server + * + * @param path relative to the Github base url + * @param extraHeaders extra github headers to be added to the call + * @return a reader of response body + */ + CompletableFuture request(final String path, final Map extraHeaders) { + final Request.Builder builder = requestBuilder(path); + extraHeaders.forEach(builder::addHeader); + final Request request = builder.build(); + log.debug("Making request to {}", request.url().toString()); + return call(request); + } + /** * Make an http GET request for the given path on the server * diff --git a/src/main/java/com/spotify/github/v3/clients/PullRequestClient.java b/src/main/java/com/spotify/github/v3/clients/PullRequestClient.java index c7c38b95..ccc5dedf 100644 --- a/src/main/java/com/spotify/github/v3/clients/PullRequestClient.java +++ b/src/main/java/com/spotify/github/v3/clients/PullRequestClient.java @@ -25,18 +25,23 @@ import static com.spotify.github.v3.clients.GitHubClient.LIST_PR_TYPE_REFERENCE; import static com.spotify.github.v3.clients.GitHubClient.LIST_REVIEW_REQUEST_TYPE_REFERENCE; import static com.spotify.github.v3.clients.GitHubClient.LIST_REVIEW_TYPE_REFERENCE; +import static java.util.Objects.isNull; import com.google.common.base.Strings; +import com.google.common.collect.ImmutableMap; import com.spotify.github.async.AsyncPage; import com.spotify.github.v3.prs.*; import com.spotify.github.v3.prs.requests.PullRequestCreate; import com.spotify.github.v3.prs.requests.PullRequestParameters; import com.spotify.github.v3.prs.requests.PullRequestUpdate; import com.spotify.github.v3.repos.CommitItem; +import java.io.Reader; import java.lang.invoke.MethodHandles; import java.util.Iterator; import java.util.List; +import java.util.Map; import java.util.concurrent.CompletableFuture; +import javax.ws.rs.core.HttpHeaders; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -227,6 +232,22 @@ public CompletableFuture merge(final int number, final MergeParameters pro return github.put(path, jsonPayload).thenAccept(IGNORE_RESPONSE_CONSUMER); } + public CompletableFuture patch(final int number) { + final String path = String.format(PR_NUMBER_TEMPLATE, owner, repo, number); + final Map extraHeaders = ImmutableMap.of( + HttpHeaders.ACCEPT, "application/vnd.github.patch" + ); + log.debug("Fetching pull request patch from " + path); + return github.request(path, extraHeaders) + .thenApply(response -> { + final var body = response.body(); + if (isNull(body)) { + return Reader.nullReader(); + } + return body.charStream(); + }); + } + private CompletableFuture> list(final String parameterPath) { final String path = String.format(PR_TEMPLATE + parameterPath, owner, repo); log.debug("Fetching pull requests from " + path); diff --git a/src/test/java/com/spotify/github/v3/clients/PullRequestClientTest.java b/src/test/java/com/spotify/github/v3/clients/PullRequestClientTest.java index 0bdbb2b8..acf14443 100644 --- a/src/test/java/com/spotify/github/v3/clients/PullRequestClientTest.java +++ b/src/test/java/com/spotify/github/v3/clients/PullRequestClientTest.java @@ -46,6 +46,7 @@ import com.spotify.github.v3.prs.requests.PullRequestCreate; import com.spotify.github.v3.prs.requests.PullRequestUpdate; import java.io.IOException; +import java.io.Reader; import java.net.URI; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutionException; @@ -57,6 +58,7 @@ import okhttp3.Request; import okhttp3.Response; import okhttp3.ResponseBody; +import org.apache.commons.io.IOUtils; import org.junit.Before; import org.junit.Test; import org.mockito.ArgumentCaptor; @@ -259,4 +261,37 @@ public void testRemoveRequestedReview_failure() throws Throwable { // expecting RequestNotOkException } } + + @Test + public void testGetPatch() throws Throwable { + final Call call = mock(Call.class); + final ArgumentCaptor capture = ArgumentCaptor.forClass(Callback.class); + doNothing().when(call).enqueue(capture.capture()); + + final Response response = + new Response.Builder() + .code(200) + .protocol(Protocol.HTTP_1_1) + .message("OK") + .body( + ResponseBody.create( + MediaType.get("application/vnd.github.patch"), + getFixture("patch.txt"))) + .request(new Request.Builder().url("http://localhost/").build()) + .build(); + + when(client.newCall(any())).thenReturn(call); + + final PullRequestClient pullRequestClient = + PullRequestClient.create(github, "owner", "repo"); + + final CompletableFuture result = + pullRequestClient.patch(1); + + capture.getValue().onResponse(call, response); + + Reader patchReader = result.get(); + + assertEquals(getFixture("patch.txt"), IOUtils.toString(patchReader)); + } } diff --git a/src/test/resources/com/spotify/github/v3/clients/patch.txt b/src/test/resources/com/spotify/github/v3/clients/patch.txt new file mode 100644 index 00000000..aea14f65 --- /dev/null +++ b/src/test/resources/com/spotify/github/v3/clients/patch.txt @@ -0,0 +1,18 @@ +From 81c53612268423500bb086afbf7f6545a97ce181 Thu Jul 17 00:00:00 2000 +From: Spotify +Date: Thu, 17 Jul 2023 22:12:00 -0700 +Subject: [PATCH] Update filename + +--- + filename | 1 + + 1 file changed, 1 insertion(+) + +diff --git a/filename b/filename +index 01a9f34..500bb03 100644 +--- a/nf ++++ b/nf +@@ -1,3 +1,4 @@ + asdf ++asdf +-- +2.39.0 \ No newline at end of file