Paging GHPullRequests getter and allow to transform iterator to a list

This commit is contained in:
Aurélien Thieriot
2012-08-12 12:25:14 +02:00
parent 17c7a3e7c5
commit 9fd34aec7f
4 changed files with 40 additions and 7 deletions

View File

@@ -129,7 +129,7 @@ public class GHOrganization extends GHPerson {
public List<GHPullRequest> getPullRequests() throws IOException {
List<GHPullRequest> all = new ArrayList<GHPullRequest>();
for (GHRepository r : getRepositoriesWithOpenPullRequests()) {
all.addAll(r.getPullRequests(GHIssueState.OPEN));
all.addAll(r.getPullRequests(GHIssueState.OPEN).iterator().asList());
}
return all;
}

View File

@@ -359,11 +359,18 @@ public class GHRepository {
/**
* Retrieves all the pull requests of a particular state.
*/
public List<GHPullRequest> 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<GHPullRequest>(Arrays.asList(r));
public PagedIterable<GHPullRequest> getPullRequests(final GHIssueState state) {
return new PagedIterable<GHPullRequest>() {
public PagedIterator<GHPullRequest> iterator() {
return new PagedIterator<GHPullRequest>(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);
}
};
};
};
}
/**

View File

@@ -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 @@ import java.util.List;
* 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 abstract class PagedIterator<T> implements Iterator<T> {
pos = 0;
return r;
}
/**
* Gets a list of all items
*/
public List<T> asList() {
List<T> r = new ArrayList<T>();
for(Iterator i = this; i.hasNext();) {
r.addAll(nextPage());
}
return r;
}
}

View File

@@ -14,11 +14,13 @@ import org.kohsuke.github.GHKey;
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 class AppTest extends TestCase {
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<GHPullRequest> i = r.getPullRequests(GHIssueState.CLOSED).iterator();
List<GHPullRequest> 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");