From f3fc0a420d436a2951c76606615c29eb9a6ec0b8 Mon Sep 17 00:00:00 2001 From: Niko Dziemba Date: Fri, 13 May 2022 11:39:57 +0200 Subject: [PATCH] Add RepositoryClient.listPullRequestsForCommit See https://docs.github.com/en/enterprise-server@3.2/rest/commits/commits#list-pull-requests-associated-with-a-commit --- .../github/v3/clients/RepositoryClient.java | 21 +++++++++++++++++++ .../v3/clients/RepositoryClientTest.java | 16 ++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/src/main/java/com/spotify/github/v3/clients/RepositoryClient.java b/src/main/java/com/spotify/github/v3/clients/RepositoryClient.java index 9727d4e9..e1b0e796 100644 --- a/src/main/java/com/spotify/github/v3/clients/RepositoryClient.java +++ b/src/main/java/com/spotify/github/v3/clients/RepositoryClient.java @@ -24,6 +24,7 @@ import static com.spotify.github.v3.clients.GitHubClient.LIST_BRANCHES; import static com.spotify.github.v3.clients.GitHubClient.LIST_COMMIT_TYPE_REFERENCE; import static com.spotify.github.v3.clients.GitHubClient.LIST_FOLDERCONTENT_TYPE_REFERENCE; +import static com.spotify.github.v3.clients.GitHubClient.LIST_PR_TYPE_REFERENCE; import static com.spotify.github.v3.clients.GitHubClient.LIST_REPOSITORY; import static com.spotify.github.v3.clients.GitHubClient.LIST_STATUS_TYPE_REFERENCE; @@ -34,6 +35,7 @@ import com.spotify.github.v3.exceptions.RequestNotOkException; import com.spotify.github.v3.git.Tree; import com.spotify.github.v3.hooks.requests.WebhookCreate; +import com.spotify.github.v3.prs.PullRequestItem; import com.spotify.github.v3.repos.Branch; import com.spotify.github.v3.repos.Commit; import com.spotify.github.v3.repos.CommitComparison; @@ -49,9 +51,11 @@ import java.lang.invoke.MethodHandles; import java.util.Iterator; import java.util.List; +import java.util.Map; import java.util.Optional; import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletionException; +import javax.ws.rs.core.HttpHeaders; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -69,6 +73,7 @@ public class RepositoryClient { public static final String STATUS_URI_TEMPLATE = "/repos/%s/%s/statuses/%s"; private static final String COMMITS_URI_TEMPLATE = "/repos/%s/%s/commits"; private static final String COMMIT_SHA_URI_TEMPLATE = "/repos/%s/%s/commits/%s"; + private static final String COMMIT_PULL_REQUESTS_SHA_URI_TEMPLATE = "/repos/%s/%s/commits/%s/pulls"; private static final String COMMIT_STATUS_URI_TEMPLATE = "/repos/%s/%s/commits/%s/status"; private static final String TREE_SHA_URI_TEMPLATE = "/repos/%s/%s/git/trees/%s"; private static final String COMPARE_COMMIT_TEMPLATE = "/repos/%s/%s/compare/%s...%s"; @@ -272,6 +277,22 @@ public CompletableFuture> listCommits() { return github.request(path, LIST_COMMIT_TYPE_REFERENCE); } + /** + * List pull requests that contain the given commit. + * + * @param sha commit sha + * @return pull requests + */ + public CompletableFuture> listPullRequestsForCommit(final String sha) { + final String path = String.format(COMMIT_PULL_REQUESTS_SHA_URI_TEMPLATE, owner, repo, sha); + + // As of GHE 3.2, this feature is still in preview, so we need to add the extra header. + // https://developer.github.com/changes/2019-04-11-pulls-branches-for-commit/ + final Map extraHeaders = + ImmutableMap.of(HttpHeaders.ACCEPT, "application/vnd.github.groot-preview+json"); + return github.request(path, LIST_PR_TYPE_REFERENCE, extraHeaders); + } + /** * Get a repository commit. * diff --git a/src/test/java/com/spotify/github/v3/clients/RepositoryClientTest.java b/src/test/java/com/spotify/github/v3/clients/RepositoryClientTest.java index d92a8169..2efb76ce 100644 --- a/src/test/java/com/spotify/github/v3/clients/RepositoryClientTest.java +++ b/src/test/java/com/spotify/github/v3/clients/RepositoryClientTest.java @@ -26,6 +26,7 @@ import static com.spotify.github.v3.clients.GitHubClient.LIST_COMMIT_TYPE_REFERENCE; import static com.spotify.github.v3.clients.GitHubClient.LIST_BRANCHES; import static com.spotify.github.v3.clients.GitHubClient.LIST_FOLDERCONTENT_TYPE_REFERENCE; +import static com.spotify.github.v3.clients.GitHubClient.LIST_PR_TYPE_REFERENCE; import static com.spotify.github.v3.clients.GitHubClient.LIST_REPOSITORY; import static com.spotify.github.v3.clients.MockHelper.createMockResponse; import static com.spotify.github.v3.clients.RepositoryClient.STATUS_URI_TEMPLATE; @@ -37,6 +38,7 @@ import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import static java.util.stream.StreamSupport.stream; @@ -44,9 +46,11 @@ import com.google.common.collect.ImmutableMap; import com.google.common.collect.Lists; import com.google.common.io.Resources; +import com.google.common.net.HttpHeaders; import com.spotify.github.async.AsyncPage; import com.spotify.github.jackson.Json; import com.spotify.github.v3.comment.Comment; +import com.spotify.github.v3.prs.PullRequestItem; import com.spotify.github.v3.repos.Branch; import com.spotify.github.v3.repos.Commit; import com.spotify.github.v3.repos.CommitComparison; @@ -171,6 +175,18 @@ public void listCommits() throws Exception { commits.get(0).commit().tree().sha(), is("6dcb09b5b57875f334f61aebed695e2e4193db5e")); } + @Test + public void listPullRequestsForCommit() throws Exception { + final CompletableFuture> fixture = + completedFuture( + json.fromJson("[" + getFixture("../prs/pull_request_item.json") + "]", LIST_PR_TYPE_REFERENCE)); + when(github.request(eq("/repos/someowner/somerepo/commits/thesha/pulls"), eq(LIST_PR_TYPE_REFERENCE), any())) + .thenReturn(fixture); + final List prs = repoClient.listPullRequestsForCommit("thesha").get(); + assertThat(prs.size(), is(1)); + assertThat(prs.get(0).number(), is(1347)); + } + @Test public void getCommit() throws Exception { final CompletableFuture fixture =