Skip to content

Commit

Permalink
Merge branch 'pull-86'
Browse files Browse the repository at this point in the history
Conflicts:
	src/main/java/org/kohsuke/github/GHOrganization.java
	src/main/java/org/kohsuke/github/Requester.java
	src/test/java/org/kohsuke/AppTest.java
  • Loading branch information
kohsuke committed May 10, 2014
2 parents bba4901 + 9267762 commit a409b4f
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 7 deletions.
105 changes: 105 additions & 0 deletions src/main/java/org/kohsuke/github/GHCommitQueryBuilder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package org.kohsuke.github;

import java.util.Date;

/**
* Builds up query for listing commits.
*
* <p>
* Call various methods that set the filter criteria, then {@link #list()} method to actually list up the commit.
*
* <pre>
* GHRepository r = ...;
* for (GHCommit c : r.queryCommits().since(x).until(y).author("kohsuke")) {
* ...
* }
* </pre>
*
* @author Kohsuke Kawaguchi
* @see GHRepository#queryCommits()
*/
public class GHCommitQueryBuilder {
private final Requester req;
private final GHRepository repo;

/*package*/ GHCommitQueryBuilder(GHRepository repo) {
this.repo = repo;
this.req = repo.root.retrieve(); // requester to build up
}

/**
* GItHub login or email address by which to filter by commit author.
*/
public GHCommitQueryBuilder author(String author) {
req.with("author",author);
return this;
}

/**
* Only commits containing this file path will be returned.
*/
public GHCommitQueryBuilder path(String path) {
req.with("path",path);
return this;
}

/**
* Specifies the SHA1 commit / tag / branch / etc to start listing commits from.
*
*/
public GHCommitQueryBuilder from(String ref) {
req.with("sha",ref);
return this;
}

public GHCommitQueryBuilder pageSize(int pageSize) {
req.with("per_page",pageSize);
return this;
}

/**
* Only commits after this date will be returned
*/
public GHCommitQueryBuilder since(Date dt) {
req.with("since",GitHub.printDate(dt));
return this;
}

/**
* Only commits after this date will be returned
*/
public GHCommitQueryBuilder since(long timestamp) {
return since(new Date(timestamp));
}

/**
* Only commits before this date will be returned
*/
public GHCommitQueryBuilder until(Date dt) {
req.with("until",GitHub.printDate(dt));
return this;
}

/**
* Only commits before this date will be returned
*/
public GHCommitQueryBuilder until(long timestamp) {
return until(new Date(timestamp));
}

/**
* Lists up the commits with the criteria built so far.
*/
public PagedIterable<GHCommit> list() {
return new PagedIterable<GHCommit>() {
public PagedIterator<GHCommit> iterator() {
return new PagedIterator<GHCommit>(req.asIterator(repo.getApiTailUrl("commits"), GHCommit[].class)) {
protected void wrapUp(GHCommit[] page) {
for (GHCommit c : page)
c.wrapUp(repo);
}
};
}
};
}
}
8 changes: 8 additions & 0 deletions src/main/java/org/kohsuke/github/GHRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
Expand Down Expand Up @@ -498,6 +499,13 @@ protected void wrapUp(GHCommit[] page) {
};
}

/**
* Search commits by specifying filters through a builder pattern.
*/
public GHCommitQueryBuilder queryCommits() {
return new GHCommitQueryBuilder(this);
}

/**
* Lists up all the commit comments in this repository.
*/
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/org/kohsuke/github/GitHub.java
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,10 @@ public boolean isCredentialValid() throws IOException {
throw new IllegalStateException("Unable to parse the timestamp: "+timestamp);
}

/*package*/ static String printDate(Date dt) {
return new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'").format(dt);
}

/*package*/ static final ObjectMapper MAPPER = new ObjectMapper();

private static final String[] TIME_FORMATS = {"yyyy/MM/dd HH:mm:ss ZZZZ","yyyy-MM-dd'T'HH:mm:ss'Z'"};
Expand Down
24 changes: 20 additions & 4 deletions src/main/java/org/kohsuke/github/Requester.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,21 @@
*/
package org.kohsuke.github;

import org.apache.commons.io.IOUtils;
import static org.kohsuke.github.GitHub.MAPPER;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.InterruptedIOException;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Field;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
Expand All @@ -47,7 +49,7 @@
import java.util.Set;
import java.util.zip.GZIPInputStream;

import static org.kohsuke.github.GitHub.MAPPER;
import org.apache.commons.io.IOUtils;

/**
* A builder pattern for making HTTP call and parsing its output.
Expand Down Expand Up @@ -208,9 +210,23 @@ private <T> T _to(String tailApiUrl, Class<T> type, T instance) throws IOExcepti
*
* Every iterator call reports a new batch.
*/
/*package*/ <T> Iterator<T> asIterator(final String tailApiUrl, final Class<T> type) {
/*package*/ <T> Iterator<T> asIterator(String _tailApiUrl, final Class<T> type) {
method("GET");
if (!args.isEmpty()) throw new IllegalStateException();

if (!args.isEmpty()) {
boolean first=true;
try {
for (Entry a : args) {
_tailApiUrl += first ? '?' : '&';
first = false;
_tailApiUrl += URLEncoder.encode(a.key,"UTF-8")+'='+URLEncoder.encode(a.value.toString(),"UTF-8");
}
} catch (UnsupportedEncodingException e) {
throw new AssertionError(e); // UTF-8 is mandatory
}
}

final String tailApiUrl = _tailApiUrl;

return new Iterator<T>() {
/**
Expand Down
25 changes: 22 additions & 3 deletions src/test/java/org/kohsuke/AppTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@
import org.kohsuke.github.GitHub;
import org.kohsuke.github.PagedIterable;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Date;
import java.util.Map;
import java.util.Set;
import java.util.List;

/**
* Unit test for simple App.
*/
Expand Down Expand Up @@ -252,7 +260,7 @@ public void testMembership() throws Exception {
Set<String> members = gitHub.getOrganization("jenkinsci").getRepository("violations-plugin").getCollaboratorNames();
System.out.println(members.contains("kohsuke"));
}

@Test
public void testMemberOrgs() throws Exception {
Set<GHOrganization> o = gitHub.getUser("kohsuke").getOrganizations();
Expand Down Expand Up @@ -296,8 +304,19 @@ public void testListCommits() throws Exception {
System.out.println(c.getSHA1());
sha1.add(c.getSHA1());
}
assertEquals("fdfad6be4db6f96faea1f153fb447b479a7a9cb7",sha1.get(0));
assertEquals(1,sha1.size());
assertEquals("fdfad6be4db6f96faea1f153fb447b479a7a9cb7", sha1.get(0));
assertEquals(1, sha1.size());
}

public void testQueryCommits() throws Exception {
List<String> sha1 = new ArrayList<String>();
for (GHCommit c : gitHub.getUser("jenkinsci").getRepository("jenkins").queryCommits()
.since(new Date(1199174400000L)).until(1201852800000L).path("pom.xml").list()) {
System.out.println(c.getSHA1());
sha1.add(c.getSHA1());
}
assertEquals("1cccddb22e305397151b2b7b87b4b47d74ca337b",sha1.get(0));
assertEquals(29,sha1.size());
}

@Test
Expand Down

0 comments on commit a409b4f

Please sign in to comment.