diff --git a/src/main/java/com/spotify/github/async/Async.java b/src/main/java/com/spotify/github/async/Async.java new file mode 100644 index 00000000..b35ef7bb --- /dev/null +++ b/src/main/java/com/spotify/github/async/Async.java @@ -0,0 +1,37 @@ +/*- + * -\-\- + * github-api + * -- + * Copyright (C) 2024 Spotify AB + * -- + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * -/-/- + */ + +package com.spotify.github.async; + +import java.util.stream.Stream; + +import static java.util.stream.StreamSupport.stream; + +/** Async class to facilitate async operations. */ +public class Async { + private Async() { + throw new UnsupportedOperationException("This is a utility class and cannot be instantiated"); + } + + public static Stream streamFromPaginatingIterable(final Iterable> iterable) { + return stream(iterable.spliterator(), false) + .flatMap(page -> stream(page.spliterator(), false)); + } +} 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 f3b63170..f48f0687 100644 --- a/src/main/java/com/spotify/github/v3/clients/RepositoryClient.java +++ b/src/main/java/com/spotify/github/v3/clients/RepositoryClient.java @@ -546,15 +546,28 @@ public CompletableFuture getBranch(final String branch) { } /** - * Get a specific branch. + * List some branches in repository. + * Doesn't return more than 30 branches. + * Use {@link RepositoryClient#listAllBranches} instead to get all branches. * - * @return list of all branches in repository + * @return list of 30 branches in repository */ public CompletableFuture> listBranches() { final String path = String.format(LIST_BRANCHES_TEMPLATE, owner, repo); return github.request(path, LIST_BRANCHES); } + /** + * List all branches in repository + * + * @return list of all branches in repository + */ + public Iterator> listAllBranches() { + final String path = String.format(LIST_BRANCHES_TEMPLATE, owner, repo); + return new GithubPageIterator<>(new GithubPage<>(github, path, LIST_BRANCHES)); + } + + /** * Delete a comment for a given id. * diff --git a/src/test/java/com/spotify/github/v3/clients/IssueClientTest.java b/src/test/java/com/spotify/github/v3/clients/IssueClientTest.java index 8c98235a..dc7d1939 100644 --- a/src/test/java/com/spotify/github/v3/clients/IssueClientTest.java +++ b/src/test/java/com/spotify/github/v3/clients/IssueClientTest.java @@ -28,7 +28,6 @@ import static java.nio.charset.Charset.defaultCharset; import static java.util.concurrent.CompletableFuture.completedFuture; import static java.util.stream.Collectors.toList; -import static java.util.stream.StreamSupport.stream; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.core.Is.is; import static org.mockito.ArgumentMatchers.anyString; @@ -38,6 +37,7 @@ import com.google.common.collect.Lists; import com.google.common.io.Resources; +import com.spotify.github.async.Async; import com.spotify.github.async.AsyncPage; import com.spotify.github.jackson.Json; import com.spotify.github.v3.comment.Comment; @@ -81,10 +81,7 @@ public void testCommentPaginationSpliterator() throws IOException { .thenReturn(completedFuture(lastPageResponse)); final Iterable> pageIterator = () -> issueClient.listComments(123); - final List listComments = - stream(pageIterator.spliterator(), false) - .flatMap(page -> stream(page.spliterator(), false)) - .collect(toList()); + final List listComments = Async.streamFromPaginatingIterable(pageIterator).collect(toList()); assertThat(listComments.size(), is(30)); assertThat(listComments.get(0).id(), is(1345268)); 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 51487050..ab286d07 100644 --- a/src/test/java/com/spotify/github/v3/clients/RepositoryClientTest.java +++ b/src/test/java/com/spotify/github/v3/clients/RepositoryClientTest.java @@ -34,7 +34,6 @@ import static java.lang.String.format; import static java.nio.charset.Charset.defaultCharset; import static java.util.concurrent.CompletableFuture.completedFuture; -import static java.util.stream.StreamSupport.stream; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.core.Is.is; import static org.junit.jupiter.api.Assertions.assertFalse; @@ -48,6 +47,7 @@ import com.google.common.collect.ImmutableMap; import com.google.common.collect.Lists; import com.google.common.io.Resources; +import com.spotify.github.async.Async; import com.spotify.github.async.AsyncPage; import com.spotify.github.jackson.Json; import com.spotify.github.v3.comment.Comment; @@ -139,10 +139,7 @@ public void listAuthenticatedUserRepositories() throws Exception { when(github.request("/user/repos")).thenReturn(completedFuture(pageResponse)); final Iterable> pageIterator = () -> repoClient.listAuthenticatedUserRepositories(ImmutableAuthenticatedUserRepositoriesFilter.builder().build()); - final List repositories = - stream(pageIterator.spliterator(), false) - .flatMap(page -> stream(page.spliterator(), false)) - .collect(Collectors.toList()); + final List repositories = Async.streamFromPaginatingIterable(pageIterator).collect(Collectors.toList()); assertThat(repositories.get(0).id(), is(1296269)); assertThat(repositories.size(), is(1)); @@ -473,6 +470,17 @@ public void listBranches() throws Exception { assertThat(branches.size(), is(1)); } + @Test + void listAllBranches() throws Exception { + final String link = "; rel=\"last\""; + final Response response = createMockResponse(link, getFixture( "list_branches.json")); + + when(github.request("/repos/someowner/somerepo/branches")) + .thenReturn(completedFuture(response)); + final List branches = Async.streamFromPaginatingIterable(repoClient::listAllBranches).collect(Collectors.toList()); + assertThat(branches.get(0).commit().sha(), is("c5b97d5ae6c19d5c5df71a34c7fbeeda2479ccbc")); + assertThat(branches.size(), is(1)); + } @Test public void testCommentCreated() throws IOException { diff --git a/src/test/java/com/spotify/github/v3/clients/TeamClientTest.java b/src/test/java/com/spotify/github/v3/clients/TeamClientTest.java index 9435f1b8..c9240eb9 100644 --- a/src/test/java/com/spotify/github/v3/clients/TeamClientTest.java +++ b/src/test/java/com/spotify/github/v3/clients/TeamClientTest.java @@ -28,7 +28,6 @@ import static java.nio.charset.Charset.defaultCharset; import static java.util.concurrent.CompletableFuture.completedFuture; import static java.util.stream.Collectors.toList; -import static java.util.stream.StreamSupport.stream; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.core.Is.is; import static org.mockito.ArgumentMatchers.any; @@ -36,18 +35,17 @@ import static org.mockito.Mockito.*; import com.google.common.io.Resources; +import com.spotify.github.async.Async; import com.spotify.github.async.AsyncPage; import com.spotify.github.jackson.Json; import com.spotify.github.v3.Team; import com.spotify.github.v3.User; -import com.spotify.github.v3.comment.Comment; import com.spotify.github.v3.orgs.Membership; import com.spotify.github.v3.orgs.TeamInvitation; import com.spotify.github.v3.orgs.requests.MembershipCreate; import com.spotify.github.v3.orgs.requests.TeamCreate; import com.spotify.github.v3.orgs.requests.TeamUpdate; import java.io.IOException; -import java.util.Iterator; import java.util.List; import java.util.concurrent.CompletableFuture; import okhttp3.Response; @@ -184,10 +182,7 @@ public void listTeamMembersPaged() throws Exception { .thenReturn(completedFuture(lastPageResponse)); final Iterable> pageIterator = () -> teamClient.listTeamMembers("1", 1); - final List users = - stream(pageIterator.spliterator(), false) - .flatMap(page -> stream(page.spliterator(), false)) - .collect(toList()); + final List users = Async.streamFromPaginatingIterable(pageIterator).collect(toList()); assertThat(users.size(), is(2)); assertThat(users.get(0).login(), is("octocat"));