Skip to content

Commit 8735400

Browse files
committed
feat(pull-request): support pagination in list commits (bisect: bad)
Summary ======= This commits introduces a test as a ground base to sustain full pagination when listing commits from a pull request WORK IN PROGRESS
1 parent d2b4785 commit 8735400

File tree

5 files changed

+6156
-1
lines changed

5 files changed

+6156
-1
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import com.google.common.base.Strings;
3333
import com.google.common.collect.ImmutableMap;
3434
import com.spotify.github.async.AsyncPage;
35+
import com.spotify.github.jackson.Json;
3536
import com.spotify.github.v3.git.FileItem;
3637
import com.spotify.github.v3.prs.Comment;
3738
import com.spotify.github.v3.prs.MergeParameters;
@@ -190,7 +191,8 @@ public CompletableFuture<List<CommitItem>> listCommits(final int prNumber) {
190191
public CompletableFuture<List<CommitItem>> listCommits(final long prNumber) {
191192
final String path = String.format(PR_COMMITS_TEMPLATE, owner, repo, prNumber);
192193
log.debug("Fetching pull request commits from " + path);
193-
return github.request(path, LIST_COMMIT_TYPE_REFERENCE);
194+
return github.request(path).thenApply(
195+
response -> Json.create().fromJsonUncheckedNotNull(response.bodyString(), LIST_COMMIT_TYPE_REFERENCE));
194196
}
195197

196198
/**

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

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,14 @@
2121
package com.spotify.github.v3.clients;
2222

2323
import static com.google.common.io.Resources.getResource;
24+
import static com.spotify.github.MockHelper.createMockHttpResponse;
2425
import static com.spotify.github.MockHelper.createMockResponse;
26+
import static com.spotify.github.v3.UserTest.assertUser;
27+
import static com.spotify.github.v3.clients.GitHubClient.LIST_COMMIT_TYPE_REFERENCE;
2528
import static java.lang.String.format;
2629
import static java.nio.charset.Charset.defaultCharset;
2730
import static java.util.concurrent.CompletableFuture.completedFuture;
31+
import static java.util.concurrent.CompletableFuture.completedStage;
2832
import static java.util.stream.Collectors.toList;
2933
import static org.hamcrest.MatcherAssert.assertThat;
3034
import static org.hamcrest.core.Is.is;
@@ -51,11 +55,13 @@
5155
import com.spotify.github.v3.prs.requests.ImmutablePullRequestUpdate;
5256
import com.spotify.github.v3.prs.requests.PullRequestCreate;
5357
import com.spotify.github.v3.prs.requests.PullRequestUpdate;
58+
import com.spotify.github.v3.repos.CommitItem;
5459
import java.io.IOException;
5560
import java.io.Reader;
5661
import java.net.URI;
5762
import java.util.List;
5863
import java.util.concurrent.CompletableFuture;
64+
import java.util.concurrent.CompletionStage;
5965
import java.util.concurrent.ExecutionException;
6066
import okhttp3.Call;
6167
import okhttp3.Callback;
@@ -464,4 +470,40 @@ public void testListComments() throws Throwable {
464470
assertThat(comments.get(0).id(), is(10L));
465471
assertThat(comments.get(0).user().login(), is("octocat"));
466472
}
473+
474+
@Test
475+
public void listCommits() throws Exception {
476+
// Given
477+
final String firstPageLink =
478+
"<https://api.github.com/repositories/10270250/pulls/20463/commits?page=2>; rel=\"next\", <https://api.github.com/repositories/10270250/pulls/20463/commits?page=4>; rel=\"last\"";
479+
final String firstPageBody =
480+
Resources.toString(getResource(this.getClass(), "pull_request_commits_page1.json"), defaultCharset());
481+
final HttpResponse firstPageResponse = createMockResponse(firstPageLink, firstPageBody);
482+
483+
final String secondPageLink =
484+
"<https://api.github.com/repositories/10270250/pulls/20463/commits?page=1>; rel=\"prev\", <https://api.github.com/repositories/10270250/pulls/20463/commits?page=3>; rel=\"next\", <https://api.github.com/repositories/10270250/pulls/20463/commits?page=3>; rel=\"last\", <https://api.github.com/repositories/10270250/pulls/20463/commits?page=1>; rel=\"first\"";
485+
final String secondPageBody =
486+
Resources.toString(getResource(this.getClass(), "pull_request_commits_page2.json"), defaultCharset());
487+
final HttpResponse secondPageResponse = createMockResponse(secondPageLink, secondPageBody);
488+
489+
final String thirdPageLink =
490+
"<https://api.github.com/repositories/10270250/pulls/20463/commits?page=2>; rel=\"prev\", <https://api.github.com/repositories/10270250/pulls/20463/commits?page=1>; rel=\"first\"";
491+
final String thirdPageBody =
492+
Resources.toString(getResource(this.getClass(), "pull_request_commits_page3.json"), defaultCharset());
493+
final HttpResponse thirdPageResponse = createMockResponse(thirdPageLink, thirdPageBody);
494+
495+
when(mockGithub.request("/repos/owner/repo/pulls/1/commits"))
496+
.thenReturn(completedFuture(firstPageResponse));
497+
when(mockGithub.request("/repos/owner/repo/pulls/1/commits?page=1"))
498+
.thenReturn(completedFuture(firstPageResponse));
499+
when(mockGithub.request("/repos/owner/repo/pulls/1/commits?page=2"))
500+
.thenReturn(completedFuture(secondPageResponse));
501+
when(mockGithub.request("/repos/owner/repo/pulls/1/commits?page=3"))
502+
.thenReturn(completedFuture(thirdPageResponse));
503+
504+
final PullRequestClient pullRequestClient = PullRequestClient.create(mockGithub, "owner", "repo");
505+
506+
final List<CommitItem> commits = pullRequestClient.listCommits(1L).get();
507+
assertThat(commits.size(), is(1));
508+
}
467509
}

0 commit comments

Comments
 (0)