Skip to content

Commit

Permalink
Paging GHPullRequests getter and allow to transform iterator to a list
Browse files Browse the repository at this point in the history
  • Loading branch information
athieriot committed Aug 12, 2012
1 parent 17c7a3e commit 9fd34ae
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/main/java/org/kohsuke/github/GHOrganization.java
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public List<GHRepository> getRepositoriesWithOpenPullRequests() throws IOExcepti
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;
}
Expand Down
17 changes: 12 additions & 5 deletions src/main/java/org/kohsuke/github/GHRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -359,11 +359,18 @@ public GHPullRequest getPullRequest(int i) throws IOException {
/**
* 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);
}
};
};
};
}

/**
Expand Down
15 changes: 14 additions & 1 deletion src/main/java/org/kohsuke/github/PagedIterator.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.kohsuke.github;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
Expand All @@ -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
*/
Expand Down Expand Up @@ -61,4 +63,15 @@ public List<T> nextPage() {
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;
}
}
13 changes: 13 additions & 0 deletions src/test/java/org/kohsuke/AppTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<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");
Expand Down

0 comments on commit 9fd34ae

Please sign in to comment.