Skip to content

Commit c6999fc

Browse files
authored
feat: support listing all branches (#178)
Also added convenience method for making a stream of paginated results
1 parent 550c880 commit c6999fc

File tree

5 files changed

+69
-19
lines changed

5 files changed

+69
-19
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*-
2+
* -\-\-
3+
* github-api
4+
* --
5+
* Copyright (C) 2024 Spotify AB
6+
* --
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
* -/-/-
19+
*/
20+
21+
package com.spotify.github.async;
22+
23+
import java.util.stream.Stream;
24+
25+
import static java.util.stream.StreamSupport.stream;
26+
27+
/** Async class to facilitate async operations. */
28+
public class Async {
29+
private Async() {
30+
throw new UnsupportedOperationException("This is a utility class and cannot be instantiated");
31+
}
32+
33+
public static <T> Stream<T> streamFromPaginatingIterable(final Iterable<AsyncPage<T>> iterable) {
34+
return stream(iterable.spliterator(), false)
35+
.flatMap(page -> stream(page.spliterator(), false));
36+
}
37+
}

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -546,15 +546,28 @@ public CompletableFuture<Branch> getBranch(final String branch) {
546546
}
547547

548548
/**
549-
* Get a specific branch.
549+
* List some branches in repository.
550+
* Doesn't return more than 30 branches.
551+
* Use {@link RepositoryClient#listAllBranches} instead to get all branches.
550552
*
551-
* @return list of all branches in repository
553+
* @return list of 30 branches in repository
552554
*/
553555
public CompletableFuture<List<Branch>> listBranches() {
554556
final String path = String.format(LIST_BRANCHES_TEMPLATE, owner, repo);
555557
return github.request(path, LIST_BRANCHES);
556558
}
557559

560+
/**
561+
* List all branches in repository
562+
*
563+
* @return list of all branches in repository
564+
*/
565+
public Iterator<AsyncPage<Branch>> listAllBranches() {
566+
final String path = String.format(LIST_BRANCHES_TEMPLATE, owner, repo);
567+
return new GithubPageIterator<>(new GithubPage<>(github, path, LIST_BRANCHES));
568+
}
569+
570+
558571
/**
559572
* Delete a comment for a given id.
560573
*

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import static java.nio.charset.Charset.defaultCharset;
2929
import static java.util.concurrent.CompletableFuture.completedFuture;
3030
import static java.util.stream.Collectors.toList;
31-
import static java.util.stream.StreamSupport.stream;
3231
import static org.hamcrest.MatcherAssert.assertThat;
3332
import static org.hamcrest.core.Is.is;
3433
import static org.mockito.ArgumentMatchers.anyString;
@@ -38,6 +37,7 @@
3837

3938
import com.google.common.collect.Lists;
4039
import com.google.common.io.Resources;
40+
import com.spotify.github.async.Async;
4141
import com.spotify.github.async.AsyncPage;
4242
import com.spotify.github.jackson.Json;
4343
import com.spotify.github.v3.comment.Comment;
@@ -81,10 +81,7 @@ public void testCommentPaginationSpliterator() throws IOException {
8181
.thenReturn(completedFuture(lastPageResponse));
8282

8383
final Iterable<AsyncPage<Comment>> pageIterator = () -> issueClient.listComments(123);
84-
final List<Comment> listComments =
85-
stream(pageIterator.spliterator(), false)
86-
.flatMap(page -> stream(page.spliterator(), false))
87-
.collect(toList());
84+
final List<Comment> listComments = Async.streamFromPaginatingIterable(pageIterator).collect(toList());
8885

8986
assertThat(listComments.size(), is(30));
9087
assertThat(listComments.get(0).id(), is(1345268));

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

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
import static java.lang.String.format;
3535
import static java.nio.charset.Charset.defaultCharset;
3636
import static java.util.concurrent.CompletableFuture.completedFuture;
37-
import static java.util.stream.StreamSupport.stream;
3837
import static org.hamcrest.MatcherAssert.assertThat;
3938
import static org.hamcrest.core.Is.is;
4039
import static org.junit.jupiter.api.Assertions.assertFalse;
@@ -48,6 +47,7 @@
4847
import com.google.common.collect.ImmutableMap;
4948
import com.google.common.collect.Lists;
5049
import com.google.common.io.Resources;
50+
import com.spotify.github.async.Async;
5151
import com.spotify.github.async.AsyncPage;
5252
import com.spotify.github.jackson.Json;
5353
import com.spotify.github.v3.comment.Comment;
@@ -139,10 +139,7 @@ public void listAuthenticatedUserRepositories() throws Exception {
139139
when(github.request("/user/repos")).thenReturn(completedFuture(pageResponse));
140140

141141
final Iterable<AsyncPage<Repository>> pageIterator = () -> repoClient.listAuthenticatedUserRepositories(ImmutableAuthenticatedUserRepositoriesFilter.builder().build());
142-
final List<Repository> repositories =
143-
stream(pageIterator.spliterator(), false)
144-
.flatMap(page -> stream(page.spliterator(), false))
145-
.collect(Collectors.toList());
142+
final List<Repository> repositories = Async.streamFromPaginatingIterable(pageIterator).collect(Collectors.toList());
146143

147144
assertThat(repositories.get(0).id(), is(1296269));
148145
assertThat(repositories.size(), is(1));
@@ -473,6 +470,17 @@ public void listBranches() throws Exception {
473470
assertThat(branches.size(), is(1));
474471
}
475472

473+
@Test
474+
void listAllBranches() throws Exception {
475+
final String link = "<https://github.com/api/v3/repos/someowner/somerepo/branches>; rel=\"last\"";
476+
final Response response = createMockResponse(link, getFixture( "list_branches.json"));
477+
478+
when(github.request("/repos/someowner/somerepo/branches"))
479+
.thenReturn(completedFuture(response));
480+
final List<Branch> branches = Async.streamFromPaginatingIterable(repoClient::listAllBranches).collect(Collectors.toList());
481+
assertThat(branches.get(0).commit().sha(), is("c5b97d5ae6c19d5c5df71a34c7fbeeda2479ccbc"));
482+
assertThat(branches.size(), is(1));
483+
}
476484

477485
@Test
478486
public void testCommentCreated() throws IOException {

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

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,26 +28,24 @@
2828
import static java.nio.charset.Charset.defaultCharset;
2929
import static java.util.concurrent.CompletableFuture.completedFuture;
3030
import static java.util.stream.Collectors.toList;
31-
import static java.util.stream.StreamSupport.stream;
3231
import static org.hamcrest.MatcherAssert.assertThat;
3332
import static org.hamcrest.core.Is.is;
3433
import static org.mockito.ArgumentMatchers.any;
3534
import static org.mockito.ArgumentMatchers.eq;
3635
import static org.mockito.Mockito.*;
3736

3837
import com.google.common.io.Resources;
38+
import com.spotify.github.async.Async;
3939
import com.spotify.github.async.AsyncPage;
4040
import com.spotify.github.jackson.Json;
4141
import com.spotify.github.v3.Team;
4242
import com.spotify.github.v3.User;
43-
import com.spotify.github.v3.comment.Comment;
4443
import com.spotify.github.v3.orgs.Membership;
4544
import com.spotify.github.v3.orgs.TeamInvitation;
4645
import com.spotify.github.v3.orgs.requests.MembershipCreate;
4746
import com.spotify.github.v3.orgs.requests.TeamCreate;
4847
import com.spotify.github.v3.orgs.requests.TeamUpdate;
4948
import java.io.IOException;
50-
import java.util.Iterator;
5149
import java.util.List;
5250
import java.util.concurrent.CompletableFuture;
5351
import okhttp3.Response;
@@ -184,10 +182,7 @@ public void listTeamMembersPaged() throws Exception {
184182
.thenReturn(completedFuture(lastPageResponse));
185183

186184
final Iterable<AsyncPage<User>> pageIterator = () -> teamClient.listTeamMembers("1", 1);
187-
final List<User> users =
188-
stream(pageIterator.spliterator(), false)
189-
.flatMap(page -> stream(page.spliterator(), false))
190-
.collect(toList());
185+
final List<User> users = Async.streamFromPaginatingIterable(pageIterator).collect(toList());
191186

192187
assertThat(users.size(), is(2));
193188
assertThat(users.get(0).login(), is("octocat"));

0 commit comments

Comments
 (0)