mirror of
https://github.com/jlengrand/github-api.git
synced 2026-03-13 08:21:20 +00:00
Compare commits
22 Commits
github-api
...
github-api
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
dcaf926a95 | ||
|
|
587278f282 | ||
|
|
cc3793cbcd | ||
|
|
c268a5dd07 | ||
|
|
58d10df5e3 | ||
|
|
ae2d01a878 | ||
|
|
dbc5b0b742 | ||
|
|
6af12c2335 | ||
|
|
dafb50d6a9 | ||
|
|
13c59b6618 | ||
|
|
8f95c4f179 | ||
|
|
9fd34aec7f | ||
|
|
17c7a3e7c5 | ||
|
|
40a8c110bf | ||
|
|
c9cd0a4d1f | ||
|
|
45eae77f8f | ||
|
|
926202900c | ||
|
|
21aa669503 | ||
|
|
dee28e7a7a | ||
|
|
c8f46a3666 | ||
|
|
ba7fe10a08 | ||
|
|
9e9db72878 |
2
pom.xml
2
pom.xml
@@ -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>
|
||||||
|
|||||||
@@ -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() {
|
||||||
|
|||||||
@@ -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());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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);
|
||||||
|
|||||||
Reference in New Issue
Block a user