From 052902fb496243f3253f5de4b15b8efedd4fadc3 Mon Sep 17 00:00:00 2001 From: Kohsuke Kawaguchi Date: Sat, 14 Feb 2015 06:40:42 -0800 Subject: [PATCH] Merge pull request #146 --- src/main/java/org/kohsuke/github/GHTeam.java | 37 ++++++++----------- .../org/kohsuke/github/PagedIterable.java | 13 +++++++ src/test/java/org/kohsuke/github/AppTest.java | 10 +++++ 3 files changed, 38 insertions(+), 22 deletions(-) diff --git a/src/main/java/org/kohsuke/github/GHTeam.java b/src/main/java/org/kohsuke/github/GHTeam.java index acdc711cfb..4ec4c7cf87 100644 --- a/src/main/java/org/kohsuke/github/GHTeam.java +++ b/src/main/java/org/kohsuke/github/GHTeam.java @@ -1,15 +1,10 @@ package org.kohsuke.github; import java.io.IOException; -import java.util.Arrays; import java.util.Collections; -import java.util.HashSet; -import java.util.Iterator; -import java.util.LinkedHashSet; import java.util.Map; import java.util.Set; import java.util.TreeMap; -import java.util.TreeSet; /** * A team in GitHub organization. @@ -55,23 +50,21 @@ public int getId() { /** * Retrieves the current members. */ - public Set getMembers() throws IOException { - - Set members = new LinkedHashSet(); - - Iterator pageIterator = org.root.retrieve().asIterator(api("/members"), - GHUser[].class); - while (pageIterator != null && pageIterator.hasNext()) { - GHUser[] users = pageIterator.next(); - if (users != null) { - GHUser.wrap(users, org.root); - for (GHUser user : users) { - members.add(user); - } - } - } - - return Collections.unmodifiableSet(members); + public PagedIterable listMembers() throws IOException { + return new PagedIterable() { + public PagedIterator iterator() { + return new PagedIterator(org.root.retrieve().asIterator(api("/members"), GHUser[].class)) { + @Override + protected void wrapUp(GHUser[] page) { + GHUser.wrap(page, org.root); + } + }; + } + }; + } + + public Set getMembers() throws IOException { + return Collections.unmodifiableSet(listMembers().asSet()); } /** diff --git a/src/main/java/org/kohsuke/github/PagedIterable.java b/src/main/java/org/kohsuke/github/PagedIterable.java index 7cbfe6dc3d..8085e1ed53 100644 --- a/src/main/java/org/kohsuke/github/PagedIterable.java +++ b/src/main/java/org/kohsuke/github/PagedIterable.java @@ -1,7 +1,9 @@ package org.kohsuke.github; import java.util.ArrayList; +import java.util.LinkedHashSet; import java.util.List; +import java.util.Set; /** * {@link Iterable} that returns {@link PagedIterator} @@ -21,4 +23,15 @@ public List asList() { } return r; } + + /** + * Eagerly walk {@link Iterable} and return the result in a set. + */ + public Set asSet() { + LinkedHashSet r = new LinkedHashSet(); + for(PagedIterator i = iterator(); i.hasNext();) { + r.addAll(i.nextPage()); + } + return r; + } } diff --git a/src/test/java/org/kohsuke/github/AppTest.java b/src/test/java/org/kohsuke/github/AppTest.java index a92b40242f..0a86b330d6 100644 --- a/src/test/java/org/kohsuke/github/AppTest.java +++ b/src/test/java/org/kohsuke/github/AppTest.java @@ -607,6 +607,16 @@ public void testCommitStatusContext() throws IOException { } + @Test + public void testMemberPagenation() throws IOException { + Set all = new HashSet(); + for (GHUser u : gitHub.getOrganization("github-api-test-org").getTeamByName("Core Developers").listMembers()) { + System.out.println(u.getLogin()); + all.add(u); + } + assertFalse(all.isEmpty()); + } + private void kohsuke() { String login = getUser().getLogin(); Assume.assumeTrue(login.equals("kohsuke") || login.equals("kohsuke2"));