Skip to content

Commit 0041341

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 92bb6ea commit 0041341

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
@@ -31,6 +31,7 @@
3131
import com.google.common.base.Strings;
3232
import com.google.common.collect.ImmutableMap;
3333
import com.spotify.github.async.AsyncPage;
34+
import com.spotify.github.jackson.Json;
3435
import com.spotify.github.v3.git.FileItem;
3536
import com.spotify.github.v3.prs.Comment;
3637
import com.spotify.github.v3.prs.MergeParameters;
@@ -188,7 +189,8 @@ public CompletableFuture<List<CommitItem>> listCommits(final int prNumber) {
188189
public CompletableFuture<List<CommitItem>> listCommits(final long prNumber) {
189190
final String path = String.format(PR_COMMITS_TEMPLATE, owner, repo, prNumber);
190191
log.debug("Fetching pull request commits from " + path);
191-
return github.request(path, LIST_COMMIT_TYPE_REFERENCE);
192+
return github.request(path).thenApply(
193+
response -> Json.create().fromJsonUncheckedNotNull(response.bodyString(), LIST_COMMIT_TYPE_REFERENCE));
192194
}
193195

194196
/**

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;
@@ -438,4 +444,40 @@ void testChangedFiles() throws IOException {
438444
assertEquals(actualFiles.get(0).filename(), expectedFiles.get(0).filename());
439445
assertEquals(actualFiles.get(1).filename(), expectedFiles.get(1).filename());
440446
}
447+
448+
@Test
449+
public void listCommits() throws Exception {
450+
// Given
451+
final String firstPageLink =
452+
"<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\"";
453+
final String firstPageBody =
454+
Resources.toString(getResource(this.getClass(), "pull_request_commits_page1.json"), defaultCharset());
455+
final HttpResponse firstPageResponse = createMockResponse(firstPageLink, firstPageBody);
456+
457+
final String secondPageLink =
458+
"<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\"";
459+
final String secondPageBody =
460+
Resources.toString(getResource(this.getClass(), "pull_request_commits_page2.json"), defaultCharset());
461+
final HttpResponse secondPageResponse = createMockResponse(secondPageLink, secondPageBody);
462+
463+
final String thirdPageLink =
464+
"<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\"";
465+
final String thirdPageBody =
466+
Resources.toString(getResource(this.getClass(), "pull_request_commits_page3.json"), defaultCharset());
467+
final HttpResponse thirdPageResponse = createMockResponse(thirdPageLink, thirdPageBody);
468+
469+
when(mockGithub.request("/repos/owner/repo/pulls/1/commits"))
470+
.thenReturn(completedFuture(firstPageResponse));
471+
when(mockGithub.request("/repos/owner/repo/pulls/1/commits?page=1"))
472+
.thenReturn(completedFuture(firstPageResponse));
473+
when(mockGithub.request("/repos/owner/repo/pulls/1/commits?page=2"))
474+
.thenReturn(completedFuture(secondPageResponse));
475+
when(mockGithub.request("/repos/owner/repo/pulls/1/commits?page=3"))
476+
.thenReturn(completedFuture(thirdPageResponse));
477+
478+
final PullRequestClient pullRequestClient = PullRequestClient.create(mockGithub, "owner", "repo");
479+
480+
final List<CommitItem> commits = pullRequestClient.listCommits(1L).get();
481+
assertThat(commits.size(), is(1));
482+
}
441483
}

0 commit comments

Comments
 (0)