diff --git a/src/main/java/org/kohsuke/github/GHDirection.java b/src/main/java/org/kohsuke/github/GHDirection.java new file mode 100644 index 0000000000..0db172dccf --- /dev/null +++ b/src/main/java/org/kohsuke/github/GHDirection.java @@ -0,0 +1,10 @@ +package org.kohsuke.github; + +/** + * Sort direction + * + * @author Kohsuke Kawaguchi + */ +public enum GHDirection { + ASC, DESC +} diff --git a/src/main/java/org/kohsuke/github/GHIssueState.java b/src/main/java/org/kohsuke/github/GHIssueState.java index d0af550ead..ec3cf10d24 100644 --- a/src/main/java/org/kohsuke/github/GHIssueState.java +++ b/src/main/java/org/kohsuke/github/GHIssueState.java @@ -24,7 +24,11 @@ package org.kohsuke.github; +/** + * @see GHPullRequestQueryBuilder#state(GHIssueState) + */ public enum GHIssueState { OPEN, - CLOSED + CLOSED, + ALL } \ No newline at end of file diff --git a/src/main/java/org/kohsuke/github/GHPullRequestQueryBuilder.java b/src/main/java/org/kohsuke/github/GHPullRequestQueryBuilder.java new file mode 100644 index 0000000000..141cf4d681 --- /dev/null +++ b/src/main/java/org/kohsuke/github/GHPullRequestQueryBuilder.java @@ -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 { + 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 list() { + return new PagedIterable() { + public PagedIterator _iterator(int pageSize) { + return new PagedIterator(req.asIterator(repo.getApiTailUrl("pulls"), GHPullRequest[].class, pageSize)) { + @Override + protected void wrapUp(GHPullRequest[] page) { + for (GHPullRequest pr : page) + pr.wrapUp(repo); + } + }; + } + }; + } +} diff --git a/src/main/java/org/kohsuke/github/GHQueryBuilder.java b/src/main/java/org/kohsuke/github/GHQueryBuilder.java new file mode 100644 index 0000000000..bb85fbbe95 --- /dev/null +++ b/src/main/java/org/kohsuke/github/GHQueryBuilder.java @@ -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 { + 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 list(); +} diff --git a/src/main/java/org/kohsuke/github/GHRepository.java b/src/main/java/org/kohsuke/github/GHRepository.java index dbc6a836e8..38a821143a 100644 --- a/src/main/java/org/kohsuke/github/GHRepository.java +++ b/src/main/java/org/kohsuke/github/GHRepository.java @@ -617,24 +617,24 @@ public GHPullRequest getPullRequest(int i) throws IOException { * @see #listPullRequests(GHIssueState) */ public List 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 listPullRequests(final GHIssueState state) { - return new PagedIterable() { - public PagedIterator _iterator(int pageSize) { - return new PagedIterator(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 listPullRequests(GHIssueState state) { + return queryPullRequests().state(state).list(); + } + + /** + * Retrieves pull requests. + */ + public GHPullRequestQueryBuilder queryPullRequests() { + return new GHPullRequestQueryBuilder(this); } /** diff --git a/src/main/java/org/kohsuke/github/GHSearchBuilder.java b/src/main/java/org/kohsuke/github/GHSearchBuilder.java index 67d10bdb72..17c2db85b8 100644 --- a/src/main/java/org/kohsuke/github/GHSearchBuilder.java +++ b/src/main/java/org/kohsuke/github/GHSearchBuilder.java @@ -10,9 +10,7 @@ * * @author Kohsuke Kawaguchi */ -public abstract class GHSearchBuilder { - protected final GitHub root; - protected final Requester req; +public abstract class GHSearchBuilder extends GHQueryBuilder { protected final List terms = new ArrayList(); /** @@ -21,15 +19,14 @@ public abstract class GHSearchBuilder { private final Class> receiverType; /*package*/ GHSearchBuilder(GitHub root, Class> receiverType) { - this.root = root; - this.req = root.retrieve(); + super(root); this.receiverType = receiverType; } /** * Search terms. */ - public GHSearchBuilder q(String term) { + public GHQueryBuilder q(String term) { terms.add(term); return this; } @@ -37,6 +34,7 @@ public GHSearchBuilder q(String term) { /** * Performs the search. */ + @Override public PagedSearchIterable list() { return new PagedSearchIterable(root) { public PagedIterator _iterator(int pageSize) { diff --git a/src/main/java/org/kohsuke/github/Requester.java b/src/main/java/org/kohsuke/github/Requester.java index 7c3052a817..c9835677ea 100644 --- a/src/main/java/org/kohsuke/github/Requester.java +++ b/src/main/java/org/kohsuke/github/Requester.java @@ -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; @@ -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);