Compare commits

...

22 Commits

Author SHA1 Message Date
Kohsuke Kawaguchi
dcaf926a95 [maven-release-plugin] prepare release github-api-1.31 2012-08-28 11:02:59 -07:00
Honza Brázdil
587278f282 we need to maintain the binary compatibility, so I reverted getComments and added listComments that exposes PagedIterable.
inspired by 8f95c4f179
2012-08-28 19:09:18 +02:00
Honza Brázdil
cc3793cbcd Comments are paged 2012-08-28 18:59:09 +02:00
Honza Brázdil
c268a5dd07 removed unused throws statement 2012-08-28 18:59:09 +02:00
Honza Brázdil
58d10df5e3 Fix GHIssue.getState() No enum constant 2012-08-28 18:59:09 +02:00
Honza Brázdil
ae2d01a878 fix GHPullRequest.getLabels() NPE 2012-08-28 18:59:09 +02:00
Honza Brázdil
dbc5b0b742 user is not just username; added url 2012-08-28 18:59:09 +02:00
Kohsuke Kawaguchi
6af12c2335 [maven-release-plugin] prepare for next development iteration 2012-08-28 09:43:22 -07:00
Kohsuke Kawaguchi
dafb50d6a9 [maven-release-plugin] prepare release github-api-1.30 2012-08-28 09:43:14 -07:00
Kohsuke Kawaguchi
13c59b6618 Merge branch 'pull-15' 2012-08-28 09:41:59 -07:00
Kohsuke Kawaguchi
8f95c4f179 Massaging the pull request 15.
- we need to maintain the binary compatibility, so I reverted
  getPullRequests and added listPullRequests that exposes PagedIterable.

- Made PagedIterator expose asList.
2012-08-28 09:39:49 -07:00
Aurélien Thieriot
9fd34aec7f Paging GHPullRequests getter and allow to transform iterator to a list 2012-08-12 13:01:42 +02:00
Kohsuke Kawaguchi
17c7a3e7c5 [maven-release-plugin] prepare for next development iteration 2012-06-18 12:51:29 -07:00
Kohsuke Kawaguchi
40a8c110bf [maven-release-plugin] prepare release github-api-1.29 2012-06-18 12:51:25 -07:00
Kohsuke Kawaguchi
c9cd0a4d1f added a simple CRUD test 2012-06-18 12:50:47 -07:00
Kohsuke Kawaguchi
45eae77f8f added missing repository delete operation 2012-06-18 12:50:40 -07:00
Kohsuke Kawaguchi
926202900c fixed a bug in editing the repository definition 2012-06-18 12:37:27 -07:00
Kohsuke Kawaguchi
21aa669503 redundant test case 2012-06-18 12:24:30 -07:00
Kohsuke Kawaguchi
dee28e7a7a Doc says this is asynchronous 2012-06-18 12:23:48 -07:00
Kohsuke Kawaguchi
c8f46a3666 needs to wrap up 2012-06-18 12:23:34 -07:00
Kohsuke Kawaguchi
ba7fe10a08 bug fix 2012-06-18 12:23:20 -07:00
Kohsuke Kawaguchi
9e9db72878 [maven-release-plugin] prepare for next development iteration 2012-06-13 08:27:58 -07:00
8 changed files with 120 additions and 34 deletions

View File

@@ -7,7 +7,7 @@
</parent> </parent>
<artifactId>github-api</artifactId> <artifactId>github-api</artifactId>
<version>1.28</version> <version>1.31</version>
<name>GitHub API for Java</name> <name>GitHub API for Java</name>
<url>http://github-api.kohsuke.org/</url> <url>http://github-api.kohsuke.org/</url>
<description>GitHub API for Java</description> <description>GitHub API for Java</description>

View File

@@ -31,6 +31,7 @@ import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Locale;
/** /**
* Represents an issue on GitHub. * Represents an issue on GitHub.
@@ -93,10 +94,13 @@ public class GHIssue {
} }
public GHIssueState getState() { public GHIssueState getState() {
return Enum.valueOf(GHIssueState.class, state); return Enum.valueOf(GHIssueState.class, state.toUpperCase(Locale.ENGLISH));
} }
public Collection<String> getLabels() { public Collection<String> getLabels() {
if(labels == null){
return Collections.EMPTY_LIST;
}
return Collections.unmodifiableList(labels); return Collections.unmodifiableList(labels);
} }
@@ -152,12 +156,27 @@ public class GHIssue {
/** /**
* Obtains all the comments associated with this issue. * Obtains all the comments associated with this issue.
*
* @see #listComments()
*/ */
public List<GHIssueComment> getComments() throws IOException { public List<GHIssueComment> getComments() throws IOException {
GHIssueComment[] r = root.retrieve(getApiRoute() + "/comments", GHIssueComment[].class); return listComments().asList();
for (GHIssueComment c : r) }
c.wrapUp(this);
return Arrays.asList(r); /**
* Obtains all the comments associated with this issue.
*/
public PagedIterable<GHIssueComment> listComments() throws IOException {
return new PagedIterable<GHIssueComment>() {
public PagedIterator<GHIssueComment> iterator() {
return new PagedIterator<GHIssueComment>(root.retrievePaged(getApiRoute() + "/comments",GHIssueComment[].class,false)) {
protected void wrapUp(GHIssueComment[] page) {
for (GHIssueComment c : page)
c.wrapUp(GHIssue.this);
}
};
}
};
} }
private String getApiRoute() { private String getApiRoute() {

View File

@@ -24,6 +24,7 @@
package org.kohsuke.github; package org.kohsuke.github;
import java.io.IOException; import java.io.IOException;
import java.net.URL;
import java.util.Date; import java.util.Date;
/** /**
@@ -34,8 +35,10 @@ import java.util.Date;
public class GHIssueComment { public class GHIssueComment {
GHIssue owner; GHIssue owner;
private String body, gravatar_id, user, created_at, updated_at; private String body, gravatar_id, created_at, updated_at;
private URL url;
private int id; private int id;
private GHUser user;
/*package*/ GHIssueComment wrapUp(GHIssue owner) { /*package*/ GHIssueComment wrapUp(GHIssue owner) {
this.owner = owner; this.owner = owner;
@@ -68,17 +71,22 @@ public class GHIssueComment {
return id; return id;
} }
public URL getUrl() {
return url;
}
/** /**
* Gets the ID of the user who posted this comment. * Gets the ID of the user who posted this comment.
*/ */
@Deprecated
public String getUserName() { public String getUserName() {
return user; return user.getLogin();
} }
/** /**
* Gets the user who posted this comment. * Gets the user who posted this comment.
*/ */
public GHUser getUser() throws IOException { public GHUser getUser() throws IOException {
return owner.root.getUser(user); return owner.root.getUser(user.getLogin());
} }
} }

View File

@@ -45,7 +45,7 @@ public class GHOrganization extends GHPerson {
GHTeam[] teams = root.retrieveWithAuth("/orgs/" + login + "/teams", GHTeam[].class); GHTeam[] teams = root.retrieveWithAuth("/orgs/" + login + "/teams", GHTeam[].class);
Map<String,GHTeam> r = new TreeMap<String, GHTeam>(); Map<String,GHTeam> r = new TreeMap<String, GHTeam>();
for (GHTeam t : teams) { for (GHTeam t : teams) {
r.put(t.getName(),t); r.put(t.getName(),t.wrapUp(this));
} }
return r; return r;
} }

View File

@@ -32,6 +32,7 @@ import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.infradna.tool.bridge_method_injector.WithBridgeMethods; import com.infradna.tool.bridge_method_injector.WithBridgeMethods;
import java.io.IOException; import java.io.IOException;
import java.io.InterruptedIOException;
import java.net.URL; import java.net.URL;
import java.util.AbstractSet; import java.util.AbstractSet;
import java.util.ArrayList; import java.util.ArrayList;
@@ -269,8 +270,11 @@ public class GHRepository {
} }
private void edit(String key, String value) throws IOException { private void edit(String key, String value) throws IOException {
new Poster(root).withCredential().with(key,value) Poster poster = new Poster(root).withCredential();
.to("/repos/" + owner.login + "/" + name,null,"PATCH"); if (!key.equals("name"))
poster.with("name", name); // even when we don't change the name, we need to send it in
poster.with(key, value)
.to("/repos/" + owner.login + "/" + name, null, "PATCH");
} }
/** /**
@@ -310,12 +314,7 @@ public class GHRepository {
* Deletes this repository. * Deletes this repository.
*/ */
public void delete() throws IOException { public void delete() throws IOException {
throw new UnsupportedOperationException(); // doesn't appear to be available in V3 new Poster(root).withCredential().to("/repos/" + owner.login +"/"+name, null, "DELETE");
// Poster poster = new Poster(root).withCredential();
// String url = "/repos/delete/" + owner.login +"/"+name;
//
// DeleteToken token = poster.to(url, DeleteToken.class);
// poster.with("delete_token", token.delete_token).to(url);
} }
/** /**
@@ -336,7 +335,18 @@ public class GHRepository {
*/ */
public GHRepository forkTo(GHOrganization org) throws IOException { public GHRepository forkTo(GHOrganization org) throws IOException {
new Poster(root).withCredential().to(String.format("/repos/%s/%s/forks?org=%s",owner.login,name,org.getLogin())); new Poster(root).withCredential().to(String.format("/repos/%s/%s/forks?org=%s",owner.login,name,org.getLogin()));
return org.getRepository(name);
// this API is asynchronous. we need to wait for a bit
for (int i=0; i<10; i++) {
GHRepository r = org.getRepository(name);
if (r!=null) return r;
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
throw (IOException)new InterruptedIOException().initCause(e);
}
}
throw new IOException(this+" was forked into "+org.getLogin()+" but can't find the new repository");
} }
/** /**
@@ -348,12 +358,28 @@ public class GHRepository {
/** /**
* Retrieves all the pull requests of a particular state. * Retrieves all the pull requests of a particular state.
*
* @see #listPullRequests(GHIssueState)
*/ */
public List<GHPullRequest> getPullRequests(GHIssueState state) throws IOException { 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); return listPullRequests(state).asList();
for (GHPullRequest p : r) }
p.wrapUp(this);
return new ArrayList<GHPullRequest>(Arrays.asList(r)); /**
* Retrieves all the pull requests of a particular state.
*/
public PagedIterable<GHPullRequest> listPullRequests(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

@@ -129,11 +129,11 @@ public class GitHub {
return new GitHub(props.getProperty("login"),props.getProperty("token"),props.getProperty("password")); return new GitHub(props.getProperty("login"),props.getProperty("token"),props.getProperty("password"));
} }
public static GitHub connect(String login, String apiToken) throws IOException { public static GitHub connect(String login, String apiToken){
return new GitHub(login,apiToken,null); return new GitHub(login,apiToken,null);
} }
public static GitHub connect(String login, String apiToken, String password) throws IOException { public static GitHub connect(String login, String apiToken, String password){
return new GitHub(login,apiToken,password); return new GitHub(login,apiToken,password);
} }
@@ -285,12 +285,12 @@ public class GitHub {
uc.setRequestProperty("Authorization", "Basic " + encodedAuthorization); uc.setRequestProperty("Authorization", "Basic " + encodedAuthorization);
uc.setRequestMethod(method); uc.setRequestMethod(method);
uc.setRequestProperty("Accept-Encoding", "gzip");
if (method.equals("PUT")) { if (method.equals("PUT")) {
uc.setDoOutput(true); uc.setDoOutput(true);
uc.setRequestProperty("Content-Length","0"); uc.setRequestProperty("Content-Length","0");
uc.getOutputStream().close(); uc.getOutputStream().close();
} }
uc.setRequestProperty("Accept-Encoding", "gzip");
return uc; return uc;
} }

View File

@@ -1,8 +1,24 @@
package org.kohsuke.github; package org.kohsuke.github;
import java.util.ArrayList;
import java.util.List;
/** /**
* {@link Iterable} that returns {@link PagedIterator}
*
* @author Kohsuke Kawaguchi * @author Kohsuke Kawaguchi
*/ */
public interface PagedIterable<T> extends Iterable<T> { public abstract class PagedIterable<T> implements Iterable<T> {
PagedIterator<T> iterator(); public abstract PagedIterator<T> iterator();
/**
* Eagerly walk {@link Iterable} and return the result in a list.
*/
public List<T> asList() {
List<T> r = new ArrayList<T>();
for(PagedIterator<T> i = iterator(); i.hasNext();) {
r.addAll(i.nextPage());
}
return r;
}
} }

View File

@@ -14,11 +14,13 @@ import org.kohsuke.github.GHKey;
import org.kohsuke.github.GHMyself; import org.kohsuke.github.GHMyself;
import org.kohsuke.github.GHOrganization; import org.kohsuke.github.GHOrganization;
import org.kohsuke.github.GHOrganization.Permission; import org.kohsuke.github.GHOrganization.Permission;
import org.kohsuke.github.GHPullRequest;
import org.kohsuke.github.GHRepository; import org.kohsuke.github.GHRepository;
import org.kohsuke.github.GHTeam; import org.kohsuke.github.GHTeam;
import org.kohsuke.github.GHUser; import org.kohsuke.github.GHUser;
import org.kohsuke.github.GitHub; import org.kohsuke.github.GitHub;
import org.kohsuke.github.PagedIterable; import org.kohsuke.github.PagedIterable;
import org.kohsuke.github.PagedIterator;
import java.io.IOException; import java.io.IOException;
import java.net.URL; import java.net.URL;
@@ -31,6 +33,16 @@ import java.util.List;
* Unit test for simple App. * Unit test for simple App.
*/ */
public class AppTest extends TestCase { public class AppTest extends TestCase {
public void testRepoCRUD() throws Exception {
GitHub hub = GitHub.connect();
GHRepository r = hub.createRepository("github-api-test", "a test repository", "http://github-api.kohsuke.org/", true);
r.enableIssueTracker(false);
r.enableDownloads(false);
r.enableWiki(false);
r.renameTo("github-api-test2");
hub.getMyself().getRepository("github-api-test2").delete();
}
public void testCredentialValid() throws IOException { public void testCredentialValid() throws IOException {
assertTrue(GitHub.connect().isCredentialValid()); assertTrue(GitHub.connect().isCredentialValid());
assertFalse(GitHub.connect("totally","bogus").isCredentialValid()); assertFalse(GitHub.connect("totally","bogus").isCredentialValid());
@@ -54,6 +66,16 @@ public class AppTest extends TestCase {
r.getPullRequests(GHIssueState.OPEN); 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());
PagedIterable<GHPullRequest> i = r.listPullRequests(GHIssueState.CLOSED);
List<GHPullRequest> prs = i.asList();
assertNotNull(prs);
assertTrue(prs.size() > 0);
}
public void testRepoPermissions() throws Exception { public void testRepoPermissions() throws Exception {
GitHub gh = GitHub.connect(); GitHub gh = GitHub.connect();
GHRepository r = gh.getOrganization("jenkinsci").getRepository("jenkins"); GHRepository r = gh.getOrganization("jenkinsci").getRepository("jenkins");
@@ -253,11 +275,6 @@ public class AppTest extends TestCase {
gitHub.getUser("kohsuke").getRepository("test").renameTo("test2"); gitHub.getUser("kohsuke").getRepository("test").renameTo("test2");
} }
private void tryOrgFork(GitHub gitHub) throws IOException {
GHOrganization o = gitHub.getOrganization("HudsonLabs");
System.out.println(gitHub.getUser("rtyler").getRepository("memcache-ada").forkTo(o).getUrl());
}
private void tryTeamCreation(GitHub gitHub) throws IOException { private void tryTeamCreation(GitHub gitHub) throws IOException {
GHOrganization o = gitHub.getOrganization("HudsonLabs"); GHOrganization o = gitHub.getOrganization("HudsonLabs");
GHTeam t = o.createTeam("auto team", Permission.PUSH); GHTeam t = o.createTeam("auto team", Permission.PUSH);