Skip to content

Commit

Permalink
Added more comprehensive API to list pull requests
Browse files Browse the repository at this point in the history
This fixes issue #234
  • Loading branch information
kohsuke committed Dec 10, 2015
1 parent 03ac6c7 commit 2440a67
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 20 deletions.
10 changes: 10 additions & 0 deletions src/main/java/org/kohsuke/github/GHDirection.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.kohsuke.github;

/**
* Sort direction
*
* @author Kohsuke Kawaguchi
*/
public enum GHDirection {
ASC, DESC
}
6 changes: 5 additions & 1 deletion src/main/java/org/kohsuke/github/GHIssueState.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@

package org.kohsuke.github;

/**
* @see GHPullRequestQueryBuilder#state(GHIssueState)
*/
public enum GHIssueState {
OPEN,
CLOSED
CLOSED,
ALL
}
58 changes: 58 additions & 0 deletions src/main/java/org/kohsuke/github/GHPullRequestQueryBuilder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package org.kohsuke.github;

/**
* Lists up pull requests with some filtering and sorting.
*
* @author Kohsuke Kawaguchi
* @see GHRepository#queryPullRequests()
*/
public class GHPullRequestQueryBuilder extends GHQueryBuilder<GHPullRequest> {
private final GHRepository repo;

/*package*/ GHPullRequestQueryBuilder(GHRepository repo) {
super(repo.root);
this.repo = repo;
}

public GHPullRequestQueryBuilder state(GHIssueState state) {
req.with("state",state);
return this;
}

public GHPullRequestQueryBuilder head(String head) {
req.with("head",head);
return this;
}

public GHPullRequestQueryBuilder base(String base) {
req.with("base",base);
return this;
}

public GHPullRequestQueryBuilder sort(Sort sort) {
req.with("sort",sort);
return this;
}

public enum Sort { CREATED, UPDATED, POPULARITY, LONG_RUNNING }

public GHPullRequestQueryBuilder direction(GHDirection d) {
req.with("direction",d);
return this;
}

@Override
public PagedIterable<GHPullRequest> list() {
return new PagedIterable<GHPullRequest>() {
public PagedIterator<GHPullRequest> _iterator(int pageSize) {
return new PagedIterator<GHPullRequest>(req.asIterator(repo.getApiTailUrl("pulls"), GHPullRequest[].class, pageSize)) {
@Override
protected void wrapUp(GHPullRequest[] page) {
for (GHPullRequest pr : page)
pr.wrapUp(repo);
}
};
}
};
}
}
21 changes: 21 additions & 0 deletions src/main/java/org/kohsuke/github/GHQueryBuilder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.kohsuke.github;

/**
* Used to specify filters, sort order, etc for listing items in a collection.
*
* @author Kohsuke Kawaguchi
*/
public abstract class GHQueryBuilder<T> {
protected final GitHub root;
protected final Requester req;

/*package*/ GHQueryBuilder(GitHub root) {
this.root = root;
this.req = root.retrieve();
}

/**
* Start listing items by using the settings built up on this object.
*/
public abstract PagedIterable<T> list();
}
26 changes: 13 additions & 13 deletions src/main/java/org/kohsuke/github/GHRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -617,24 +617,24 @@ public GHPullRequest getPullRequest(int i) throws IOException {
* @see #listPullRequests(GHIssueState)
*/
public List<GHPullRequest> getPullRequests(GHIssueState state) throws IOException {
return listPullRequests(state).asList();
return queryPullRequests().state(state).list().asList();
}

/**
* Retrieves all the pull requests of a particular state.
*
* @deprecated
* Use {@link #queryPullRequests()}
*/
public PagedIterable<GHPullRequest> listPullRequests(final GHIssueState state) {
return new PagedIterable<GHPullRequest>() {
public PagedIterator<GHPullRequest> _iterator(int pageSize) {
return new PagedIterator<GHPullRequest>(root.retrieve().asIterator(getApiTailUrl("pulls?state="+state.name().toLowerCase(Locale.ENGLISH)), GHPullRequest[].class, pageSize)) {
@Override
protected void wrapUp(GHPullRequest[] page) {
for (GHPullRequest pr : page)
pr.wrapUp(GHRepository.this);
}
};
}
};
public PagedIterable<GHPullRequest> listPullRequests(GHIssueState state) {
return queryPullRequests().state(state).list();
}

/**
* Retrieves pull requests.
*/
public GHPullRequestQueryBuilder queryPullRequests() {
return new GHPullRequestQueryBuilder(this);
}

/**
Expand Down
10 changes: 4 additions & 6 deletions src/main/java/org/kohsuke/github/GHSearchBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@
*
* @author Kohsuke Kawaguchi
*/
public abstract class GHSearchBuilder<T> {
protected final GitHub root;
protected final Requester req;
public abstract class GHSearchBuilder<T> extends GHQueryBuilder<T> {
protected final List<String> terms = new ArrayList<String>();

/**
Expand All @@ -21,22 +19,22 @@ public abstract class GHSearchBuilder<T> {
private final Class<? extends SearchResult<T>> receiverType;

/*package*/ GHSearchBuilder(GitHub root, Class<? extends SearchResult<T>> receiverType) {
this.root = root;
this.req = root.retrieve();
super(root);
this.receiverType = receiverType;
}

/**
* Search terms.
*/
public GHSearchBuilder q(String term) {
public GHQueryBuilder<T> q(String term) {
terms.add(term);
return this;
}

/**
* Performs the search.
*/
@Override
public PagedSearchIterable<T> list() {
return new PagedSearchIterable<T>(root) {
public PagedIterator<T> _iterator(int pageSize) {
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/org/kohsuke/github/Requester.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.regex.Matcher;
Expand Down Expand Up @@ -130,6 +131,14 @@ public Requester with(String key, Boolean value) {
return _with(key, value);
}

public Requester with(String key, Enum e) {
if (e==null) return _with(key, null);

// by convention Java constant names are upper cases, but github uses
// lower-case constants. GitHub also uses '-', which in Java we always
// replace by '_'
return with(key, e.toString().toLowerCase(Locale.ENGLISH).replace('_','-'));
}

public Requester with(String key, String value) {
return _with(key, value);
Expand Down

0 comments on commit 2440a67

Please sign in to comment.