Skip to content
This repository was archived by the owner on Apr 20, 2026. It is now read-only.
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
37 changes: 37 additions & 0 deletions src/main/java/com/spotify/github/async/Async.java
Original file line number Diff line number Diff line change
@@ -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 <T> Stream<T> streamFromPaginatingIterable(final Iterable<AsyncPage<T>> iterable) {
return stream(iterable.spliterator(), false)
.flatMap(page -> stream(page.spliterator(), false));
}
}
17 changes: 15 additions & 2 deletions src/main/java/com/spotify/github/v3/clients/RepositoryClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -546,15 +546,28 @@ public CompletableFuture<Branch> 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<List<Branch>> 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<AsyncPage<Branch>> 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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -81,10 +81,7 @@ public void testCommentPaginationSpliterator() throws IOException {
.thenReturn(completedFuture(lastPageResponse));

final Iterable<AsyncPage<Comment>> pageIterator = () -> issueClient.listComments(123);
final List<Comment> listComments =
stream(pageIterator.spliterator(), false)
.flatMap(page -> stream(page.spliterator(), false))
.collect(toList());
final List<Comment> listComments = Async.streamFromPaginatingIterable(pageIterator).collect(toList());

assertThat(listComments.size(), is(30));
assertThat(listComments.get(0).id(), is(1345268));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -139,10 +139,7 @@ public void listAuthenticatedUserRepositories() throws Exception {
when(github.request("/user/repos")).thenReturn(completedFuture(pageResponse));

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

assertThat(repositories.get(0).id(), is(1296269));
assertThat(repositories.size(), is(1));
Expand Down Expand Up @@ -473,6 +470,17 @@ public void listBranches() throws Exception {
assertThat(branches.size(), is(1));
}

@Test
void listAllBranches() throws Exception {
final String link = "<https://github.com/api/v3/repos/someowner/somerepo/branches>; rel=\"last\"";
final Response response = createMockResponse(link, getFixture( "list_branches.json"));

when(github.request("/repos/someowner/somerepo/branches"))
.thenReturn(completedFuture(response));
final List<Branch> 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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,26 +28,24 @@
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;
import static org.mockito.ArgumentMatchers.eq;
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;
Expand Down Expand Up @@ -184,10 +182,7 @@ public void listTeamMembersPaged() throws Exception {
.thenReturn(completedFuture(lastPageResponse));

final Iterable<AsyncPage<User>> pageIterator = () -> teamClient.listTeamMembers("1", 1);
final List<User> users =
stream(pageIterator.spliterator(), false)
.flatMap(page -> stream(page.spliterator(), false))
.collect(toList());
final List<User> users = Async.streamFromPaginatingIterable(pageIterator).collect(toList());

assertThat(users.size(), is(2));
assertThat(users.get(0).login(), is("octocat"));
Expand Down