Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/main/java/com/spotify/github/v3/clients/RepositoryClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand All @@ -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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is an alternative class which we could use com.google.common.net.HttpHeaders

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just copied this from other classes in this project, doesn't matter much I think 🤷

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -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";
Expand Down Expand Up @@ -272,6 +277,22 @@ public CompletableFuture<List<CommitItem>> 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<List<PullRequestItem>> 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/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great addition.

final Map<String, String> extraHeaders =
ImmutableMap.of(HttpHeaders.ACCEPT, "application/vnd.github.groot-preview+json");
return github.request(path, LIST_PR_TYPE_REFERENCE, extraHeaders);
}

/**
* Get a repository commit.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -37,16 +38,19 @@
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;

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;
Expand Down Expand Up @@ -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<List<PullRequestItem>> 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<PullRequestItem> 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<Commit> fixture =
Expand Down