mirror of
https://github.com/jlengrand/github-api.git
synced 2026-03-12 08:21:22 +00:00
Compare commits
34 Commits
github-api
...
github-api
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
db845850b2 | ||
|
|
d516597659 | ||
|
|
3af5a8145a | ||
|
|
31bebd4a7a | ||
|
|
ccb87258b0 | ||
|
|
2ab71e88bd | ||
|
|
78bd7585bb | ||
|
|
92cc81d9b8 | ||
|
|
97a1c741de | ||
|
|
077d693959 | ||
|
|
94831fc10a | ||
|
|
1f298a88b0 | ||
|
|
7e49946bed | ||
|
|
1e81ab1017 | ||
|
|
311180d12e | ||
|
|
0efb206881 | ||
|
|
0b92fa5615 | ||
|
|
a58a5b56b2 | ||
|
|
b9764c0a44 | ||
|
|
590f7ba511 | ||
|
|
449909b0e8 | ||
|
|
40780525f8 | ||
|
|
0e3707d1c3 | ||
|
|
8ddbef093b | ||
|
|
4ccdfccdf0 | ||
|
|
3c8aa0c630 | ||
|
|
ba519f996a | ||
|
|
62d9b92e6e | ||
|
|
d2c909584d | ||
|
|
219916eb14 | ||
|
|
0a37ac901f | ||
|
|
4586baea27 | ||
|
|
d83961e85b | ||
|
|
7e35716ff0 |
15
pom.xml
15
pom.xml
@@ -3,11 +3,11 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<groupId>org.kohsuke</groupId>
|
<groupId>org.kohsuke</groupId>
|
||||||
<artifactId>pom</artifactId>
|
<artifactId>pom</artifactId>
|
||||||
<version>8</version>
|
<version>10</version>
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
<artifactId>github-api</artifactId>
|
<artifactId>github-api</artifactId>
|
||||||
<version>1.55</version>
|
<version>1.58</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>
|
||||||
@@ -16,7 +16,7 @@
|
|||||||
<connection>scm:git:git@github.com/kohsuke/${project.artifactId}.git</connection>
|
<connection>scm:git:git@github.com/kohsuke/${project.artifactId}.git</connection>
|
||||||
<developerConnection>scm:git:ssh://git@github.com/kohsuke/${project.artifactId}.git</developerConnection>
|
<developerConnection>scm:git:ssh://git@github.com/kohsuke/${project.artifactId}.git</developerConnection>
|
||||||
<url>http://${project.artifactId}.kohsuke.org/</url>
|
<url>http://${project.artifactId}.kohsuke.org/</url>
|
||||||
<tag>github-api-1.55</tag>
|
<tag>github-api-1.58</tag>
|
||||||
</scm>
|
</scm>
|
||||||
|
|
||||||
<distributionManagement>
|
<distributionManagement>
|
||||||
@@ -93,8 +93,8 @@
|
|||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.squareup.okhttp</groupId>
|
<groupId>com.squareup.okhttp</groupId>
|
||||||
<artifactId>okhttp</artifactId>
|
<artifactId>okhttp-urlconnection</artifactId>
|
||||||
<version>1.5.3</version>
|
<version>2.0.0</version>
|
||||||
<optional>true</optional>
|
<optional>true</optional>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
@@ -103,6 +103,11 @@
|
|||||||
<version>1.2</version>
|
<version>1.2</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.mockito</groupId>
|
||||||
|
<artifactId>mockito-all</artifactId>
|
||||||
|
<version>1.9.5</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
<repositories>
|
<repositories>
|
||||||
<repository>
|
<repository>
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ package org.kohsuke.github;
|
|||||||
* The response that is returned when updating
|
* The response that is returned when updating
|
||||||
* repository content.
|
* repository content.
|
||||||
**/
|
**/
|
||||||
public final class GHContentUpdateResponse {
|
public class GHContentUpdateResponse {
|
||||||
private GHContent content;
|
private GHContent content;
|
||||||
private GHCommit commit;
|
private GHCommit commit;
|
||||||
|
|
||||||
|
|||||||
@@ -12,6 +12,8 @@ public enum GHEvent {
|
|||||||
COMMIT_COMMENT,
|
COMMIT_COMMENT,
|
||||||
CREATE,
|
CREATE,
|
||||||
DELETE,
|
DELETE,
|
||||||
|
DEPLOYMENT,
|
||||||
|
DEPLOYMENT_STATUS,
|
||||||
DOWNLOAD,
|
DOWNLOAD,
|
||||||
FOLLOW,
|
FOLLOW,
|
||||||
FORK,
|
FORK,
|
||||||
@@ -25,6 +27,8 @@ public enum GHEvent {
|
|||||||
PULL_REQUEST,
|
PULL_REQUEST,
|
||||||
PULL_REQUEST_REVIEW_COMMENT,
|
PULL_REQUEST_REVIEW_COMMENT,
|
||||||
PUSH,
|
PUSH,
|
||||||
|
RELEASE,
|
||||||
|
STATUS,
|
||||||
TEAM_ADD,
|
TEAM_ADD,
|
||||||
WATCH
|
WATCH
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ import java.util.Map.Entry;
|
|||||||
* @see GitHub#getGist(String)
|
* @see GitHub#getGist(String)
|
||||||
* @see GitHub#createGist()
|
* @see GitHub#createGist()
|
||||||
*/
|
*/
|
||||||
public final class GHGist {
|
public class GHGist {
|
||||||
/*package almost final*/ GHUser owner;
|
/*package almost final*/ GHUser owner;
|
||||||
/*package almost final*/ GitHub root;
|
/*package almost final*/ GitHub root;
|
||||||
|
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ import java.util.Map;
|
|||||||
/**
|
/**
|
||||||
* @author Kohsuke Kawaguchi
|
* @author Kohsuke Kawaguchi
|
||||||
*/
|
*/
|
||||||
public final class GHHook {
|
public class GHHook {
|
||||||
/**
|
/**
|
||||||
* Repository that the hook belongs to.
|
* Repository that the hook belongs to.
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -101,12 +101,24 @@ public class GHMyself extends GHUser {
|
|||||||
* Lists up all repositories this user owns (public and private).
|
* Lists up all repositories this user owns (public and private).
|
||||||
*
|
*
|
||||||
* Unlike {@link #getAllRepositories()}, this does not wait until all the repositories are returned.
|
* Unlike {@link #getAllRepositories()}, this does not wait until all the repositories are returned.
|
||||||
|
* Repositories are returned by GitHub API with a 30 items per page.
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public PagedIterable<GHRepository> listRepositories() {
|
public PagedIterable<GHRepository> listRepositories() {
|
||||||
|
return listRepositories(30);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Lists up all the repositories this user owns (public and private) using the specified page size.
|
||||||
|
*
|
||||||
|
* @param pageSize size for each page of items returned by GitHub. Maximum page size is 100.
|
||||||
|
*
|
||||||
|
* Unlike {@link #getRepositories()}, this does not wait until all the repositories are returned.
|
||||||
|
*/
|
||||||
|
public PagedIterable<GHRepository> listRepositories(final int pageSize) {
|
||||||
return new PagedIterable<GHRepository>() {
|
return new PagedIterable<GHRepository>() {
|
||||||
public PagedIterator<GHRepository> iterator() {
|
public PagedIterator<GHRepository> iterator() {
|
||||||
return new PagedIterator<GHRepository>(root.retrieve().asIterator("/user/repos", GHRepository[].class)) {
|
return new PagedIterator<GHRepository>(root.retrieve().asIterator("/user/repos?per_page=" + pageSize, GHRepository[].class)) {
|
||||||
@Override
|
@Override
|
||||||
protected void wrapUp(GHRepository[] page) {
|
protected void wrapUp(GHRepository[] page) {
|
||||||
for (GHRepository c : page)
|
for (GHRepository c : page)
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ import java.util.HashSet;
|
|||||||
*
|
*
|
||||||
* @author Kohsuke Kawaguchi
|
* @author Kohsuke Kawaguchi
|
||||||
*/
|
*/
|
||||||
public final class GHPersonSet<T extends GHPerson> extends HashSet<T> {
|
public class GHPersonSet<T extends GHPerson> extends HashSet<T> {
|
||||||
public GHPersonSet() {
|
public GHPersonSet() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -196,7 +196,7 @@ public class GHPullRequest extends GHIssue {
|
|||||||
return new PagedIterable<GHPullRequestCommitDetail>() {
|
return new PagedIterable<GHPullRequestCommitDetail>() {
|
||||||
public PagedIterator<GHPullRequestCommitDetail> iterator() {
|
public PagedIterator<GHPullRequestCommitDetail> iterator() {
|
||||||
return new PagedIterator<GHPullRequestCommitDetail>(root.retrieve().asIterator(
|
return new PagedIterator<GHPullRequestCommitDetail>(root.retrieve().asIterator(
|
||||||
String.format("%s/commits", getApiURL().getPath()),
|
String.format("%s/commits", getApiURL()),
|
||||||
GHPullRequestCommitDetail[].class)) {
|
GHPullRequestCommitDetail[].class)) {
|
||||||
@Override
|
@Override
|
||||||
protected void wrapUp(GHPullRequestCommitDetail[] page) {
|
protected void wrapUp(GHPullRequestCommitDetail[] page) {
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package org.kohsuke.github;
|
package org.kohsuke.github;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -8,6 +9,7 @@ import java.net.URL;
|
|||||||
* @author Michael Clarke
|
* @author Michael Clarke
|
||||||
*/
|
*/
|
||||||
public class GHRef {
|
public class GHRef {
|
||||||
|
/*package almost final*/ GitHub root;
|
||||||
|
|
||||||
private String ref, url;
|
private String ref, url;
|
||||||
private GHObject object;
|
private GHObject object;
|
||||||
@@ -33,6 +35,48 @@ public class GHRef {
|
|||||||
return object;
|
return object;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Updates this ref to the specified commit.
|
||||||
|
*
|
||||||
|
* @param sha
|
||||||
|
* The SHA1 value to set this reference to
|
||||||
|
*/
|
||||||
|
public void updateTo(String sha) throws IOException {
|
||||||
|
updateTo(sha, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Updates this ref to the specified commit.
|
||||||
|
*
|
||||||
|
* @param sha
|
||||||
|
* The SHA1 value to set this reference to
|
||||||
|
* @param force
|
||||||
|
* Whether or not to force this ref update.
|
||||||
|
*/
|
||||||
|
public void updateTo(String sha, Boolean force) throws IOException {
|
||||||
|
new Requester(root)
|
||||||
|
.with("sha", sha).with("force", force).method("PATCH").to(url, GHRef.class).wrap(root);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Deletes this ref from the repository using the GitHub API.
|
||||||
|
*/
|
||||||
|
public void delete() throws IOException {
|
||||||
|
new Requester(root).method("DELETE").to(url);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*package*/ GHRef wrap(GitHub root) {
|
||||||
|
this.root = root;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*package*/ static GHRef[] wrap(GHRef[] in, GitHub root) {
|
||||||
|
for (GHRef r : in) {
|
||||||
|
r.wrap(root);
|
||||||
|
}
|
||||||
|
return in;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public static class GHObject {
|
public static class GHObject {
|
||||||
private String type, sha, url;
|
private String type, sha, url;
|
||||||
|
|||||||
@@ -25,6 +25,7 @@ package org.kohsuke.github;
|
|||||||
|
|
||||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||||
import com.infradna.tool.bridge_method_injector.WithBridgeMethods;
|
import com.infradna.tool.bridge_method_injector.WithBridgeMethods;
|
||||||
|
import java.io.FileNotFoundException;
|
||||||
|
|
||||||
import javax.xml.bind.DatatypeConverter;
|
import javax.xml.bind.DatatypeConverter;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@@ -56,7 +57,7 @@ import static java.util.Arrays.*;
|
|||||||
public class GHRepository {
|
public class GHRepository {
|
||||||
/*package almost final*/ GitHub root;
|
/*package almost final*/ GitHub root;
|
||||||
|
|
||||||
private String description, homepage, name;
|
private String description, homepage, name, full_name;
|
||||||
private String url; // this is the API url
|
private String url; // this is the API url
|
||||||
private String html_url; // this is the UI
|
private String html_url; // this is the UI
|
||||||
private String git_url, ssh_url, clone_url, svn_url;
|
private String git_url, ssh_url, clone_url, svn_url;
|
||||||
@@ -130,6 +131,13 @@ public class GHRepository {
|
|||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Full repository name including the owner or organization. For example 'jenkinsci/jenkins' in case of http://github.com/jenkinsci/jenkins
|
||||||
|
*/
|
||||||
|
public String getFullName() {
|
||||||
|
return full_name;
|
||||||
|
}
|
||||||
|
|
||||||
public boolean hasPullAccess() {
|
public boolean hasPullAccess() {
|
||||||
return permissions!=null && permissions.pull;
|
return permissions!=null && permissions.pull;
|
||||||
}
|
}
|
||||||
@@ -205,7 +213,7 @@ public class GHRepository {
|
|||||||
*/
|
*/
|
||||||
public GHRef createRef(String name, String sha) throws IOException {
|
public GHRef createRef(String name, String sha) throws IOException {
|
||||||
return new Requester(root)
|
return new Requester(root)
|
||||||
.with("ref", name).with("sha", sha).method("POST").to(getApiTailUrl("git/refs"), GHRef.class);
|
.with("ref", name).with("sha", sha).method("POST").to(getApiTailUrl("git/refs"), GHRef.class).wrap(root);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -321,7 +329,32 @@ public class GHRepository {
|
|||||||
*/
|
*/
|
||||||
@WithBridgeMethods(Set.class)
|
@WithBridgeMethods(Set.class)
|
||||||
public GHPersonSet<GHUser> getCollaborators() throws IOException {
|
public GHPersonSet<GHUser> getCollaborators() throws IOException {
|
||||||
return new GHPersonSet<GHUser>(GHUser.wrap(root.retrieve().to("/repos/" + owner.login + "/" + name + "/collaborators", GHUser[].class),root));
|
return new GHPersonSet<GHUser>(listCollaborators().asList());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Lists up the collaborators on this repository.
|
||||||
|
*
|
||||||
|
* @return Users
|
||||||
|
* @throws IOException
|
||||||
|
*/
|
||||||
|
public PagedIterable<GHUser> listCollaborators() throws IOException {
|
||||||
|
return new PagedIterable<GHUser>() {
|
||||||
|
public PagedIterator<GHUser> iterator() {
|
||||||
|
|
||||||
|
return new PagedIterator<GHUser>(root.retrieve().asIterator("/repos/" + owner.login + "/" + name + "/collaborators", GHUser[].class)) {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void wrapUp(GHUser[] users) {
|
||||||
|
for (GHUser user : users) {
|
||||||
|
user.wrapUp(root);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -416,7 +449,11 @@ public class GHRepository {
|
|||||||
* Deletes this repository.
|
* Deletes this repository.
|
||||||
*/
|
*/
|
||||||
public void delete() throws IOException {
|
public void delete() throws IOException {
|
||||||
new Requester(root).method("DELETE").to("/repos/" + owner.login + "/" + name);
|
try {
|
||||||
|
new Requester(root).method("DELETE").to("/repos/" + owner.login + "/" + name);
|
||||||
|
} catch (FileNotFoundException x) {
|
||||||
|
throw (FileNotFoundException) new FileNotFoundException("Failed to delete " + owner.login + "/" + name + "; might not exist, or you might need the delete_repo scope in your token: http://stackoverflow.com/a/19327004/12916").initCause(x);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -549,17 +586,17 @@ public class GHRepository {
|
|||||||
* @throws IOException on failure communicating with GitHub
|
* @throws IOException on failure communicating with GitHub
|
||||||
*/
|
*/
|
||||||
public GHRef[] getRefs() throws IOException {
|
public GHRef[] getRefs() throws IOException {
|
||||||
return root.retrieve().to(String.format("/repos/%s/%s/git/refs", owner.login, name), GHRef[].class);
|
return GHRef.wrap(root.retrieve().to(String.format("/repos/%s/%s/git/refs", owner.login, name), GHRef[].class),root);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Retrienved all refs of the given type for the current GitHub repository.
|
* Retrieves all refs of the given type for the current GitHub repository.
|
||||||
* @param refType the type of reg to search for e.g. <tt>tags</tt> or <tt>commits</tt>
|
* @param refType the type of reg to search for e.g. <tt>tags</tt> or <tt>commits</tt>
|
||||||
* @return an array of all refs matching the request type
|
* @return an array of all refs matching the request type
|
||||||
* @throws IOException on failure communicating with GitHub, potentially due to an invalid ref type being requested
|
* @throws IOException on failure communicating with GitHub, potentially due to an invalid ref type being requested
|
||||||
*/
|
*/
|
||||||
public GHRef[] getRefs(String refType) throws IOException {
|
public GHRef[] getRefs(String refType) throws IOException {
|
||||||
return root.retrieve().to(String.format("/repos/%s/%s/git/refs/%s", owner.login, name, refType), GHRef[].class);
|
return GHRef.wrap(root.retrieve().to(String.format("/repos/%s/%s/git/refs/%s", owner.login, name, refType), GHRef[].class),root);
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* Retrive a ref of the given type for the current GitHub repository.
|
* Retrive a ref of the given type for the current GitHub repository.
|
||||||
@@ -572,7 +609,7 @@ public class GHRepository {
|
|||||||
* invalid ref type being requested
|
* invalid ref type being requested
|
||||||
*/
|
*/
|
||||||
public GHRef getRef(String refName) throws IOException {
|
public GHRef getRef(String refName) throws IOException {
|
||||||
return root.retrieve().to(String.format("/repos/%s/%s/git/refs/%s", owner.login, name, refName), GHRef.class);
|
return root.retrieve().to(String.format("/repos/%s/%s/git/refs/%s", owner.login, name, refName), GHRef.class).wrap(root);
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* Gets a commit object in this repository.
|
* Gets a commit object in this repository.
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ import java.util.TreeMap;
|
|||||||
public class GHTeam {
|
public class GHTeam {
|
||||||
private String name,permission;
|
private String name,permission;
|
||||||
private int id;
|
private int id;
|
||||||
|
private GHOrganization organization; // populated by GET /user/teams where Teams+Orgs are returned together
|
||||||
|
|
||||||
protected /*final*/ GHOrganization org;
|
protected /*final*/ GHOrganization org;
|
||||||
|
|
||||||
@@ -23,6 +24,11 @@ public class GHTeam {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*package*/ GHTeam wrapUp(GitHub root) { // auto-wrapUp when organization is known from GET /user/teams
|
||||||
|
this.organization.wrapUp(root);
|
||||||
|
return wrapUp(organization);
|
||||||
|
}
|
||||||
|
|
||||||
/*package*/ static GHTeam[] wrapUp(GHTeam[] teams, GHOrganization owner) {
|
/*package*/ static GHTeam[] wrapUp(GHTeam[] teams, GHOrganization owner) {
|
||||||
for (GHTeam t : teams) {
|
for (GHTeam t : teams) {
|
||||||
t.wrapUp(owner);
|
t.wrapUp(owner);
|
||||||
@@ -95,4 +101,8 @@ public class GHTeam {
|
|||||||
private String api(String tail) {
|
private String api(String tail) {
|
||||||
return "/teams/"+id+tail;
|
return "/teams/"+id+tail;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public GHOrganization getOrganization() {
|
||||||
|
return org;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,7 +23,8 @@
|
|||||||
*/
|
*/
|
||||||
package org.kohsuke.github;
|
package org.kohsuke.github;
|
||||||
|
|
||||||
import static com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility.*;
|
import static com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility.ANY;
|
||||||
|
import static com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility.NONE;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileInputStream;
|
import java.io.FileInputStream;
|
||||||
@@ -38,9 +39,11 @@ import java.util.Arrays;
|
|||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
import java.util.HashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
|
import java.util.Set;
|
||||||
import java.util.TimeZone;
|
import java.util.TimeZone;
|
||||||
|
|
||||||
import org.apache.commons.codec.binary.Base64;
|
import org.apache.commons.codec.binary.Base64;
|
||||||
@@ -280,6 +283,15 @@ public class GitHub {
|
|||||||
return u;
|
return u;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* clears all cached data in order for external changes (modifications and del
|
||||||
|
*/
|
||||||
|
public void refreshCache() {
|
||||||
|
users.clear();
|
||||||
|
orgs.clear();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Interns the given {@link GHUser}.
|
* Interns the given {@link GHUser}.
|
||||||
*/
|
*/
|
||||||
@@ -328,11 +340,32 @@ public class GitHub {
|
|||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets complete map of organizations/teams that current user belongs to.
|
||||||
|
*
|
||||||
|
* Leverages the new GitHub API /user/teams made available recently to
|
||||||
|
* get in a single call the complete set of organizations, teams and permissions
|
||||||
|
* in a single call.
|
||||||
|
*/
|
||||||
|
public Map<String, Set<GHTeam>> getMyTeams() throws IOException {
|
||||||
|
Map<String, Set<GHTeam>> allMyTeams = new HashMap<String, Set<GHTeam>>();
|
||||||
|
for (GHTeam team : retrieve().to("/user/teams", GHTeam[].class)) {
|
||||||
|
team.wrapUp(this);
|
||||||
|
String orgLogin = team.getOrganization().getLogin();
|
||||||
|
Set<GHTeam> teamsPerOrg = allMyTeams.get(orgLogin);
|
||||||
|
if (teamsPerOrg == null) {
|
||||||
|
teamsPerOrg = new HashSet<GHTeam>();
|
||||||
|
}
|
||||||
|
teamsPerOrg.add(team);
|
||||||
|
allMyTeams.put(orgLogin, teamsPerOrg);
|
||||||
|
}
|
||||||
|
return allMyTeams;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Public events visible to you. Equivalent of what's displayed on https://github.com/
|
* Public events visible to you. Equivalent of what's displayed on https://github.com/
|
||||||
*/
|
*/
|
||||||
public List<GHEventInfo> getEvents() throws IOException {
|
public List<GHEventInfo> getEvents() throws IOException {
|
||||||
// TODO: pagination
|
|
||||||
GHEventInfo[] events = retrieve().to("/events", GHEventInfo[].class);
|
GHEventInfo[] events = retrieve().to("/events", GHEventInfo[].class);
|
||||||
for (GHEventInfo e : events)
|
for (GHEventInfo e : events)
|
||||||
e.wrapUp(this);
|
e.wrapUp(this);
|
||||||
|
|||||||
@@ -32,6 +32,7 @@ import java.io.InputStreamReader;
|
|||||||
import java.io.InterruptedIOException;
|
import java.io.InterruptedIOException;
|
||||||
import java.io.Reader;
|
import java.io.Reader;
|
||||||
import java.io.UnsupportedEncodingException;
|
import java.io.UnsupportedEncodingException;
|
||||||
|
import java.lang.reflect.Array;
|
||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
import java.net.HttpURLConnection;
|
import java.net.HttpURLConnection;
|
||||||
import java.net.MalformedURLException;
|
import java.net.MalformedURLException;
|
||||||
@@ -47,10 +48,14 @@ import java.util.List;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.NoSuchElementException;
|
import java.util.NoSuchElementException;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
import java.util.regex.Matcher;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
import java.util.zip.GZIPInputStream;
|
import java.util.zip.GZIPInputStream;
|
||||||
|
|
||||||
import org.apache.commons.io.IOUtils;
|
import org.apache.commons.io.IOUtils;
|
||||||
|
|
||||||
|
import javax.net.ssl.HttpsURLConnection;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A builder pattern for making HTTP call and parsing its output.
|
* A builder pattern for making HTTP call and parsing its output.
|
||||||
*
|
*
|
||||||
@@ -177,7 +182,26 @@ class Requester {
|
|||||||
buildRequest(uc);
|
buildRequest(uc);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
return parse(uc,type,instance);
|
T result = parse(uc, type, instance);
|
||||||
|
if (type != null && type.isArray()) { // we might have to loop for pagination - done through recursion
|
||||||
|
final String links = uc.getHeaderField("link");
|
||||||
|
if (links != null && links.contains("rel=\"next\"")) {
|
||||||
|
Pattern nextLinkPattern = Pattern.compile(".*<(.*)>; rel=\"next\"");
|
||||||
|
Matcher nextLinkMatcher = nextLinkPattern.matcher(links);
|
||||||
|
if (nextLinkMatcher.find()) {
|
||||||
|
final String link = nextLinkMatcher.group(1);
|
||||||
|
T nextResult = _to(link, type, instance);
|
||||||
|
|
||||||
|
final int resultLength = Array.getLength(result);
|
||||||
|
final int nextResultLength = Array.getLength(nextResult);
|
||||||
|
T concatResult = (T) Array.newInstance(type.getComponentType(), resultLength + nextResultLength);
|
||||||
|
System.arraycopy(result, 0, concatResult, 0, resultLength);
|
||||||
|
System.arraycopy(nextResult, 0, concatResult, resultLength, nextResultLength);
|
||||||
|
result = concatResult;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
handleApiError(e,uc);
|
handleApiError(e,uc);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package org.kohsuke.github.extras;
|
package org.kohsuke.github.extras;
|
||||||
|
|
||||||
import com.squareup.okhttp.OkHttpClient;
|
import com.squareup.okhttp.OkHttpClient;
|
||||||
|
import com.squareup.okhttp.OkUrlFactory;
|
||||||
import org.kohsuke.github.HttpConnector;
|
import org.kohsuke.github.HttpConnector;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@@ -19,13 +20,13 @@ import java.net.URL;
|
|||||||
* @author Kohsuke Kawaguchi
|
* @author Kohsuke Kawaguchi
|
||||||
*/
|
*/
|
||||||
public class OkHttpConnector implements HttpConnector {
|
public class OkHttpConnector implements HttpConnector {
|
||||||
private final OkHttpClient client;
|
private final OkUrlFactory urlFactory;
|
||||||
|
|
||||||
public OkHttpConnector(OkHttpClient client) {
|
public OkHttpConnector(OkUrlFactory urlFactory) {
|
||||||
this.client = client;
|
this.urlFactory = urlFactory;
|
||||||
}
|
}
|
||||||
|
|
||||||
public HttpURLConnection connect(URL url) throws IOException {
|
public HttpURLConnection connect(URL url) throws IOException {
|
||||||
return client.open(url);
|
return urlFactory.open(url);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,19 +3,13 @@ package org.kohsuke.github;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collection;
|
import java.util.Date;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Map.Entry;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
|
||||||
import static org.junit.Assert.assertFalse;
|
|
||||||
import static org.junit.Assert.assertNotNull;
|
|
||||||
import static org.junit.Assert.assertSame;
|
|
||||||
import static org.junit.Assert.assertTrue;
|
|
||||||
import static org.junit.Assert.fail;
|
|
||||||
|
|
||||||
import org.junit.Assume;
|
import org.junit.Assume;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.kohsuke.github.GHCommit.File;
|
import org.kohsuke.github.GHCommit.File;
|
||||||
@@ -24,8 +18,6 @@ import org.kohsuke.github.GHOrganization.Permission;
|
|||||||
import com.google.common.base.Predicate;
|
import com.google.common.base.Predicate;
|
||||||
import com.google.common.collect.Iterables;
|
import com.google.common.collect.Iterables;
|
||||||
|
|
||||||
import java.util.Date;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Unit test for simple App.
|
* Unit test for simple App.
|
||||||
*/
|
*/
|
||||||
@@ -170,6 +162,35 @@ public class AppTest extends AbstractGitHubApiTestBase {
|
|||||||
System.out.println(org);
|
System.out.println(org);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testMyTeamsContainsAllMyOrganizations() throws IOException {
|
||||||
|
Map<String, Set<GHTeam>> teams = gitHub.getMyTeams();
|
||||||
|
Map<String, GHOrganization> myOrganizations = gitHub.getMyOrganizations();
|
||||||
|
assertEquals(teams.keySet(), myOrganizations.keySet());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testMyTeamsShouldIncludeMyself() throws IOException {
|
||||||
|
Map<String, Set<GHTeam>> teams = gitHub.getMyTeams();
|
||||||
|
for (Entry<String, Set<GHTeam>> teamsPerOrg : teams.entrySet()) {
|
||||||
|
String organizationName = teamsPerOrg.getKey();
|
||||||
|
for (GHTeam team : teamsPerOrg.getValue()) {
|
||||||
|
String teamName = team.getName();
|
||||||
|
assertTrue("Team " + teamName + " in organization " + organizationName
|
||||||
|
+ " does not contain myself",
|
||||||
|
shouldBelongToTeam(organizationName, teamName));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean shouldBelongToTeam(String organizationName, String teamName) throws IOException {
|
||||||
|
GHOrganization org = gitHub.getOrganization(organizationName);
|
||||||
|
assertNotNull(org);
|
||||||
|
GHTeam team = org.getTeamByName(teamName);
|
||||||
|
assertNotNull(team);
|
||||||
|
return team.hasMember(gitHub.getMyself());
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testFetchPullRequest() throws Exception {
|
public void testFetchPullRequest() throws Exception {
|
||||||
GHRepository r = gitHub.getOrganization("jenkinsci").getRepository("jenkins");
|
GHRepository r = gitHub.getOrganization("jenkinsci").getRepository("jenkins");
|
||||||
|
|||||||
91
src/test/java/org/kohsuke/github/RepositoryTest.java
Normal file
91
src/test/java/org/kohsuke/github/RepositoryTest.java
Normal file
@@ -0,0 +1,91 @@
|
|||||||
|
package org.kohsuke.github;
|
||||||
|
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.mockito.Mock;
|
||||||
|
import org.mockito.Mockito;
|
||||||
|
import org.mockito.MockitoAnnotations;
|
||||||
|
|
||||||
|
import java.util.Iterator;
|
||||||
|
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Luciano P. Sabenca (luciano.sabenca [at] movile [com] | lucianosabenca [at] gmail [dot] com
|
||||||
|
*/
|
||||||
|
public class RepositoryTest {
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
GitHub mockGitHub;
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
Iterator<GHUser[]> iterator;
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
GHRepository mockRepository;
|
||||||
|
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setUp() {
|
||||||
|
MockitoAnnotations.initMocks(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void listCollaborators() throws Exception {
|
||||||
|
GHUser user1 = new GHUser();
|
||||||
|
user1.login = "login1";
|
||||||
|
|
||||||
|
GHUser user2 = new GHUser();
|
||||||
|
user2.login = "login2";
|
||||||
|
|
||||||
|
|
||||||
|
when(iterator.hasNext()).thenReturn(true, false, true);
|
||||||
|
when(iterator.next()).thenReturn(new GHUser[]{user1}, new GHUser[]{user2});
|
||||||
|
|
||||||
|
Requester requester = Mockito.mock(Requester.class);
|
||||||
|
when(mockGitHub.retrieve()).thenReturn(requester);
|
||||||
|
|
||||||
|
|
||||||
|
when(requester.asIterator("/repos/*/*/collaborators",
|
||||||
|
GHUser[].class)).thenReturn(iterator, iterator);
|
||||||
|
|
||||||
|
|
||||||
|
PagedIterable<GHUser> pagedIterable = Mockito.mock(PagedIterable.class);
|
||||||
|
when(mockRepository.listCollaborators()).thenReturn(pagedIterable);
|
||||||
|
|
||||||
|
PagedIterator<GHUser> userPagedIterator = new PagedIterator<GHUser>(iterator) {
|
||||||
|
@Override
|
||||||
|
protected void wrapUp(GHUser[] page) {
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
|
PagedIterator<GHUser> userPagedIterator2 = new PagedIterator<GHUser>(iterator) {
|
||||||
|
@Override
|
||||||
|
protected void wrapUp(GHUser[] page) {
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
when(pagedIterable.iterator()).thenReturn(userPagedIterator, userPagedIterator2);
|
||||||
|
|
||||||
|
Iterator<GHUser> returnIterator1 = mockRepository.listCollaborators().iterator();
|
||||||
|
|
||||||
|
|
||||||
|
Assert.assertTrue(returnIterator1.hasNext());
|
||||||
|
GHUser user = returnIterator1.next();
|
||||||
|
Assert.assertEquals(user, user1);
|
||||||
|
Assert.assertFalse(returnIterator1.hasNext());
|
||||||
|
|
||||||
|
|
||||||
|
Iterator returnIterator2 = mockRepository.listCollaborators().iterator();
|
||||||
|
|
||||||
|
|
||||||
|
Assert.assertTrue(returnIterator2.hasNext());
|
||||||
|
user = returnIterator1.next();
|
||||||
|
Assert.assertEquals(user, user2);
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user