mirror of
https://github.com/jlengrand/github-api.git
synced 2026-03-14 08:21:22 +00:00
Compare commits
36 Commits
github-api
...
github-api
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7e49946bed | ||
|
|
1e81ab1017 | ||
|
|
0b92fa5615 | ||
|
|
590f7ba511 | ||
|
|
449909b0e8 | ||
|
|
40780525f8 | ||
|
|
0e3707d1c3 | ||
|
|
8ddbef093b | ||
|
|
4ccdfccdf0 | ||
|
|
3c8aa0c630 | ||
|
|
ba519f996a | ||
|
|
62d9b92e6e | ||
|
|
d2c909584d | ||
|
|
219916eb14 | ||
|
|
0a37ac901f | ||
|
|
4586baea27 | ||
|
|
d83961e85b | ||
|
|
7e35716ff0 | ||
|
|
d0cf1ac605 | ||
|
|
9a19d07ab8 | ||
|
|
922be0b164 | ||
|
|
d0b8e2e37e | ||
|
|
e3d6e08b6a | ||
|
|
16a6623095 | ||
|
|
7bf31ca149 | ||
|
|
030f2360ca | ||
|
|
08eafc4214 | ||
|
|
134ece9385 | ||
|
|
3e4b06e959 | ||
|
|
3e3c6f70ba | ||
|
|
3097378a10 | ||
|
|
53f7a0f78a | ||
|
|
45a6841772 | ||
|
|
80b93a6e33 | ||
|
|
6aaab641be | ||
|
|
4999490c06 |
19
pom.xml
19
pom.xml
@@ -3,11 +3,11 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<groupId>org.kohsuke</groupId>
|
<groupId>org.kohsuke</groupId>
|
||||||
<artifactId>pom</artifactId>
|
<artifactId>pom</artifactId>
|
||||||
<version>6</version>
|
<version>9</version>
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
<artifactId>github-api</artifactId>
|
<artifactId>github-api</artifactId>
|
||||||
<version>1.53</version>
|
<version>1.57</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.53</tag>
|
<tag>HEAD</tag>
|
||||||
</scm>
|
</scm>
|
||||||
|
|
||||||
<distributionManagement>
|
<distributionManagement>
|
||||||
@@ -35,7 +35,7 @@
|
|||||||
<plugin>
|
<plugin>
|
||||||
<groupId>com.infradna.tool</groupId>
|
<groupId>com.infradna.tool</groupId>
|
||||||
<artifactId>bridge-method-injector</artifactId>
|
<artifactId>bridge-method-injector</artifactId>
|
||||||
<version>1.8</version>
|
<version>1.12</version>
|
||||||
<executions>
|
<executions>
|
||||||
<execution>
|
<execution>
|
||||||
<goals>
|
<goals>
|
||||||
@@ -77,7 +77,7 @@
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.infradna.tool</groupId>
|
<groupId>com.infradna.tool</groupId>
|
||||||
<artifactId>bridge-method-annotation</artifactId>
|
<artifactId>bridge-method-annotation</artifactId>
|
||||||
<version>1.8</version>
|
<version>1.12</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.kohsuke.stapler</groupId>
|
<groupId>org.kohsuke.stapler</groupId>
|
||||||
@@ -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>
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ public class GHCommitStatus {
|
|||||||
String target_url,description;
|
String target_url,description;
|
||||||
int id;
|
int id;
|
||||||
String url;
|
String url;
|
||||||
|
String context;
|
||||||
GHUser creator;
|
GHUser creator;
|
||||||
|
|
||||||
private GitHub root;
|
private GitHub root;
|
||||||
@@ -69,4 +70,8 @@ public class GHCommitStatus {
|
|||||||
public GHUser getCreator() {
|
public GHUser getCreator() {
|
||||||
return creator;
|
return creator;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getContext() {
|
||||||
|
return context;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
package org.kohsuke.github;
|
package org.kohsuke.github;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
import javax.xml.bind.DatatypeConverter;
|
import javax.xml.bind.DatatypeConverter;
|
||||||
|
|
||||||
@@ -8,7 +10,9 @@ import javax.xml.bind.DatatypeConverter;
|
|||||||
* A Content of a repository.
|
* A Content of a repository.
|
||||||
*
|
*
|
||||||
* @author Alexandre COLLIGNON
|
* @author Alexandre COLLIGNON
|
||||||
|
* @see GHRepository#getFileContent(String)
|
||||||
*/
|
*/
|
||||||
|
@SuppressWarnings({"UnusedDeclaration"})
|
||||||
public class GHContent {
|
public class GHContent {
|
||||||
private GHRepository owner;
|
private GHRepository owner;
|
||||||
|
|
||||||
@@ -105,6 +109,25 @@ public class GHContent {
|
|||||||
return "dir".equals(type);
|
return "dir".equals(type);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* List immediate children of this directory.
|
||||||
|
*/
|
||||||
|
public PagedIterable<GHContent> listDirectoryContent() throws IOException {
|
||||||
|
if (!isDirectory())
|
||||||
|
throw new IllegalStateException(path+" is not a directory");
|
||||||
|
|
||||||
|
return new PagedIterable<GHContent>() {
|
||||||
|
public PagedIterator<GHContent> iterator() {
|
||||||
|
return new PagedIterator<GHContent>(owner.root.retrieve().asIterator(url, GHContent[].class)) {
|
||||||
|
@Override
|
||||||
|
protected void wrapUp(GHContent[] page) {
|
||||||
|
GHContent.wrap(page,owner);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
public GHContentUpdateResponse update(String newContent, String commitMessage) throws IOException {
|
public GHContentUpdateResponse update(String newContent, String commitMessage) throws IOException {
|
||||||
return update(newContent, commitMessage, null);
|
return update(newContent, commitMessage, null);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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;
|
||||||
|
|
||||||
|
|||||||
46
src/main/java/org/kohsuke/github/GHDeployKey.java
Normal file
46
src/main/java/org/kohsuke/github/GHDeployKey.java
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
package org.kohsuke.github;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import org.apache.commons.lang.builder.ToStringBuilder;
|
||||||
|
|
||||||
|
public class GHDeployKey {
|
||||||
|
|
||||||
|
protected String url, key, title;
|
||||||
|
protected boolean verified;
|
||||||
|
protected int id;
|
||||||
|
private GHRepository owner;
|
||||||
|
|
||||||
|
public int getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getKey() {
|
||||||
|
return key;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getTitle() {
|
||||||
|
return title;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getUrl() {
|
||||||
|
return url;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isVerified() {
|
||||||
|
return verified;
|
||||||
|
}
|
||||||
|
|
||||||
|
public GHDeployKey wrap(GHRepository repo) {
|
||||||
|
this.owner = repo;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String toString() {
|
||||||
|
return new ToStringBuilder(this).append("title",title).append("id",id).append("key",key).toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void delete() throws IOException {
|
||||||
|
new Requester(owner.root).method("DELETE").to(String.format("/repos/%s/%s/keys/%d", owner.getOwnerName(), owner.getName(), id));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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;
|
||||||
@@ -321,7 +322,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 +442,11 @@ public class GHRepository {
|
|||||||
* Deletes this repository.
|
* Deletes this repository.
|
||||||
*/
|
*/
|
||||||
public void delete() throws IOException {
|
public void delete() throws IOException {
|
||||||
|
try {
|
||||||
new Requester(root).method("DELETE").to("/repos/" + owner.login + "/" + name);
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -561,7 +591,19 @@ public class GHRepository {
|
|||||||
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 root.retrieve().to(String.format("/repos/%s/%s/git/refs/%s", owner.login, name, refType), GHRef[].class);
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
|
* Retrive a ref of the given type for the current GitHub repository.
|
||||||
|
*
|
||||||
|
* @param refName
|
||||||
|
* eg: heads/branch
|
||||||
|
* @return refs matching the request type
|
||||||
|
* @throws IOException
|
||||||
|
* on failure communicating with GitHub, potentially due to an
|
||||||
|
* invalid ref type being requested
|
||||||
|
*/
|
||||||
|
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);
|
||||||
|
}
|
||||||
/**
|
/**
|
||||||
* Gets a commit object in this repository.
|
* Gets a commit object in this repository.
|
||||||
*/
|
*/
|
||||||
@@ -646,15 +688,25 @@ public class GHRepository {
|
|||||||
* Optional parameter that points to the URL that has more details.
|
* Optional parameter that points to the URL that has more details.
|
||||||
* @param description
|
* @param description
|
||||||
* Optional short description.
|
* Optional short description.
|
||||||
|
* @param context
|
||||||
|
* Optinal commit status context.
|
||||||
*/
|
*/
|
||||||
public GHCommitStatus createCommitStatus(String sha1, GHCommitState state, String targetUrl, String description) throws IOException {
|
public GHCommitStatus createCommitStatus(String sha1, GHCommitState state, String targetUrl, String description, String context) throws IOException {
|
||||||
return new Requester(root)
|
return new Requester(root)
|
||||||
.with("state", state.name().toLowerCase(Locale.ENGLISH))
|
.with("state", state.name().toLowerCase(Locale.ENGLISH))
|
||||||
.with("target_url", targetUrl)
|
.with("target_url", targetUrl)
|
||||||
.with("description", description)
|
.with("description", description)
|
||||||
|
.with("context", context)
|
||||||
.to(String.format("/repos/%s/%s/statuses/%s",owner.login,this.name,sha1),GHCommitStatus.class).wrapUp(root);
|
.to(String.format("/repos/%s/%s/statuses/%s",owner.login,this.name,sha1),GHCommitStatus.class).wrapUp(root);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @see {@link #createCommitStatus(String, GHCommitState,String,String,String) createCommitStatus}
|
||||||
|
*/
|
||||||
|
public GHCommitStatus createCommitStatus(String sha1, GHCommitState state, String targetUrl, String description) throws IOException {
|
||||||
|
return createCommitStatus(sha1, state, targetUrl, description,null);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Lists repository events.
|
* Lists repository events.
|
||||||
*/
|
*/
|
||||||
@@ -908,6 +960,22 @@ public class GHRepository {
|
|||||||
.with("title", title).with("description", description).method("POST").to(getApiTailUrl("milestones"), GHMilestone.class).wrap(this);
|
.with("title", title).with("description", description).method("POST").to(getApiTailUrl("milestones"), GHMilestone.class).wrap(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public GHDeployKey addDeployKey(String title,String key) throws IOException {
|
||||||
|
return new Requester(root)
|
||||||
|
.with("title", title).with("key", key).method("POST").to(getApiTailUrl("keys"), GHDeployKey.class).wrap(this);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<GHDeployKey> getDeployKeys() throws IOException{
|
||||||
|
List<GHDeployKey> list = new ArrayList<GHDeployKey>(Arrays.asList(
|
||||||
|
root.retrieve().to(String.format("/repos/%s/%s/keys", owner.login, name), GHDeployKey[].class)));
|
||||||
|
for (GHDeployKey h : list)
|
||||||
|
h.wrap(this);
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "Repository:"+owner.login+":"+name;
|
return "Repository:"+owner.login+":"+name;
|
||||||
|
|||||||
@@ -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;
|
||||||
@@ -328,6 +331,28 @@ 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/
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -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,24 +3,20 @@ 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.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;
|
||||||
import org.kohsuke.github.GHOrganization.Permission;
|
import org.kohsuke.github.GHOrganization.Permission;
|
||||||
|
|
||||||
import java.util.Date;
|
import com.google.common.base.Predicate;
|
||||||
|
import com.google.common.collect.Iterables;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Unit test for simple App.
|
* Unit test for simple App.
|
||||||
@@ -166,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");
|
||||||
@@ -529,6 +554,52 @@ public class AppTest extends AbstractGitHubApiTestBase {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testRef() throws IOException {
|
||||||
|
GHRef masterRef = gitHub.getRepository("jenkinsci/jenkins").getRef("heads/master");
|
||||||
|
assertEquals("https://api.github.com/repos/jenkinsci/jenkins/git/refs/heads/master", masterRef.getUrl().toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void directoryListing() throws IOException {
|
||||||
|
List<GHContent> children = gitHub.getRepository("jenkinsci/jenkins").getDirectoryContent("core");
|
||||||
|
for (GHContent c : children) {
|
||||||
|
System.out.println(c.getName());
|
||||||
|
if (c.isDirectory()) {
|
||||||
|
for (GHContent d : c.listDirectoryContent()) {
|
||||||
|
System.out.println(" "+d.getName());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testAddDeployKey() throws IOException {
|
||||||
|
GHRepository myRepository = Iterables.get(gitHub.getMyself().getRepositories().values(),0);
|
||||||
|
final GHDeployKey newDeployKey = myRepository.addDeployKey("test", "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDUt0RAycC5cS42JKh6SecfFZBR1RrF+2hYMctz4mk74/arBE+wFb7fnSHGzdGKX2h5CFOWODifRCJVhB7hlVxodxe+QkQQYAEL/x1WVCJnGgTGQGOrhOMj95V3UE5pQKhsKD608C+u5tSofcWXLToP1/wZ7U4/AHjqYi08OLsWToHCax55TZkvdt2jo0hbIoYU+XI9Q8Uv4ONDN1oabiOdgeKi8+crvHAuvNleiBhWVBzFh8KdfzaH5uNdw7ihhFjEd1vzqACsjCINCjdMfzl6jD9ExuWuE92nZJnucls2cEoNC6k2aPmrZDg9hA32FXVpyseY+bDUWFU6LO2LG6PB kohsuke@atlas");
|
||||||
|
try {
|
||||||
|
assertNotNull(newDeployKey.getId());
|
||||||
|
|
||||||
|
GHDeployKey k = Iterables.find(myRepository.getDeployKeys(), new Predicate<GHDeployKey>() {
|
||||||
|
public boolean apply(GHDeployKey deployKey) {
|
||||||
|
return newDeployKey.getId() == deployKey.getId();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
assertNotNull(k);
|
||||||
|
} finally {
|
||||||
|
newDeployKey.delete();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testCommitStatusContext() throws IOException {
|
||||||
|
GHRepository myRepository = Iterables.get(gitHub.getMyself().getRepositories().values(), 0);
|
||||||
|
GHRef masterRef = myRepository.getRef("heads/master");
|
||||||
|
GHCommitStatus commitStatus = myRepository.createCommitStatus(masterRef.getObject().getSha(), GHCommitState.SUCCESS, "http://www.example.com", "test", "test/context");
|
||||||
|
assertEquals("test/context", commitStatus.getContext());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
private void kohsuke() {
|
private void kohsuke() {
|
||||||
String login = getUser().getLogin();
|
String login = getUser().getLogin();
|
||||||
Assume.assumeTrue(login.equals("kohsuke") || login.equals("kohsuke2"));
|
Assume.assumeTrue(login.equals("kohsuke") || login.equals("kohsuke2"));
|
||||||
|
|||||||
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