diff --git a/src/main/java/org/kohsuke/github/GHOrganization.java b/src/main/java/org/kohsuke/github/GHOrganization.java index f15626370c..884e165b32 100644 --- a/src/main/java/org/kohsuke/github/GHOrganization.java +++ b/src/main/java/org/kohsuke/github/GHOrganization.java @@ -129,7 +129,7 @@ public List getRepositoriesWithOpenPullRequests() throws IOExcepti public List getPullRequests() throws IOException { List all = new ArrayList(); for (GHRepository r : getRepositoriesWithOpenPullRequests()) { - all.addAll(r.getPullRequests(GHIssueState.OPEN)); + all.addAll(r.getPullRequests(GHIssueState.OPEN).iterator().asList()); } return all; } diff --git a/src/main/java/org/kohsuke/github/GHRepository.java b/src/main/java/org/kohsuke/github/GHRepository.java index 6ff0553580..811209656c 100644 --- a/src/main/java/org/kohsuke/github/GHRepository.java +++ b/src/main/java/org/kohsuke/github/GHRepository.java @@ -359,11 +359,18 @@ public GHPullRequest getPullRequest(int i) throws IOException { /** * Retrieves all the pull requests of a particular state. */ - public List getPullRequests(GHIssueState state) throws IOException { - GHPullRequest[] r = root.retrieveWithAuth("/repos/" + owner.login + '/' + name + "/pulls?state=" + state.name().toLowerCase(Locale.ENGLISH), GHPullRequest[].class); - for (GHPullRequest p : r) - p.wrapUp(this); - return new ArrayList(Arrays.asList(r)); + public PagedIterable getPullRequests(final GHIssueState state) { + return new PagedIterable() { + public PagedIterator iterator() { + return new PagedIterator(root.retrievePaged(String.format("/repos/%s/%s/pulls?state=%s", owner.login,name,state.name().toLowerCase(Locale.ENGLISH)), GHPullRequest[].class, false)) { + @Override + protected void wrapUp(GHPullRequest[] page) { + for (GHPullRequest pr : page) + pr.wrap(GHRepository.this); + } + }; + }; + }; } /** diff --git a/src/main/java/org/kohsuke/github/PagedIterator.java b/src/main/java/org/kohsuke/github/PagedIterator.java index 74c197aa2e..60cffa9019 100644 --- a/src/main/java/org/kohsuke/github/PagedIterator.java +++ b/src/main/java/org/kohsuke/github/PagedIterator.java @@ -1,5 +1,6 @@ package org.kohsuke.github; +import java.util.ArrayList; import java.util.Arrays; import java.util.Iterator; import java.util.List; @@ -8,7 +9,8 @@ * Iterator over a pagenated data source. * * Aside from the normal iterator operation, this method exposes {@link #nextPage()} - * that allows the caller to retrieve items per page. + * that allows the caller to retrieve items per page and {@link #asList()} + * that allows the caller to retrieve all items at once. * * @author Kohsuke Kawaguchi */ @@ -61,4 +63,15 @@ public List nextPage() { pos = 0; return r; } + + /** + * Gets a list of all items + */ + public List asList() { + List r = new ArrayList(); + for(Iterator i = this; i.hasNext();) { + r.addAll(nextPage()); + } + return r; + } } diff --git a/src/test/java/org/kohsuke/AppTest.java b/src/test/java/org/kohsuke/AppTest.java index 120ef0e7c1..b79ce584f2 100644 --- a/src/test/java/org/kohsuke/AppTest.java +++ b/src/test/java/org/kohsuke/AppTest.java @@ -14,11 +14,13 @@ import org.kohsuke.github.GHMyself; import org.kohsuke.github.GHOrganization; import org.kohsuke.github.GHOrganization.Permission; +import org.kohsuke.github.GHPullRequest; import org.kohsuke.github.GHRepository; import org.kohsuke.github.GHTeam; import org.kohsuke.github.GHUser; import org.kohsuke.github.GitHub; import org.kohsuke.github.PagedIterable; +import org.kohsuke.github.PagedIterator; import java.io.IOException; import java.net.URL; @@ -64,6 +66,17 @@ public void testFetchPullRequest() throws Exception { r.getPullRequests(GHIssueState.OPEN); } + public void testFetchPullRequestAsList() throws Exception { + GitHub gh = GitHub.connect(); + GHRepository r = gh.getOrganization("symfony").getRepository("symfony-docs"); + assertEquals("master", r.getMasterBranch()); + PagedIterator i = r.getPullRequests(GHIssueState.CLOSED).iterator(); + List prs = i.asList(); + assertNotNull(prs); + assertTrue(prs.size() > 0); + assertFalse(i.hasNext()); + } + public void testRepoPermissions() throws Exception { GitHub gh = GitHub.connect(); GHRepository r = gh.getOrganization("jenkinsci").getRepository("jenkins");