Skip to content

Commit e28a71b

Browse files
Return response from PR create/update (#107)
* Return response from PR create/update * Fix incorrect http method
1 parent d75c5c9 commit e28a71b

File tree

4 files changed

+649
-10
lines changed

4 files changed

+649
-10
lines changed

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,7 @@ CompletableFuture<Response> put(final String path, final String data) {
519519
* @param path relative to the Github base url
520520
* @param data request body as stringified JSON
521521
* @param clazz class to cast response as
522-
* @return response body as String
522+
* @return response body deserialized as provided class
523523
*/
524524
<T> CompletableFuture<T> put(final String path, final String data, final Class<T> clazz) {
525525
return put(path, data)
@@ -543,13 +543,27 @@ CompletableFuture<Response> patch(final String path, final String data) {
543543
return call(request);
544544
}
545545

546+
/**
547+
* Make an http PATCH request for the given path with provided JSON body.
548+
*
549+
* @param path relative to the Github base url
550+
* @param data request body as stringified JSON
551+
* @param clazz class to cast response as
552+
* @return response body deserialized as provided class
553+
*/
554+
<T> CompletableFuture<T> patch(final String path, final String data, final Class<T> clazz) {
555+
return patch(path, data)
556+
.thenApply(
557+
response -> json().fromJsonUncheckedNotNull(responseBodyUnchecked(response), clazz));
558+
}
559+
546560
/**
547561
* Make an http PATCH request for the given path with provided JSON body
548562
*
549563
* @param path relative to the Github base url
550564
* @param data request body as stringified JSON
551565
* @param clazz class to cast response as
552-
* @return response body as String
566+
* @return response body deserialized as provided class
553567
*/
554568
<T> CompletableFuture<T> patch(
555569
final String path,

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

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -102,25 +102,23 @@ public CompletableFuture<PullRequest> get(final int number) {
102102
* Create a pull request.
103103
*
104104
* @param request create request
105+
* @return pull request
105106
*/
106-
public CompletableFuture<Void> create(final PullRequestCreate request) {
107+
public CompletableFuture<PullRequest> create(final PullRequestCreate request) {
107108
final String path = String.format(PR_TEMPLATE, owner, repo);
108-
return github
109-
.post(path, github.json().toJsonUnchecked(request))
110-
.thenAccept(IGNORE_RESPONSE_CONSUMER);
109+
return github.post(path, github.json().toJsonUnchecked(request), PullRequest.class);
111110
}
112111

113112
/**
114113
* Update given pull request.
115114
*
116115
* @param number pull request number
117116
* @param request update request
117+
* @return pull request
118118
*/
119-
public CompletableFuture<Void> update(final int number, final PullRequestUpdate request) {
119+
public CompletableFuture<PullRequest> update(final int number, final PullRequestUpdate request) {
120120
final String path = String.format(PR_NUMBER_TEMPLATE, owner, repo, number);
121-
return github
122-
.patch(path, github.json().toJsonUnchecked(request))
123-
.thenAccept(IGNORE_RESPONSE_CONSUMER);
121+
return github.patch(path, github.json().toJsonUnchecked(request), PullRequest.class);
124122
}
125123

126124
/**

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

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,31 @@
2020

2121
package com.spotify.github.v3.clients;
2222

23+
import static com.google.common.collect.ImmutableMap.of;
2324
import static com.google.common.io.Resources.getResource;
2425
import static java.nio.charset.Charset.defaultCharset;
26+
import static java.util.concurrent.CompletableFuture.completedFuture;
27+
import static org.hamcrest.MatcherAssert.assertThat;
28+
import static org.hamcrest.core.Is.is;
2529
import static org.junit.Assert.assertEquals;
2630
import static org.mockito.ArgumentMatchers.any;
2731
import static org.mockito.Mockito.doNothing;
2832
import static org.mockito.Mockito.mock;
2933
import static org.mockito.Mockito.when;
3034

3135
import com.google.common.collect.ImmutableList;
36+
import com.google.common.collect.ImmutableMap;
3237
import com.google.common.io.Resources;
38+
import com.spotify.github.jackson.Json;
3339
import com.spotify.github.v3.exceptions.RequestNotOkException;
40+
import com.spotify.github.v3.git.Reference;
3441
import com.spotify.github.v3.prs.ImmutableRequestReviewParameters;
42+
import com.spotify.github.v3.prs.PullRequest;
3543
import com.spotify.github.v3.prs.ReviewRequests;
44+
import com.spotify.github.v3.prs.requests.ImmutablePullRequestCreate;
45+
import com.spotify.github.v3.prs.requests.ImmutablePullRequestUpdate;
46+
import com.spotify.github.v3.prs.requests.PullRequestCreate;
47+
import com.spotify.github.v3.prs.requests.PullRequestUpdate;
3648
import java.io.IOException;
3749
import java.net.URI;
3850
import java.util.concurrent.CompletableFuture;
@@ -64,6 +76,88 @@ public void setUp() {
6476
github = GitHubClient.create(client, URI.create("http://bogus"), "token");
6577
}
6678

79+
@Test
80+
public void createPullRequest() throws Exception {
81+
final String title = "Amazing new feature";
82+
final String body = "Please pull these awesome changes in!";
83+
final String head = "octocat:new-topic";
84+
final String base = "master";
85+
86+
final Call call = mock(Call.class);
87+
final ArgumentCaptor<Callback> capture = ArgumentCaptor.forClass(Callback.class);
88+
doNothing().when(call).enqueue(capture.capture());
89+
90+
final Response response =
91+
new Response.Builder()
92+
.code(201)
93+
.protocol(Protocol.HTTP_1_1)
94+
.message("Created")
95+
.body(
96+
ResponseBody.create(
97+
MediaType.get("application/json"),
98+
getFixture("pull_request.json")))
99+
.request(new Request.Builder().url("http://localhost/").build())
100+
.build();
101+
102+
when(client.newCall(any())).thenReturn(call);
103+
104+
final PullRequestClient pullRequestClient =
105+
PullRequestClient.create(github, "owner", "repo");
106+
107+
final PullRequestCreate request = ImmutablePullRequestCreate.builder().title(title).body(body)
108+
.head(head).base(base).build();
109+
110+
final CompletableFuture<PullRequest> result = pullRequestClient.create(request);
111+
112+
capture.getValue().onResponse(call, response);
113+
114+
PullRequest pullRequest = result.get();
115+
116+
assertThat(pullRequest.title(), is(title));
117+
assertThat(pullRequest.body().get(), is(body));
118+
assertThat(pullRequest.head().label().get(), is(head));
119+
assertThat(pullRequest.base().ref(), is(base));
120+
}
121+
122+
@Test
123+
public void updatePullRequest() throws Exception {
124+
final String title = "Amazing new feature";
125+
final String body = "Please pull these awesome changes in!";
126+
127+
final Call call = mock(Call.class);
128+
final ArgumentCaptor<Callback> capture = ArgumentCaptor.forClass(Callback.class);
129+
doNothing().when(call).enqueue(capture.capture());
130+
131+
final Response response =
132+
new Response.Builder()
133+
.code(200)
134+
.protocol(Protocol.HTTP_1_1)
135+
.message("OK")
136+
.body(
137+
ResponseBody.create(
138+
MediaType.get("application/json"),
139+
getFixture("pull_request.json")))
140+
.request(new Request.Builder().url("http://localhost/").build())
141+
.build();
142+
143+
when(client.newCall(any())).thenReturn(call);
144+
145+
final PullRequestClient pullRequestClient =
146+
PullRequestClient.create(github, "owner", "repo");
147+
148+
final PullRequestUpdate request = ImmutablePullRequestUpdate.builder().title(title).body(body)
149+
.build();
150+
151+
final CompletableFuture<PullRequest> result = pullRequestClient.update(1, request);
152+
153+
capture.getValue().onResponse(call, response);
154+
155+
PullRequest pullRequest = result.get();
156+
157+
assertThat(pullRequest.title(), is(title));
158+
assertThat(pullRequest.body().get(), is(body));
159+
}
160+
67161
@Test
68162
public void testListReviewRequests() throws Throwable {
69163
final Call call = mock(Call.class);

0 commit comments

Comments
 (0)