mirror of
https://github.com/jlengrand/github-api.git
synced 2026-03-13 00:11:22 +00:00
Compare commits
21 Commits
github-api
...
github-api
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
67280951ff | ||
|
|
c3a9f6f9f5 | ||
|
|
e631e46dd1 | ||
|
|
15163ffde0 | ||
|
|
b898284821 | ||
|
|
3bb7eb2e03 | ||
|
|
11fcb9d456 | ||
|
|
29f826448a | ||
|
|
a8cf4a7120 | ||
|
|
60dce94a47 | ||
|
|
c965b9cc24 | ||
|
|
762a32eb6d | ||
|
|
541dac1aee | ||
|
|
e2e2329301 | ||
|
|
9afad71b0f | ||
|
|
7bbe0f7e8a | ||
|
|
d90adfa98e | ||
|
|
1dbcc4b776 | ||
|
|
18696fca2d | ||
|
|
c40100b6da | ||
|
|
6396818740 |
6
pom.xml
6
pom.xml
@@ -3,11 +3,11 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<groupId>org.kohsuke</groupId>
|
<groupId>org.kohsuke</groupId>
|
||||||
<artifactId>pom</artifactId>
|
<artifactId>pom</artifactId>
|
||||||
<version>10</version>
|
<version>12</version>
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
<artifactId>github-api</artifactId>
|
<artifactId>github-api</artifactId>
|
||||||
<version>1.60-SNAPSHOT</version>
|
<version>1.62</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>HEAD</tag>
|
<tag>github-api-1.62</tag>
|
||||||
</scm>
|
</scm>
|
||||||
|
|
||||||
<distributionManagement>
|
<distributionManagement>
|
||||||
|
|||||||
@@ -120,6 +120,7 @@ public abstract class GHEventPayload {
|
|||||||
private String ref;
|
private String ref;
|
||||||
private int size;
|
private int size;
|
||||||
private List<PushCommit> commits;
|
private List<PushCommit> commits;
|
||||||
|
private GHRepository repository;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The SHA of the HEAD commit on the repository
|
* The SHA of the HEAD commit on the repository
|
||||||
@@ -158,6 +159,16 @@ public abstract class GHEventPayload {
|
|||||||
return commits;
|
return commits;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public GHRepository getRepository() {
|
||||||
|
return repository;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
void wrapUp(GitHub root) {
|
||||||
|
if (repository!=null)
|
||||||
|
repository.wrap(root);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Commit in a push
|
* Commit in a push
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ import java.util.Map.Entry;
|
|||||||
* @see GHUser#listGists()
|
* @see GHUser#listGists()
|
||||||
* @see GitHub#getGist(String)
|
* @see GitHub#getGist(String)
|
||||||
* @see GitHub#createGist()
|
* @see GitHub#createGist()
|
||||||
|
* @see <a href="https://developer.github.com/v3/gists/">documentation</a>
|
||||||
*/
|
*/
|
||||||
public class GHGist extends GHObject {
|
public class GHGist extends GHObject {
|
||||||
/*package almost final*/ GHUser owner;
|
/*package almost final*/ GHUser owner;
|
||||||
|
|||||||
@@ -52,29 +52,17 @@ public class GHIssue extends GHObject {
|
|||||||
protected String closed_at;
|
protected String closed_at;
|
||||||
protected int comments;
|
protected int comments;
|
||||||
protected String body;
|
protected String body;
|
||||||
protected List<Label> labels;
|
protected List<GHLabel> labels;
|
||||||
protected GHUser user;
|
protected GHUser user;
|
||||||
protected String title, html_url;
|
protected String title, html_url;
|
||||||
protected GHIssue.PullRequest pull_request;
|
protected GHIssue.PullRequest pull_request;
|
||||||
protected GHMilestone milestone;
|
protected GHMilestone milestone;
|
||||||
protected GHUser closed_by;
|
protected GHUser closed_by;
|
||||||
|
|
||||||
public static class Label {
|
/**
|
||||||
private String url;
|
* @deprecated use {@link GHLabel}
|
||||||
private String name;
|
*/
|
||||||
private String color;
|
public static class Label extends GHLabel {
|
||||||
|
|
||||||
public String getUrl() {
|
|
||||||
return url;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getName() {
|
|
||||||
return name;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getColor() {
|
|
||||||
return color;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*package*/ GHIssue wrap(GHRepository owner) {
|
/*package*/ GHIssue wrap(GHRepository owner) {
|
||||||
@@ -134,9 +122,9 @@ public class GHIssue extends GHObject {
|
|||||||
return Enum.valueOf(GHIssueState.class, state.toUpperCase(Locale.ENGLISH));
|
return Enum.valueOf(GHIssueState.class, state.toUpperCase(Locale.ENGLISH));
|
||||||
}
|
}
|
||||||
|
|
||||||
public Collection<Label> getLabels() throws IOException {
|
public Collection<GHLabel> getLabels() throws IOException {
|
||||||
if(labels == null){
|
if(labels == null){
|
||||||
return Collections.EMPTY_LIST;
|
return Collections.emptyList();
|
||||||
}
|
}
|
||||||
return Collections.unmodifiableList(labels);
|
return Collections.unmodifiableList(labels);
|
||||||
}
|
}
|
||||||
@@ -237,7 +225,15 @@ public class GHIssue extends GHObject {
|
|||||||
public GHUser getUser() {
|
public GHUser getUser() {
|
||||||
return user;
|
return user;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reports who has closed the issue.
|
||||||
|
*
|
||||||
|
* <p>
|
||||||
|
* Note that GitHub doesn't always seem to report this information
|
||||||
|
* even for an issue that's already closed. See
|
||||||
|
* https://github.com/kohsuke/github-api/issues/60.
|
||||||
|
*/
|
||||||
public GHUser getClosedBy() {
|
public GHUser getClosedBy() {
|
||||||
if(!"closed".equals(state)) return null;
|
if(!"closed".equals(state)) return null;
|
||||||
if(closed_by != null) return closed_by;
|
if(closed_by != null) return closed_by;
|
||||||
@@ -250,10 +246,17 @@ public class GHIssue extends GHObject {
|
|||||||
return comments;
|
return comments;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns non-null if this issue is a shadow of a pull request.
|
||||||
|
*/
|
||||||
public PullRequest getPullRequest() {
|
public PullRequest getPullRequest() {
|
||||||
return pull_request;
|
return pull_request;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean isPullRequest() {
|
||||||
|
return pull_request!=null;
|
||||||
|
}
|
||||||
|
|
||||||
public GHMilestone getMilestone() {
|
public GHMilestone getMilestone() {
|
||||||
return milestone;
|
return milestone;
|
||||||
}
|
}
|
||||||
|
|||||||
37
src/main/java/org/kohsuke/github/GHLabel.java
Normal file
37
src/main/java/org/kohsuke/github/GHLabel.java
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
package org.kohsuke.github;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Kohsuke Kawaguchi
|
||||||
|
* @see GHIssue#getLabels()
|
||||||
|
* @see GHRepository#listLabels()
|
||||||
|
*/
|
||||||
|
public class GHLabel {
|
||||||
|
private String url, name, color;
|
||||||
|
private GHRepository repo;
|
||||||
|
|
||||||
|
public String getUrl() {
|
||||||
|
return url;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Color code without leading '#', such as 'f29513'
|
||||||
|
*/
|
||||||
|
public String getColor() {
|
||||||
|
return color;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*package*/ GHLabel wrapUp(GHRepository repo) {
|
||||||
|
this.repo = repo;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void delete() throws IOException {
|
||||||
|
repo.root.retrieve().method("DELETE").to(url);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -36,7 +36,7 @@ public abstract class GHPerson extends GHObject {
|
|||||||
*
|
*
|
||||||
* Depending on the original API call where this object is created, it may not contain everything.
|
* Depending on the original API call where this object is created, it may not contain everything.
|
||||||
*/
|
*/
|
||||||
protected void populate() throws IOException {
|
protected synchronized void populate() throws IOException {
|
||||||
if (created_at!=null) return; // already populated
|
if (created_at!=null) return; // already populated
|
||||||
|
|
||||||
root.retrieve().to(url, this);
|
root.retrieve().to(url, this);
|
||||||
|
|||||||
@@ -127,7 +127,7 @@ public class GHPullRequest extends GHIssue {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Collection<Label> getLabels() throws IOException {
|
public Collection<GHLabel> getLabels() throws IOException {
|
||||||
fetchIssue();
|
fetchIssue();
|
||||||
return super.getLabels();
|
return super.getLabels();
|
||||||
}
|
}
|
||||||
@@ -194,7 +194,7 @@ public class GHPullRequest extends GHIssue {
|
|||||||
private void populate() throws IOException {
|
private void populate() throws IOException {
|
||||||
if (merged_by!=null) return; // already populated
|
if (merged_by!=null) return; // already populated
|
||||||
|
|
||||||
root.retrieve().to(url, this);
|
root.retrieve().to(url, this).wrapUp(owner);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -120,7 +120,7 @@ public class GHRelease extends GHObject {
|
|||||||
public GHAsset uploadAsset(File file, String contentType) throws IOException {
|
public GHAsset uploadAsset(File file, String contentType) throws IOException {
|
||||||
Requester builder = new Requester(owner.root);
|
Requester builder = new Requester(owner.root);
|
||||||
|
|
||||||
String url = format("https://uploads.github.com%sreleases/%d/assets?name=%s",
|
String url = format("https://uploads.github.com%s/releases/%d/assets?name=%s",
|
||||||
owner.getApiTailUrl(""), getId(), file.getName());
|
owner.getApiTailUrl(""), getId(), file.getName());
|
||||||
return builder.contentType(contentType)
|
return builder.contentType(contentType)
|
||||||
.with(new FileInputStream(file))
|
.with(new FileInputStream(file))
|
||||||
|
|||||||
@@ -195,7 +195,7 @@ public class GHRepository extends GHObject {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public GHIssue getIssue(int id) throws IOException {
|
public GHIssue getIssue(int id) throws IOException {
|
||||||
return root.retrieve().to("/repos/" + owner.login + "/" + name + "/issues/" + id, GHIssue.class).wrap(this);
|
return root.retrieve().to(getApiTailUrl("issues/" + id), GHIssue.class).wrap(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
public GHIssueBuilder createIssue(String title) {
|
public GHIssueBuilder createIssue(String title) {
|
||||||
@@ -208,8 +208,8 @@ public class GHRepository extends GHObject {
|
|||||||
|
|
||||||
public List<GHIssue> getIssues(GHIssueState state, GHMilestone milestone) throws IOException {
|
public List<GHIssue> getIssues(GHIssueState state, GHMilestone milestone) throws IOException {
|
||||||
return Arrays.asList(GHIssue.wrap(root.retrieve()
|
return Arrays.asList(GHIssue.wrap(root.retrieve()
|
||||||
.to(String.format("/repos/%s/%s/issues?state=%s&milestone=%s", owner.login, name,
|
.to(getApiTailUrl(String.format("issues?state=%s&milestone=%s",
|
||||||
state.toString().toLowerCase(), milestone == null ? "none" : "" + milestone.getNumber()),
|
state.toString().toLowerCase(), milestone == null ? "none" : "" + milestone.getNumber())),
|
||||||
GHIssue[].class
|
GHIssue[].class
|
||||||
), this));
|
), this));
|
||||||
}
|
}
|
||||||
@@ -285,7 +285,7 @@ public class GHRepository extends GHObject {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
protected String getOwnerName() {
|
public String getOwnerName() {
|
||||||
return owner.login;
|
return owner.login;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -371,7 +371,7 @@ public class GHRepository extends GHObject {
|
|||||||
return new PagedIterable<GHUser>() {
|
return new PagedIterable<GHUser>() {
|
||||||
public PagedIterator<GHUser> iterator() {
|
public PagedIterator<GHUser> iterator() {
|
||||||
|
|
||||||
return new PagedIterator<GHUser>(root.retrieve().asIterator("/repos/" + owner.login + "/" + name + "/collaborators", GHUser[].class)) {
|
return new PagedIterator<GHUser>(root.retrieve().asIterator(getApiTailUrl("collaborators"), GHUser[].class)) {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void wrapUp(GHUser[] users) {
|
protected void wrapUp(GHUser[] users) {
|
||||||
@@ -392,7 +392,7 @@ public class GHRepository extends GHObject {
|
|||||||
*/
|
*/
|
||||||
public Set<String> getCollaboratorNames() throws IOException {
|
public Set<String> getCollaboratorNames() throws IOException {
|
||||||
Set<String> r = new HashSet<String>();
|
Set<String> r = new HashSet<String>();
|
||||||
for (GHUser u : GHUser.wrap(root.retrieve().to("/repos/" + owner.login + "/" + name + "/collaborators", GHUser[].class),root))
|
for (GHUser u : GHUser.wrap(root.retrieve().to(getApiTailUrl("collaborators"), GHUser[].class),root))
|
||||||
r.add(u.login);
|
r.add(u.login);
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
@@ -401,7 +401,7 @@ public class GHRepository extends GHObject {
|
|||||||
* If this repository belongs to an organization, return a set of teams.
|
* If this repository belongs to an organization, return a set of teams.
|
||||||
*/
|
*/
|
||||||
public Set<GHTeam> getTeams() throws IOException {
|
public Set<GHTeam> getTeams() throws IOException {
|
||||||
return Collections.unmodifiableSet(new HashSet<GHTeam>(Arrays.asList(GHTeam.wrapUp(root.retrieve().to("/repos/" + owner.login + "/" + name + "/teams", GHTeam[].class), root.getOrganization(owner.login)))));
|
return Collections.unmodifiableSet(new HashSet<GHTeam>(Arrays.asList(GHTeam.wrapUp(root.retrieve().to(getApiTailUrl("teams"), GHTeam[].class), root.getOrganization(owner.login)))));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addCollaborators(GHUser... users) throws IOException {
|
public void addCollaborators(GHUser... users) throws IOException {
|
||||||
@@ -423,7 +423,7 @@ public class GHRepository extends GHObject {
|
|||||||
private void modifyCollaborators(Collection<GHUser> users, String method) throws IOException {
|
private void modifyCollaborators(Collection<GHUser> users, String method) throws IOException {
|
||||||
verifyMine();
|
verifyMine();
|
||||||
for (GHUser user : users) {
|
for (GHUser user : users) {
|
||||||
new Requester(root).method(method).to("/repos/" + owner.login + "/" + name + "/collaborators/" + user.getLogin());
|
new Requester(root).method(method).to(getApiTailUrl("collaborators/" + user.getLogin()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -431,14 +431,14 @@ public class GHRepository extends GHObject {
|
|||||||
Map<String, String> config = new HashMap<String, String>();
|
Map<String, String> config = new HashMap<String, String>();
|
||||||
config.put("address", address);
|
config.put("address", address);
|
||||||
new Requester(root).method("POST").with("name", "email").with("config", config).with("active", "true")
|
new Requester(root).method("POST").with("name", "email").with("config", config).with("active", "true")
|
||||||
.to(String.format("/repos/%s/%s/hooks", owner.login, name));
|
.to(getApiTailUrl("hooks"));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void edit(String key, String value) throws IOException {
|
private void edit(String key, String value) throws IOException {
|
||||||
Requester requester = new Requester(root);
|
Requester requester = new Requester(root);
|
||||||
if (!key.equals("name"))
|
if (!key.equals("name"))
|
||||||
requester.with("name", name); // even when we don't change the name, we need to send it in
|
requester.with("name", name); // even when we don't change the name, we need to send it in
|
||||||
requester.with(key, value).method("PATCH").to("/repos/" + owner.login + "/" + name);
|
requester.with(key, value).method("PATCH").to(getApiTailUrl(""));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -479,7 +479,7 @@ public class GHRepository extends GHObject {
|
|||||||
*/
|
*/
|
||||||
public void delete() throws IOException {
|
public void delete() throws IOException {
|
||||||
try {
|
try {
|
||||||
new Requester(root).method("DELETE").to("/repos/" + owner.login + "/" + name);
|
new Requester(root).method("DELETE").to(getApiTailUrl(""));
|
||||||
} catch (FileNotFoundException x) {
|
} 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);
|
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);
|
||||||
}
|
}
|
||||||
@@ -492,7 +492,7 @@ public class GHRepository extends GHObject {
|
|||||||
* Newly forked repository that belong to you.
|
* Newly forked repository that belong to you.
|
||||||
*/
|
*/
|
||||||
public GHRepository fork() throws IOException {
|
public GHRepository fork() throws IOException {
|
||||||
return new Requester(root).method("POST").to("/repos/" + owner.login + "/" + name + "/forks", GHRepository.class).wrap(root);
|
return new Requester(root).method("POST").to(getApiTailUrl("forks"), GHRepository.class).wrap(root);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -502,7 +502,7 @@ public class GHRepository extends GHObject {
|
|||||||
* Newly forked repository that belong to you.
|
* Newly forked repository that belong to you.
|
||||||
*/
|
*/
|
||||||
public GHRepository forkTo(GHOrganization org) throws IOException {
|
public GHRepository forkTo(GHOrganization org) throws IOException {
|
||||||
new Requester(root).to(String.format("/repos/%s/%s/forks?org=%s", owner.login, name, org.getLogin()));
|
new Requester(root).to(getApiTailUrl("forks?org="+org.getLogin()));
|
||||||
|
|
||||||
// this API is asynchronous. we need to wait for a bit
|
// this API is asynchronous. we need to wait for a bit
|
||||||
for (int i=0; i<10; i++) {
|
for (int i=0; i<10; i++) {
|
||||||
@@ -521,7 +521,7 @@ public class GHRepository extends GHObject {
|
|||||||
* Retrieves a specified pull request.
|
* Retrieves a specified pull request.
|
||||||
*/
|
*/
|
||||||
public GHPullRequest getPullRequest(int i) throws IOException {
|
public GHPullRequest getPullRequest(int i) throws IOException {
|
||||||
return root.retrieve().to("/repos/" + owner.login + '/' + name + "/pulls/" + i, GHPullRequest.class).wrapUp(this);
|
return root.retrieve().to(getApiTailUrl("pulls/" + i), GHPullRequest.class).wrapUp(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -539,7 +539,7 @@ public class GHRepository extends GHObject {
|
|||||||
public PagedIterable<GHPullRequest> listPullRequests(final GHIssueState state) {
|
public PagedIterable<GHPullRequest> listPullRequests(final GHIssueState state) {
|
||||||
return new PagedIterable<GHPullRequest>() {
|
return new PagedIterable<GHPullRequest>() {
|
||||||
public PagedIterator<GHPullRequest> iterator() {
|
public PagedIterator<GHPullRequest> iterator() {
|
||||||
return new PagedIterator<GHPullRequest>(root.retrieve().asIterator(String.format("/repos/%s/%s/pulls?state=%s", owner.login, name, state.name().toLowerCase(Locale.ENGLISH)), GHPullRequest[].class)) {
|
return new PagedIterator<GHPullRequest>(root.retrieve().asIterator(getApiTailUrl("pulls?state="+state.name().toLowerCase(Locale.ENGLISH)), GHPullRequest[].class)) {
|
||||||
@Override
|
@Override
|
||||||
protected void wrapUp(GHPullRequest[] page) {
|
protected void wrapUp(GHPullRequest[] page) {
|
||||||
for (GHPullRequest pr : page)
|
for (GHPullRequest pr : page)
|
||||||
@@ -578,14 +578,14 @@ public class GHRepository extends GHObject {
|
|||||||
*/
|
*/
|
||||||
public List<GHHook> getHooks() throws IOException {
|
public List<GHHook> getHooks() throws IOException {
|
||||||
List<GHHook> list = new ArrayList<GHHook>(Arrays.asList(
|
List<GHHook> list = new ArrayList<GHHook>(Arrays.asList(
|
||||||
root.retrieve().to(String.format("/repos/%s/%s/hooks", owner.login, name), GHHook[].class)));
|
root.retrieve().to(getApiTailUrl("hooks"), GHHook[].class)));
|
||||||
for (GHHook h : list)
|
for (GHHook h : list)
|
||||||
h.wrap(this);
|
h.wrap(this);
|
||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
public GHHook getHook(int id) throws IOException {
|
public GHHook getHook(int id) throws IOException {
|
||||||
return root.retrieve().to(String.format("/repos/%s/%s/hooks/%d", owner.login, name, id), GHHook.class).wrap(this);
|
return root.retrieve().to(getApiTailUrl("hooks/" + id), GHHook.class).wrap(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -597,7 +597,7 @@ public class GHRepository extends GHObject {
|
|||||||
* @throws IOException on failure communicating with GitHub
|
* @throws IOException on failure communicating with GitHub
|
||||||
*/
|
*/
|
||||||
public GHCompare getCompare(String id1, String id2) throws IOException {
|
public GHCompare getCompare(String id1, String id2) throws IOException {
|
||||||
GHCompare compare = root.retrieve().to(String.format("/repos/%s/%s/compare/%s...%s", owner.login, name, id1, id2), GHCompare.class);
|
GHCompare compare = root.retrieve().to(getApiTailUrl(String.format("compare/%s...%s", id1, id2)), GHCompare.class);
|
||||||
return compare.wrap(this);
|
return compare.wrap(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -760,6 +760,54 @@ public class GHRepository extends GHObject {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Lists labels in this repository.
|
||||||
|
*
|
||||||
|
* https://developer.github.com/v3/issues/labels/#list-all-labels-for-this-repository
|
||||||
|
*/
|
||||||
|
public PagedIterable<GHLabel> listLabels() throws IOException {
|
||||||
|
return new PagedIterable<GHLabel>() {
|
||||||
|
public PagedIterator<GHLabel> iterator() {
|
||||||
|
return new PagedIterator<GHLabel>(root.retrieve().asIterator(getApiTailUrl("labels"), GHLabel[].class)) {
|
||||||
|
@Override
|
||||||
|
protected void wrapUp(GHLabel[] page) {
|
||||||
|
for (GHLabel c : page)
|
||||||
|
c.wrapUp(GHRepository.this);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public GHLabel getLabel(String name) throws IOException {
|
||||||
|
return root.retrieve().to(getApiTailUrl("labels/"+name), GHLabel.class).wrapUp(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
public GHLabel createLabel(String name, String color) throws IOException {
|
||||||
|
return root.retrieve().method("POST")
|
||||||
|
.with("name",name)
|
||||||
|
.with("color",color)
|
||||||
|
.to(getApiTailUrl("labels"), GHLabel.class).wrapUp(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Lists all the subscribers (aka watchers.)
|
||||||
|
*
|
||||||
|
* https://developer.github.com/v3/activity/watching/
|
||||||
|
*/
|
||||||
|
public PagedIterable<GHUser> listSubscribers() {
|
||||||
|
return new PagedIterable<GHUser>() {
|
||||||
|
public PagedIterator<GHUser> iterator() {
|
||||||
|
return new PagedIterator<GHUser>(root.retrieve().asIterator(getApiTailUrl("subscribers"), GHUser[].class)) {
|
||||||
|
protected void wrapUp(GHUser[] page) {
|
||||||
|
for (GHUser c : page)
|
||||||
|
c.wrapUp(root);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* See https://api.github.com/hooks for possible names and their configuration scheme.
|
* See https://api.github.com/hooks for possible names and their configuration scheme.
|
||||||
@@ -938,7 +986,7 @@ public class GHRepository extends GHObject {
|
|||||||
|
|
||||||
public GHContent getFileContent(String path, String ref) throws IOException {
|
public GHContent getFileContent(String path, String ref) throws IOException {
|
||||||
Requester requester = root.retrieve();
|
Requester requester = root.retrieve();
|
||||||
String target = String.format("/repos/%s/%s/contents/%s", owner.login, name, path);
|
String target = getApiTailUrl("contents/" + path);
|
||||||
|
|
||||||
if (ref != null)
|
if (ref != null)
|
||||||
target = target + "?ref=" + ref;
|
target = target + "?ref=" + ref;
|
||||||
@@ -952,7 +1000,7 @@ public class GHRepository extends GHObject {
|
|||||||
|
|
||||||
public List<GHContent> getDirectoryContent(String path, String ref) throws IOException {
|
public List<GHContent> getDirectoryContent(String path, String ref) throws IOException {
|
||||||
Requester requester = root.retrieve();
|
Requester requester = root.retrieve();
|
||||||
String target = String.format("/repos/%s/%s/contents/%s", owner.login, name, path);
|
String target = getApiTailUrl("contents/" + path);
|
||||||
|
|
||||||
if (ref != null)
|
if (ref != null)
|
||||||
target = target + "?ref=" + ref;
|
target = target + "?ref=" + ref;
|
||||||
@@ -964,8 +1012,12 @@ public class GHRepository extends GHObject {
|
|||||||
return Arrays.asList(files);
|
return Arrays.asList(files);
|
||||||
}
|
}
|
||||||
|
|
||||||
public GHContent getReadme() throws Exception {
|
/**
|
||||||
return getFileContent("readme");
|
* https://developer.github.com/v3/repos/contents/#get-the-readme
|
||||||
|
*/
|
||||||
|
public GHContent getReadme() throws IOException {
|
||||||
|
Requester requester = root.retrieve();
|
||||||
|
return requester.to(getApiTailUrl("readme"), GHContent.class).wrap(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
public GHContentUpdateResponse createContent(String content, String commitMessage, String path) throws IOException {
|
public GHContentUpdateResponse createContent(String content, String commitMessage, String path) throws IOException {
|
||||||
@@ -1012,11 +1064,56 @@ public class GHRepository extends GHObject {
|
|||||||
|
|
||||||
public List<GHDeployKey> getDeployKeys() throws IOException{
|
public List<GHDeployKey> getDeployKeys() throws IOException{
|
||||||
List<GHDeployKey> list = new ArrayList<GHDeployKey>(Arrays.asList(
|
List<GHDeployKey> list = new ArrayList<GHDeployKey>(Arrays.asList(
|
||||||
root.retrieve().to(String.format("/repos/%s/%s/keys", owner.login, name), GHDeployKey[].class)));
|
root.retrieve().to(getApiTailUrl("keys"), GHDeployKey[].class)));
|
||||||
for (GHDeployKey h : list)
|
for (GHDeployKey h : list)
|
||||||
h.wrap(this);
|
h.wrap(this);
|
||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Subscribes to this repository to get notifications.
|
||||||
|
*/
|
||||||
|
public GHSubscription subscribe(boolean subscribed, boolean ignored) throws IOException {
|
||||||
|
return new Requester(root)
|
||||||
|
.with("subscribed", subscribed)
|
||||||
|
.with("ignored", ignored)
|
||||||
|
.method("PUT").to(getApiTailUrl("subscription"), GHSubscription.class).wrapUp(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the current subscription.
|
||||||
|
*
|
||||||
|
* @return null if no subscription exists.
|
||||||
|
*/
|
||||||
|
public GHSubscription getSubscription() throws IOException {
|
||||||
|
try {
|
||||||
|
return new Requester(root).to(getApiTailUrl("subscription"), GHSubscription.class).wrapUp(this);
|
||||||
|
} catch (FileNotFoundException e) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public PagedIterable<Contributor> listContributors() throws IOException {
|
||||||
|
return new PagedIterable<Contributor>() {
|
||||||
|
public PagedIterator<Contributor> iterator() {
|
||||||
|
return new PagedIterator<Contributor>(root.retrieve().asIterator(getApiTailUrl("contributors"), Contributor[].class)) {
|
||||||
|
@Override
|
||||||
|
protected void wrapUp(Contributor[] page) {
|
||||||
|
for (Contributor c : page)
|
||||||
|
c.wrapUp(root);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class Contributor extends GHUser {
|
||||||
|
private int contributions;
|
||||||
|
|
||||||
|
public int getContributions() {
|
||||||
|
return contributions;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -1041,6 +1138,7 @@ public class GHRepository extends GHObject {
|
|||||||
}
|
}
|
||||||
|
|
||||||
String getApiTailUrl(String tail) {
|
String getApiTailUrl(String tail) {
|
||||||
return "/repos/" + owner.login + "/" + name +'/'+tail;
|
if (tail.length()>0 && !tail.startsWith("/")) tail='/'+tail;
|
||||||
|
return "/repos/" + owner.login + "/" + name +tail;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
62
src/main/java/org/kohsuke/github/GHSubscription.java
Normal file
62
src/main/java/org/kohsuke/github/GHSubscription.java
Normal file
@@ -0,0 +1,62 @@
|
|||||||
|
package org.kohsuke.github;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represents your subscribing to a repository.
|
||||||
|
*
|
||||||
|
* @author Kohsuke Kawaguchi
|
||||||
|
*/
|
||||||
|
public class GHSubscription {
|
||||||
|
private String created_at, url, repository_url, reason;
|
||||||
|
private boolean subscribed, ignored;
|
||||||
|
|
||||||
|
private GitHub root;
|
||||||
|
private GHRepository repo;
|
||||||
|
|
||||||
|
public Date getCreatedAt() {
|
||||||
|
return GitHub.parseDate(created_at);
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getUrl() {
|
||||||
|
return url;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getRepositoryUrl() {
|
||||||
|
return repository_url;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getReason() {
|
||||||
|
return reason;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isSubscribed() {
|
||||||
|
return subscribed;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isIgnored() {
|
||||||
|
return ignored;
|
||||||
|
}
|
||||||
|
|
||||||
|
public GHRepository getRepository() {
|
||||||
|
return repo;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Removes this subscription.
|
||||||
|
*/
|
||||||
|
public void delete() throws IOException {
|
||||||
|
new Requester(root).method("DELETE").to(url);
|
||||||
|
}
|
||||||
|
|
||||||
|
GHSubscription wrapUp(GHRepository repo) {
|
||||||
|
this.repo = repo;
|
||||||
|
return wrapUp(repo.root);
|
||||||
|
}
|
||||||
|
|
||||||
|
GHSubscription wrapUp(GitHub root) {
|
||||||
|
this.root = root;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -15,6 +15,8 @@ public class GHTag {
|
|||||||
/*package*/ GHTag wrap(GHRepository owner) {
|
/*package*/ GHTag wrap(GHRepository owner) {
|
||||||
this.owner = owner;
|
this.owner = owner;
|
||||||
this.root = owner.root;
|
this.root = owner.root;
|
||||||
|
if (commit!=null)
|
||||||
|
commit.wrapUp(owner);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -69,6 +69,24 @@ public class GHUser extends GHPerson {
|
|||||||
return new GHPersonSet<GHUser>(Arrays.asList(wrap(followers,root)));
|
return new GHPersonSet<GHUser>(Arrays.asList(wrap(followers,root)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Lists all the subscribed (aka watched) repositories.
|
||||||
|
*
|
||||||
|
* https://developer.github.com/v3/activity/watching/
|
||||||
|
*/
|
||||||
|
public PagedIterable<GHRepository> listSubscriptions() {
|
||||||
|
return new PagedIterable<GHRepository>() {
|
||||||
|
public PagedIterator<GHRepository> iterator() {
|
||||||
|
return new PagedIterator<GHRepository>(root.retrieve().asIterator(getApiTailUrl("subscriptions"), GHRepository[].class)) {
|
||||||
|
protected void wrapUp(GHRepository[] page) {
|
||||||
|
for (GHRepository c : page)
|
||||||
|
c.wrap(root);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns true if this user belongs to the specified organization.
|
* Returns true if this user belongs to the specified organization.
|
||||||
*/
|
*/
|
||||||
@@ -162,4 +180,9 @@ public class GHUser extends GHPerson {
|
|||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
String getApiTailUrl(String tail) {
|
||||||
|
if (tail.length()>0 && !tail.startsWith("/")) tail='/'+tail;
|
||||||
|
return "/users/" + login + tail;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -38,6 +38,7 @@ 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.HashSet;
|
||||||
|
import java.util.Hashtable;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
@@ -53,6 +54,12 @@ import com.infradna.tool.bridge_method_injector.WithBridgeMethods;
|
|||||||
/**
|
/**
|
||||||
* Root of the GitHub API.
|
* Root of the GitHub API.
|
||||||
*
|
*
|
||||||
|
* <h2>Thread safety</h2>
|
||||||
|
* <p>
|
||||||
|
* This library aims to be safe for use by multiple threads concurrently, although
|
||||||
|
* the library itself makes no attempt to control/serialize potentially conflicting
|
||||||
|
* operations to GitHub, such as updating & deleting a repository at the same time.
|
||||||
|
*
|
||||||
* @author Kohsuke Kawaguchi
|
* @author Kohsuke Kawaguchi
|
||||||
*/
|
*/
|
||||||
public class GitHub {
|
public class GitHub {
|
||||||
@@ -63,8 +70,8 @@ public class GitHub {
|
|||||||
*/
|
*/
|
||||||
/*package*/ final String encodedAuthorization;
|
/*package*/ final String encodedAuthorization;
|
||||||
|
|
||||||
private final Map<String,GHUser> users = new HashMap<String, GHUser>();
|
private final Map<String,GHUser> users = new Hashtable<String, GHUser>();
|
||||||
private final Map<String,GHOrganization> orgs = new HashMap<String, GHOrganization>();
|
private final Map<String,GHOrganization> orgs = new Hashtable<String, GHOrganization>();
|
||||||
|
|
||||||
private final String apiUrl;
|
private final String apiUrl;
|
||||||
|
|
||||||
|
|||||||
@@ -6,6 +6,9 @@ import java.io.File;
|
|||||||
import java.io.FileInputStream;
|
import java.io.FileInputStream;
|
||||||
import java.io.FileNotFoundException;
|
import java.io.FileNotFoundException;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.net.HttpURLConnection;
|
||||||
|
import java.net.Proxy;
|
||||||
|
import java.net.URL;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
|
|
||||||
@@ -153,6 +156,19 @@ public class GitHubBuilder {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Configures {@linkplain #withConnector(HttpConnector) connector}
|
||||||
|
* that uses HTTP library in JRE but use a specific proxy, instead of
|
||||||
|
* the system default one.
|
||||||
|
*/
|
||||||
|
public GitHubBuilder withProxy(final Proxy p) {
|
||||||
|
return withConnector(new HttpConnector() {
|
||||||
|
public HttpURLConnection connect(URL url) throws IOException {
|
||||||
|
return (HttpURLConnection) url.openConnection(p);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
public GitHub build() throws IOException {
|
public GitHub build() throws IOException {
|
||||||
return new GitHub(endpoint, user, oauthToken, password, connector);
|
return new GitHub(endpoint, user, oauthToken, password, connector);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,22 +1,17 @@
|
|||||||
import org.kohsuke.github.GHIssue;
|
import org.kohsuke.github.GHRepository;
|
||||||
import org.kohsuke.github.GHOrganization;
|
|
||||||
import org.kohsuke.github.GHTeam;
|
|
||||||
import org.kohsuke.github.GitHub;
|
import org.kohsuke.github.GitHub;
|
||||||
import org.kohsuke.github.PagedIterable;
|
|
||||||
import org.kohsuke.github.PagedSearchIterable;
|
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Collection;
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Kohsuke Kawaguchi
|
* @author Kohsuke Kawaguchi
|
||||||
*/
|
*/
|
||||||
public class Foo {
|
public class Foo {
|
||||||
public static void main(String[] args) throws Exception {
|
public static void main(String[] args) throws Exception {
|
||||||
PagedSearchIterable<GHIssue> reviewbybees = GitHub.connect().searchIssues().mentions("reviewbybees").isOpen().list();
|
Collection<GHRepository> lst = GitHub.connect().getUser("kohsuke").getRepositories().values();
|
||||||
for (GHIssue r : reviewbybees) {
|
for (GHRepository r : lst) {
|
||||||
System.out.println(r.getTitle());
|
System.out.println(r.getName());
|
||||||
}
|
}
|
||||||
System.out.println("total="+reviewbybees.getTotalCount());
|
System.out.println(lst.size());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ import java.io.IOException;
|
|||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.Map.Entry;
|
import java.util.Map.Entry;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Unit test for simple App.
|
* Unit test for simple App.
|
||||||
@@ -658,6 +659,56 @@ public class AppTest extends AbstractGitHubApiTestBase {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test // issue #99
|
||||||
|
public void testReadme() throws IOException {
|
||||||
|
GHContent readme = gitHub.getRepository("github-api-test-org/test-readme").getReadme();
|
||||||
|
assertEquals(readme.getName(),"README.md");
|
||||||
|
assertEquals(readme.getContent(),"This is a markdown readme.\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testRepoLabel() throws IOException {
|
||||||
|
GHRepository r = gitHub.getRepository("github-api-test-org/test-labels");
|
||||||
|
List<GHLabel> lst = r.listLabels().asList();
|
||||||
|
for (GHLabel l : lst) {
|
||||||
|
System.out.println(l.getName());
|
||||||
|
}
|
||||||
|
assertTrue(lst.size() > 5);
|
||||||
|
GHLabel e = r.getLabel("enhancement");
|
||||||
|
assertEquals("enhancement",e.getName());
|
||||||
|
assertNotNull(e.getUrl());
|
||||||
|
assertTrue(Pattern.matches("[0-9a-fA-F]{6}",e.getColor()));
|
||||||
|
|
||||||
|
{// CRUD
|
||||||
|
GHLabel t = r.createLabel("test", "123456");
|
||||||
|
GHLabel t2 = r.getLabel("test");
|
||||||
|
assertEquals(t.getName(), t2.getName());
|
||||||
|
assertEquals(t.getColor(), "123456");
|
||||||
|
assertEquals(t.getColor(), t2.getColor());
|
||||||
|
assertEquals(t.getUrl(), t2.getUrl());
|
||||||
|
t.delete();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSubscribers() throws IOException {
|
||||||
|
boolean kohsuke = false;
|
||||||
|
GHRepository mr = gitHub.getRepository("kohsuke/github-api");
|
||||||
|
for (GHUser u : mr.listSubscribers()) {
|
||||||
|
System.out.println(u.getLogin());
|
||||||
|
kohsuke |= u.getLogin().equals("kohsuke");
|
||||||
|
}
|
||||||
|
assertTrue(kohsuke);
|
||||||
|
System.out.println("---");
|
||||||
|
|
||||||
|
boolean githubApi = false;
|
||||||
|
for (GHRepository r : gitHub.getUser("kohsuke").listRepositories()) {
|
||||||
|
System.out.println(r.getName());
|
||||||
|
githubApi |= r.equals(mr);
|
||||||
|
}
|
||||||
|
assertTrue(githubApi);
|
||||||
|
}
|
||||||
|
|
||||||
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"));
|
||||||
|
|||||||
16
src/test/java/org/kohsuke/github/CommitTest.java
Normal file
16
src/test/java/org/kohsuke/github/CommitTest.java
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
package org.kohsuke.github;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Kohsuke Kawaguchi
|
||||||
|
*/
|
||||||
|
public class CommitTest extends AbstractGitHubApiTestBase {
|
||||||
|
@Test // issue 152
|
||||||
|
public void lastStatus() throws IOException {
|
||||||
|
GHTag t = gitHub.getRepository("stapler/stapler").listTags().iterator().next();
|
||||||
|
t.getCommit().getLastStatus();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -24,7 +24,7 @@ public class PullRequestTest extends AbstractGitHubApiTestBase {
|
|||||||
String label = rnd.next();
|
String label = rnd.next();
|
||||||
p.setLabels(label);
|
p.setLabels(label);
|
||||||
|
|
||||||
Collection<GHIssue.Label> labels = getRepository().getPullRequest(p.getNumber()).getLabels();
|
Collection<GHLabel> labels = getRepository().getPullRequest(p.getNumber()).getLabels();
|
||||||
assertEquals(1, labels.size());
|
assertEquals(1, labels.size());
|
||||||
assertEquals(label, labels.iterator().next().getName());
|
assertEquals(label, labels.iterator().next().getName());
|
||||||
}
|
}
|
||||||
@@ -38,6 +38,22 @@ public class PullRequestTest extends AbstractGitHubApiTestBase {
|
|||||||
assertEquals(user, getRepository().getPullRequest(p.getNumber()).getAssignee());
|
assertEquals(user, getRepository().getPullRequest(p.getNumber()).getAssignee());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetUser() throws IOException {
|
||||||
|
GHPullRequest p = getRepository().createPullRequest(rnd.next(), "stable", "master", "## test");
|
||||||
|
GHPullRequest prSingle = getRepository().getPullRequest(p.getNumber());
|
||||||
|
assertNotNull(prSingle.getUser().root);
|
||||||
|
prSingle.getMergeable();
|
||||||
|
assertNotNull(prSingle.getUser().root);
|
||||||
|
|
||||||
|
PagedIterable<GHPullRequest> ghPullRequests = getRepository().listPullRequests(GHIssueState.OPEN);
|
||||||
|
for (GHPullRequest pr : ghPullRequests) {
|
||||||
|
assertNotNull(pr.getUser().root);
|
||||||
|
assertFalse(pr.getMergeable());
|
||||||
|
assertNotNull(pr.getUser().root);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@After
|
@After
|
||||||
public void cleanUp() throws Exception {
|
public void cleanUp() throws Exception {
|
||||||
for (GHPullRequest pr : getRepository().getPullRequests(GHIssueState.OPEN)) {
|
for (GHPullRequest pr : getRepository().getPullRequests(GHIssueState.OPEN)) {
|
||||||
|
|||||||
89
src/test/java/org/kohsuke/github/RepositoryMockTest.java
Normal file
89
src/test/java/org/kohsuke/github/RepositoryMockTest.java
Normal file
@@ -0,0 +1,89 @@
|
|||||||
|
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 RepositoryMockTest {
|
||||||
|
|
||||||
|
@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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,91 +1,46 @@
|
|||||||
package org.kohsuke.github;
|
package org.kohsuke.github;
|
||||||
|
|
||||||
import org.junit.Assert;
|
|
||||||
import org.junit.Before;
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.mockito.Mock;
|
import org.kohsuke.github.GHRepository.Contributor;
|
||||||
import org.mockito.Mockito;
|
|
||||||
import org.mockito.MockitoAnnotations;
|
|
||||||
|
|
||||||
import java.util.Iterator;
|
import java.io.IOException;
|
||||||
|
|
||||||
import static org.mockito.Mockito.when;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Luciano P. Sabenca (luciano.sabenca [at] movile [com] | lucianosabenca [at] gmail [dot] com
|
* @author Kohsuke Kawaguchi
|
||||||
*/
|
*/
|
||||||
public class RepositoryTest {
|
public class RepositoryTest extends AbstractGitHubApiTestBase {
|
||||||
|
@Test
|
||||||
|
public void subscription() throws Exception {
|
||||||
|
GHRepository r = getRepository();
|
||||||
|
assertNull(r.getSubscription());
|
||||||
|
|
||||||
@Mock
|
GHSubscription s = r.subscribe(true, false);
|
||||||
GitHub mockGitHub;
|
assertEquals(s.getRepository(), r);
|
||||||
|
|
||||||
@Mock
|
s.delete();
|
||||||
Iterator<GHUser[]> iterator;
|
|
||||||
|
|
||||||
@Mock
|
assertNull(r.getSubscription());
|
||||||
GHRepository mockRepository;
|
|
||||||
|
|
||||||
|
|
||||||
@Before
|
|
||||||
public void setUp() {
|
|
||||||
MockitoAnnotations.initMocks(this);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void listCollaborators() throws Exception {
|
public void listContributors() throws IOException {
|
||||||
GHUser user1 = new GHUser();
|
GHRepository r = gitHub.getOrganization("stapler").getRepository("stapler");
|
||||||
user1.login = "login1";
|
int i=0;
|
||||||
|
boolean kohsuke = false;
|
||||||
|
|
||||||
GHUser user2 = new GHUser();
|
for (Contributor c : r.listContributors()) {
|
||||||
user2.login = "login2";
|
System.out.println(c.getName());
|
||||||
|
assertTrue(c.getContributions()>0);
|
||||||
|
if (c.getLogin().equals("kohsuke"))
|
||||||
when(iterator.hasNext()).thenReturn(true, false, true);
|
kohsuke = true;
|
||||||
when(iterator.next()).thenReturn(new GHUser[]{user1}, new GHUser[]{user2});
|
if (i++ > 5)
|
||||||
|
break;
|
||||||
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);
|
|
||||||
|
|
||||||
|
assertTrue(kohsuke);
|
||||||
|
}
|
||||||
|
|
||||||
|
private GHRepository getRepository() throws IOException {
|
||||||
|
return gitHub.getOrganization("github-api-test-org").getRepository("jenkins");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user