Skip to content

Commit

Permalink
Adding support for getting a PR's patch.
Browse files Browse the repository at this point in the history
This also adds a new request method that accepts extra headers and doesn't try to parse JSON
  • Loading branch information
olbapmar committed Jul 13, 2023
1 parent 18e478c commit c539a95
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/main/java/com/spotify/github/v3/clients/GitHubClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,21 @@ CompletableFuture<Response> 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<Response> request(final String path, final Map<String, String> 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
*
Expand Down
21 changes: 21 additions & 0 deletions src/main/java/com/spotify/github/v3/clients/PullRequestClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -227,6 +232,22 @@ public CompletableFuture<Void> merge(final int number, final MergeParameters pro
return github.put(path, jsonPayload).thenAccept(IGNORE_RESPONSE_CONSUMER);
}

public CompletableFuture<Reader> patch(final int number) {
final String path = String.format(PR_NUMBER_TEMPLATE, owner, repo, number);
final Map<String, String> 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<PullRequestItem>> list(final String parameterPath) {
final String path = String.format(PR_TEMPLATE + parameterPath, owner, repo);
log.debug("Fetching pull requests from " + path);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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<Callback> 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<Reader> result =
pullRequestClient.patch(1);

capture.getValue().onResponse(call, response);

Reader patchReader = result.get();

assertEquals(getFixture("patch.txt"), IOUtils.toString(patchReader));
}
}
18 changes: 18 additions & 0 deletions src/test/resources/com/spotify/github/v3/clients/patch.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
From 81c53612268423500bb086afbf7f6545a97ce181 Thu Jul 17 00:00:00 2000
From: Spotify <office@spotify.com>
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

0 comments on commit c539a95

Please sign in to comment.