Skip to content

Commit eef048f

Browse files
authored
feat: add diff method PullRequestClient (#149)
* Add test method for PullRequestClient * fix test
1 parent 1ffd719 commit eef048f

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,22 @@ public CompletableFuture<Reader> patch(final int number) {
248248
});
249249
}
250250

251+
public CompletableFuture<Reader> diff(final int number) {
252+
final String path = String.format(PR_NUMBER_TEMPLATE, owner, repo, number);
253+
final Map<String, String> extraHeaders = ImmutableMap.of(
254+
HttpHeaders.ACCEPT, "application/vnd.github.diff"
255+
);
256+
log.debug("Fetching pull diff from " + path);
257+
return github.request(path, extraHeaders)
258+
.thenApply(response -> {
259+
final var body = response.body();
260+
if (isNull(body)) {
261+
return Reader.nullReader();
262+
}
263+
return body.charStream();
264+
});
265+
}
266+
251267
private CompletableFuture<List<PullRequestItem>> list(final String parameterPath) {
252268
final String path = String.format(PR_TEMPLATE + parameterPath, owner, repo);
253269
log.debug("Fetching pull requests from " + path);

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,4 +294,37 @@ public void testGetPatch() throws Throwable {
294294

295295
assertEquals(getFixture("patch.txt"), IOUtils.toString(patchReader));
296296
}
297+
298+
@Test
299+
public void testGetDiff() throws Throwable {
300+
final Call call = mock(Call.class);
301+
final ArgumentCaptor<Callback> capture = ArgumentCaptor.forClass(Callback.class);
302+
doNothing().when(call).enqueue(capture.capture());
303+
304+
final Response response =
305+
new Response.Builder()
306+
.code(200)
307+
.protocol(Protocol.HTTP_1_1)
308+
.message("OK")
309+
.body(
310+
ResponseBody.create(
311+
MediaType.get("application/vnd.github.diff"),
312+
getFixture("diff.txt")))
313+
.request(new Request.Builder().url("http://localhost/").build())
314+
.build();
315+
316+
when(client.newCall(any())).thenReturn(call);
317+
318+
final PullRequestClient pullRequestClient =
319+
PullRequestClient.create(github, "owner", "repo");
320+
321+
final CompletableFuture<Reader> result =
322+
pullRequestClient.diff(1);
323+
324+
capture.getValue().onResponse(call, response);
325+
326+
Reader diffReader = result.get();
327+
328+
assertEquals(getFixture("diff.txt"), IOUtils.toString(diffReader));
329+
}
297330
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
diff --git a/filename b/filename
2+
index 01a9f34..500bb03 100644
3+
--- a/nf
4+
+++ b/nf
5+
@@ -1,3 +1,4 @@
6+
asdf
7+
+asdf
8+
--
9+
2.39.0

0 commit comments

Comments
 (0)