Skip to content

Commit 32ac475

Browse files
authored
Adding support for getting a PR's patch. (#139)
This also adds a new request method that accepts extra headers and doesn't try to parse JSON Co-authored-by: Pablo Pastor Martín <[email protected]>
1 parent 18e478c commit 32ac475

File tree

4 files changed

+89
-0
lines changed

4 files changed

+89
-0
lines changed

src/main/java/com/spotify/github/v3/clients/GitHubClient.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,21 @@ CompletableFuture<Response> request(final String path) {
399399
return call(request);
400400
}
401401

402+
/**
403+
* Make an http GET request for the given path on the server
404+
*
405+
* @param path relative to the Github base url
406+
* @param extraHeaders extra github headers to be added to the call
407+
* @return a reader of response body
408+
*/
409+
CompletableFuture<Response> request(final String path, final Map<String, String> extraHeaders) {
410+
final Request.Builder builder = requestBuilder(path);
411+
extraHeaders.forEach(builder::addHeader);
412+
final Request request = builder.build();
413+
log.debug("Making request to {}", request.url().toString());
414+
return call(request);
415+
}
416+
402417
/**
403418
* Make an http GET request for the given path on the server
404419
*

src/main/java/com/spotify/github/v3/clients/PullRequestClient.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,23 @@
2525
import static com.spotify.github.v3.clients.GitHubClient.LIST_PR_TYPE_REFERENCE;
2626
import static com.spotify.github.v3.clients.GitHubClient.LIST_REVIEW_REQUEST_TYPE_REFERENCE;
2727
import static com.spotify.github.v3.clients.GitHubClient.LIST_REVIEW_TYPE_REFERENCE;
28+
import static java.util.Objects.isNull;
2829

2930
import com.google.common.base.Strings;
31+
import com.google.common.collect.ImmutableMap;
3032
import com.spotify.github.async.AsyncPage;
3133
import com.spotify.github.v3.prs.*;
3234
import com.spotify.github.v3.prs.requests.PullRequestCreate;
3335
import com.spotify.github.v3.prs.requests.PullRequestParameters;
3436
import com.spotify.github.v3.prs.requests.PullRequestUpdate;
3537
import com.spotify.github.v3.repos.CommitItem;
38+
import java.io.Reader;
3639
import java.lang.invoke.MethodHandles;
3740
import java.util.Iterator;
3841
import java.util.List;
42+
import java.util.Map;
3943
import java.util.concurrent.CompletableFuture;
44+
import javax.ws.rs.core.HttpHeaders;
4045
import org.slf4j.Logger;
4146
import org.slf4j.LoggerFactory;
4247

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

235+
public CompletableFuture<Reader> patch(final int number) {
236+
final String path = String.format(PR_NUMBER_TEMPLATE, owner, repo, number);
237+
final Map<String, String> extraHeaders = ImmutableMap.of(
238+
HttpHeaders.ACCEPT, "application/vnd.github.patch"
239+
);
240+
log.debug("Fetching pull request patch from " + path);
241+
return github.request(path, extraHeaders)
242+
.thenApply(response -> {
243+
final var body = response.body();
244+
if (isNull(body)) {
245+
return Reader.nullReader();
246+
}
247+
return body.charStream();
248+
});
249+
}
250+
230251
private CompletableFuture<List<PullRequestItem>> list(final String parameterPath) {
231252
final String path = String.format(PR_TEMPLATE + parameterPath, owner, repo);
232253
log.debug("Fetching pull requests from " + path);

src/test/java/com/spotify/github/v3/clients/PullRequestClientTest.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import com.spotify.github.v3.prs.requests.PullRequestCreate;
4747
import com.spotify.github.v3.prs.requests.PullRequestUpdate;
4848
import java.io.IOException;
49+
import java.io.Reader;
4950
import java.net.URI;
5051
import java.util.concurrent.CompletableFuture;
5152
import java.util.concurrent.ExecutionException;
@@ -57,6 +58,7 @@
5758
import okhttp3.Request;
5859
import okhttp3.Response;
5960
import okhttp3.ResponseBody;
61+
import org.apache.commons.io.IOUtils;
6062
import org.junit.Before;
6163
import org.junit.Test;
6264
import org.mockito.ArgumentCaptor;
@@ -259,4 +261,37 @@ public void testRemoveRequestedReview_failure() throws Throwable {
259261
// expecting RequestNotOkException
260262
}
261263
}
264+
265+
@Test
266+
public void testGetPatch() throws Throwable {
267+
final Call call = mock(Call.class);
268+
final ArgumentCaptor<Callback> capture = ArgumentCaptor.forClass(Callback.class);
269+
doNothing().when(call).enqueue(capture.capture());
270+
271+
final Response response =
272+
new Response.Builder()
273+
.code(200)
274+
.protocol(Protocol.HTTP_1_1)
275+
.message("OK")
276+
.body(
277+
ResponseBody.create(
278+
MediaType.get("application/vnd.github.patch"),
279+
getFixture("patch.txt")))
280+
.request(new Request.Builder().url("http://localhost/").build())
281+
.build();
282+
283+
when(client.newCall(any())).thenReturn(call);
284+
285+
final PullRequestClient pullRequestClient =
286+
PullRequestClient.create(github, "owner", "repo");
287+
288+
final CompletableFuture<Reader> result =
289+
pullRequestClient.patch(1);
290+
291+
capture.getValue().onResponse(call, response);
292+
293+
Reader patchReader = result.get();
294+
295+
assertEquals(getFixture("patch.txt"), IOUtils.toString(patchReader));
296+
}
262297
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
From 81c53612268423500bb086afbf7f6545a97ce181 Thu Jul 17 00:00:00 2000
2+
From: Spotify <[email protected]>
3+
Date: Thu, 17 Jul 2023 22:12:00 -0700
4+
Subject: [PATCH] Update filename
5+
6+
---
7+
filename | 1 +
8+
1 file changed, 1 insertion(+)
9+
10+
diff --git a/filename b/filename
11+
index 01a9f34..500bb03 100644
12+
--- a/nf
13+
+++ b/nf
14+
@@ -1,3 +1,4 @@
15+
asdf
16+
+asdf
17+
--
18+
2.39.0

0 commit comments

Comments
 (0)