Skip to content

Commit bf30ce0

Browse files
authored
1 parent e19601c commit bf30ce0

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import static com.spotify.github.v3.clients.GitHubClient.LIST_BRANCHES;
2525
import static com.spotify.github.v3.clients.GitHubClient.LIST_COMMIT_TYPE_REFERENCE;
2626
import static com.spotify.github.v3.clients.GitHubClient.LIST_FOLDERCONTENT_TYPE_REFERENCE;
27+
import static com.spotify.github.v3.clients.GitHubClient.LIST_PR_TYPE_REFERENCE;
2728
import static com.spotify.github.v3.clients.GitHubClient.LIST_REPOSITORY;
2829
import static com.spotify.github.v3.clients.GitHubClient.LIST_STATUS_TYPE_REFERENCE;
2930

@@ -34,6 +35,7 @@
3435
import com.spotify.github.v3.exceptions.RequestNotOkException;
3536
import com.spotify.github.v3.git.Tree;
3637
import com.spotify.github.v3.hooks.requests.WebhookCreate;
38+
import com.spotify.github.v3.prs.PullRequestItem;
3739
import com.spotify.github.v3.repos.Branch;
3840
import com.spotify.github.v3.repos.Commit;
3941
import com.spotify.github.v3.repos.CommitComparison;
@@ -49,9 +51,11 @@
4951
import java.lang.invoke.MethodHandles;
5052
import java.util.Iterator;
5153
import java.util.List;
54+
import java.util.Map;
5255
import java.util.Optional;
5356
import java.util.concurrent.CompletableFuture;
5457
import java.util.concurrent.CompletionException;
58+
import javax.ws.rs.core.HttpHeaders;
5559
import org.slf4j.Logger;
5660
import org.slf4j.LoggerFactory;
5761

@@ -69,6 +73,7 @@ public class RepositoryClient {
6973
public static final String STATUS_URI_TEMPLATE = "/repos/%s/%s/statuses/%s";
7074
private static final String COMMITS_URI_TEMPLATE = "/repos/%s/%s/commits";
7175
private static final String COMMIT_SHA_URI_TEMPLATE = "/repos/%s/%s/commits/%s";
76+
private static final String COMMIT_PULL_REQUESTS_SHA_URI_TEMPLATE = "/repos/%s/%s/commits/%s/pulls";
7277
private static final String COMMIT_STATUS_URI_TEMPLATE = "/repos/%s/%s/commits/%s/status";
7378
private static final String TREE_SHA_URI_TEMPLATE = "/repos/%s/%s/git/trees/%s";
7479
private static final String COMPARE_COMMIT_TEMPLATE = "/repos/%s/%s/compare/%s...%s";
@@ -272,6 +277,22 @@ public CompletableFuture<List<CommitItem>> listCommits() {
272277
return github.request(path, LIST_COMMIT_TYPE_REFERENCE);
273278
}
274279

280+
/**
281+
* List pull requests that contain the given commit.
282+
*
283+
* @param sha commit sha
284+
* @return pull requests
285+
*/
286+
public CompletableFuture<List<PullRequestItem>> listPullRequestsForCommit(final String sha) {
287+
final String path = String.format(COMMIT_PULL_REQUESTS_SHA_URI_TEMPLATE, owner, repo, sha);
288+
289+
// As of GHE 3.2, this feature is still in preview, so we need to add the extra header.
290+
// https://developer.github.com/changes/2019-04-11-pulls-branches-for-commit/
291+
final Map<String, String> extraHeaders =
292+
ImmutableMap.of(HttpHeaders.ACCEPT, "application/vnd.github.groot-preview+json");
293+
return github.request(path, LIST_PR_TYPE_REFERENCE, extraHeaders);
294+
}
295+
275296
/**
276297
* Get a repository commit.
277298
*

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import static com.spotify.github.v3.clients.GitHubClient.LIST_COMMIT_TYPE_REFERENCE;
2727
import static com.spotify.github.v3.clients.GitHubClient.LIST_BRANCHES;
2828
import static com.spotify.github.v3.clients.GitHubClient.LIST_FOLDERCONTENT_TYPE_REFERENCE;
29+
import static com.spotify.github.v3.clients.GitHubClient.LIST_PR_TYPE_REFERENCE;
2930
import static com.spotify.github.v3.clients.GitHubClient.LIST_REPOSITORY;
3031
import static com.spotify.github.v3.clients.MockHelper.createMockResponse;
3132
import static com.spotify.github.v3.clients.RepositoryClient.STATUS_URI_TEMPLATE;
@@ -37,16 +38,19 @@
3738
import static org.junit.Assert.assertFalse;
3839
import static org.junit.Assert.assertTrue;
3940
import static org.mockito.ArgumentMatchers.any;
41+
import static org.mockito.ArgumentMatchers.eq;
4042
import static org.mockito.Mockito.mock;
4143
import static org.mockito.Mockito.when;
4244
import static java.util.stream.StreamSupport.stream;
4345

4446
import com.google.common.collect.ImmutableMap;
4547
import com.google.common.collect.Lists;
4648
import com.google.common.io.Resources;
49+
import com.google.common.net.HttpHeaders;
4750
import com.spotify.github.async.AsyncPage;
4851
import com.spotify.github.jackson.Json;
4952
import com.spotify.github.v3.comment.Comment;
53+
import com.spotify.github.v3.prs.PullRequestItem;
5054
import com.spotify.github.v3.repos.Branch;
5155
import com.spotify.github.v3.repos.Commit;
5256
import com.spotify.github.v3.repos.CommitComparison;
@@ -171,6 +175,18 @@ public void listCommits() throws Exception {
171175
commits.get(0).commit().tree().sha(), is("6dcb09b5b57875f334f61aebed695e2e4193db5e"));
172176
}
173177

178+
@Test
179+
public void listPullRequestsForCommit() throws Exception {
180+
final CompletableFuture<List<PullRequestItem>> fixture =
181+
completedFuture(
182+
json.fromJson("[" + getFixture("../prs/pull_request_item.json") + "]", LIST_PR_TYPE_REFERENCE));
183+
when(github.request(eq("/repos/someowner/somerepo/commits/thesha/pulls"), eq(LIST_PR_TYPE_REFERENCE), any()))
184+
.thenReturn(fixture);
185+
final List<PullRequestItem> prs = repoClient.listPullRequestsForCommit("thesha").get();
186+
assertThat(prs.size(), is(1));
187+
assertThat(prs.get(0).number(), is(1347));
188+
}
189+
174190
@Test
175191
public void getCommit() throws Exception {
176192
final CompletableFuture<Commit> fixture =

0 commit comments

Comments
 (0)