mirror of
https://github.com/jlengrand/github-api.git
synced 2026-03-12 00:11:22 +00:00
Compare commits
7 Commits
github-api
...
github-api
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0023ecefa4 | ||
|
|
511f156603 | ||
|
|
3f223b1ba0 | ||
|
|
a1528a1a63 | ||
|
|
b8bfddbf3a | ||
|
|
47fc813027 | ||
|
|
c7f2228a44 |
4
pom.xml
4
pom.xml
@@ -7,7 +7,7 @@
|
|||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
<artifactId>github-api</artifactId>
|
<artifactId>github-api</artifactId>
|
||||||
<version>1.80</version>
|
<version>1.81</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.80</tag>
|
<tag>github-api-1.81</tag>
|
||||||
</scm>
|
</scm>
|
||||||
|
|
||||||
<distributionManagement>
|
<distributionManagement>
|
||||||
|
|||||||
@@ -29,13 +29,15 @@ import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
|||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
|
|
||||||
import static org.kohsuke.github.Previews.SQUIRREL_GIRL;
|
import static org.kohsuke.github.Previews.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Represents an issue on GitHub.
|
* Represents an issue on GitHub.
|
||||||
@@ -51,7 +53,8 @@ public class GHIssue extends GHObject implements Reactable{
|
|||||||
GHRepository owner;
|
GHRepository owner;
|
||||||
|
|
||||||
// API v3
|
// API v3
|
||||||
protected GHUser assignee;
|
protected GHUser assignee; // not sure what this field is now that 'assignees' exist
|
||||||
|
protected GHUser[] assignees;
|
||||||
protected String state;
|
protected String state;
|
||||||
protected int number;
|
protected int number;
|
||||||
protected String closed_at;
|
protected String closed_at;
|
||||||
@@ -81,6 +84,7 @@ public class GHIssue extends GHObject implements Reactable{
|
|||||||
/*package*/ GHIssue wrap(GitHub root) {
|
/*package*/ GHIssue wrap(GitHub root) {
|
||||||
this.root = root;
|
this.root = root;
|
||||||
if(assignee != null) assignee.wrapUp(root);
|
if(assignee != null) assignee.wrapUp(root);
|
||||||
|
if(assignees!=null) GHUser.wrap(assignees,root);
|
||||||
if(user != null) user.wrapUp(root);
|
if(user != null) user.wrapUp(root);
|
||||||
if(closed_by != null) closed_by.wrapUp(root);
|
if(closed_by != null) closed_by.wrapUp(root);
|
||||||
return this;
|
return this;
|
||||||
@@ -187,7 +191,7 @@ public class GHIssue extends GHObject implements Reactable{
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void assignTo(GHUser user) throws IOException {
|
public void assignTo(GHUser user) throws IOException {
|
||||||
editIssue("assignee", user.getLogin());
|
setAssignees(user);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setLabels(String... labels) throws IOException {
|
public void setLabels(String... labels) throws IOException {
|
||||||
@@ -242,6 +246,40 @@ public class GHIssue extends GHObject implements Reactable{
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void addAssignees(GHUser... assignees) throws IOException {
|
||||||
|
addAssignees(Arrays.asList(assignees));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addAssignees(Collection<GHUser> assignees) throws IOException {
|
||||||
|
List<String> names = toLogins(assignees);
|
||||||
|
root.retrieve().method("POST").with("assignees",names).to(getIssuesApiRoute()+"/assignees",this);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAssignees(GHUser... assignees) throws IOException {
|
||||||
|
setAssignees(Arrays.asList(assignees));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAssignees(Collection<GHUser> assignees) throws IOException {
|
||||||
|
editIssue("assignees",toLogins(assignees));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void removeAssignees(GHUser... assignees) throws IOException {
|
||||||
|
removeAssignees(Arrays.asList(assignees));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void removeAssignees(Collection<GHUser> assignees) throws IOException {
|
||||||
|
List<String> names = toLogins(assignees);
|
||||||
|
root.retrieve().method("DELETE").with("assignees",names).inBody().to(getIssuesApiRoute()+"/assignees",this);
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<String> toLogins(Collection<GHUser> assignees) {
|
||||||
|
List<String> names = new ArrayList<String>(assignees.size());
|
||||||
|
for (GHUser a : assignees) {
|
||||||
|
names.add(a.getLogin());
|
||||||
|
}
|
||||||
|
return names;
|
||||||
|
}
|
||||||
|
|
||||||
protected String getApiRoute() {
|
protected String getApiRoute() {
|
||||||
return getIssuesApiRoute();
|
return getIssuesApiRoute();
|
||||||
}
|
}
|
||||||
@@ -253,7 +291,11 @@ public class GHIssue extends GHObject implements Reactable{
|
|||||||
public GHUser getAssignee() {
|
public GHUser getAssignee() {
|
||||||
return assignee;
|
return assignee;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public List<GHUser> getAssignees() {
|
||||||
|
return Collections.unmodifiableList(Arrays.asList(assignees));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* User who submitted the issue.
|
* User who submitted the issue.
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ public class GHIssueBuilder {
|
|||||||
private final GHRepository repo;
|
private final GHRepository repo;
|
||||||
private final Requester builder;
|
private final Requester builder;
|
||||||
private List<String> labels = new ArrayList<String>();
|
private List<String> labels = new ArrayList<String>();
|
||||||
|
private List<String> assignees = new ArrayList<String>();
|
||||||
|
|
||||||
GHIssueBuilder(GHRepository repo, String title) {
|
GHIssueBuilder(GHRepository repo, String title) {
|
||||||
this.repo = repo;
|
this.repo = repo;
|
||||||
@@ -28,13 +29,13 @@ public class GHIssueBuilder {
|
|||||||
|
|
||||||
public GHIssueBuilder assignee(GHUser user) {
|
public GHIssueBuilder assignee(GHUser user) {
|
||||||
if (user!=null)
|
if (user!=null)
|
||||||
builder.with("assignee",user.getLogin());
|
assignees.add(user.getLogin());
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public GHIssueBuilder assignee(String user) {
|
public GHIssueBuilder assignee(String user) {
|
||||||
if (user!=null)
|
if (user!=null)
|
||||||
builder.with("assignee",user);
|
assignees.add(user);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -54,6 +55,6 @@ public class GHIssueBuilder {
|
|||||||
* Creates a new issue.
|
* Creates a new issue.
|
||||||
*/
|
*/
|
||||||
public GHIssue create() throws IOException {
|
public GHIssue create() throws IOException {
|
||||||
return builder.with("labels",labels).to(repo.getApiTailUrl("issues"),GHIssue.class).wrap(repo);
|
return builder.with("labels",labels).with("assignees",assignees).to(repo.getApiTailUrl("issues"),GHIssue.class).wrap(repo);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
84
src/main/java/org/kohsuke/github/GHMembership.java
Normal file
84
src/main/java/org/kohsuke/github/GHMembership.java
Normal file
@@ -0,0 +1,84 @@
|
|||||||
|
package org.kohsuke.github;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.net.URL;
|
||||||
|
import java.util.Locale;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represents a membership of a user in an organization.
|
||||||
|
*
|
||||||
|
* @author Kohsuke Kawaguchi
|
||||||
|
* @see GHMyself#listOrgMemberships()
|
||||||
|
*/
|
||||||
|
public class GHMembership /* extends GHObject --- but it doesn't have id, created_at, etc. */ {
|
||||||
|
GitHub root;
|
||||||
|
|
||||||
|
String url;
|
||||||
|
String state;
|
||||||
|
String role;
|
||||||
|
GHUser user;
|
||||||
|
GHOrganization organization;
|
||||||
|
|
||||||
|
public URL getUrl() {
|
||||||
|
return GitHub.parseURL(url);
|
||||||
|
}
|
||||||
|
|
||||||
|
public State getState() {
|
||||||
|
return Enum.valueOf(State.class, state.toUpperCase(Locale.ENGLISH));
|
||||||
|
}
|
||||||
|
|
||||||
|
public Role getRole() {
|
||||||
|
return Enum.valueOf(Role.class, role.toUpperCase(Locale.ENGLISH));
|
||||||
|
}
|
||||||
|
|
||||||
|
public GHUser getUser() {
|
||||||
|
return user;
|
||||||
|
}
|
||||||
|
|
||||||
|
public GHOrganization getOrganization() {
|
||||||
|
return organization;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Accepts a pending invitation to an organization.
|
||||||
|
*
|
||||||
|
* @see GHMyself#getMembership(GHOrganization)
|
||||||
|
*/
|
||||||
|
public void activate() throws IOException {
|
||||||
|
root.retrieve().method("PATCH").with("state",State.ACTIVE).to(url,this);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*package*/ GHMembership wrap(GitHub root) {
|
||||||
|
this.root = root;
|
||||||
|
if (user!=null) user = root.getUser(user.wrapUp(root));
|
||||||
|
if (organization!=null) organization.wrapUp(root);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*package*/ static void wrap(GHMembership[] page, GitHub root) {
|
||||||
|
for (GHMembership m : page)
|
||||||
|
m.wrap(root);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Role of a user in an organization.
|
||||||
|
*/
|
||||||
|
public enum Role {
|
||||||
|
/**
|
||||||
|
* Organization owner.
|
||||||
|
*/
|
||||||
|
ADMIN,
|
||||||
|
/**
|
||||||
|
* Non-owner organization member.
|
||||||
|
*/
|
||||||
|
MEMBER;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Whether a role is currently active or waiting for acceptance (pending)
|
||||||
|
*/
|
||||||
|
public enum State {
|
||||||
|
ACTIVE,
|
||||||
|
PENDING;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -178,6 +178,39 @@ public class GHMyself extends GHUser {
|
|||||||
return listRepositories();
|
return listRepositories();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* List your organization memberships
|
||||||
|
*/
|
||||||
|
public PagedIterable<GHMembership> listOrgMemberships() {
|
||||||
|
return listOrgMemberships(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* List your organization memberships
|
||||||
|
*
|
||||||
|
* @param state
|
||||||
|
* Filter by a specific state
|
||||||
|
*/
|
||||||
|
public PagedIterable<GHMembership> listOrgMemberships(final GHMembership.State state) {
|
||||||
|
return new PagedIterable<GHMembership>() {
|
||||||
|
public PagedIterator<GHMembership> _iterator(int pageSize) {
|
||||||
|
return new PagedIterator<GHMembership>(root.retrieve().with("state",state).asIterator("/user/memberships/orgs", GHMembership[].class, pageSize)) {
|
||||||
|
@Override
|
||||||
|
protected void wrapUp(GHMembership[] page) {
|
||||||
|
GHMembership.wrap(page,root);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets your membership in a specific organization.
|
||||||
|
*/
|
||||||
|
public GHMembership getMembership(GHOrganization o) throws IOException {
|
||||||
|
return root.retrieve().to("/user/memberships/orgs/"+o.getLogin(),GHMembership.class).wrap(root);
|
||||||
|
}
|
||||||
|
|
||||||
// public void addEmails(Collection<String> emails) throws IOException {
|
// public void addEmails(Collection<String> emails) throws IOException {
|
||||||
//// new Requester(root,ApiVersion.V3).withCredential().to("/user/emails");
|
//// new Requester(root,ApiVersion.V3).withCredential().to("/user/emails");
|
||||||
// root.retrieveWithAuth3()
|
// root.retrieveWithAuth3()
|
||||||
|
|||||||
@@ -50,8 +50,8 @@ import java.util.Map;
|
|||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.TreeMap;
|
import java.util.TreeMap;
|
||||||
|
|
||||||
import static java.util.Arrays.asList;
|
import static java.util.Arrays.*;
|
||||||
import static org.kohsuke.github.Previews.DRAX;
|
import static org.kohsuke.github.Previews.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A repository on GitHub.
|
* A repository on GitHub.
|
||||||
@@ -451,22 +451,22 @@ public class GHRepository extends GHObject {
|
|||||||
* @throws IOException
|
* @throws IOException
|
||||||
*/
|
*/
|
||||||
public PagedIterable<GHUser> listCollaborators() throws IOException {
|
public PagedIterable<GHUser> listCollaborators() throws IOException {
|
||||||
return new PagedIterable<GHUser>() {
|
return listUsers("collaborators");
|
||||||
public PagedIterator<GHUser> _iterator(int pageSize) {
|
}
|
||||||
|
|
||||||
return new PagedIterator<GHUser>(root.retrieve().asIterator(getApiTailUrl("collaborators"), GHUser[].class, pageSize)) {
|
/**
|
||||||
|
* Lists all <a href="https://help.github.com/articles/assigning-issues-and-pull-requests-to-other-github-users/">the available assignees</a>
|
||||||
@Override
|
* to which issues may be assigned.
|
||||||
protected void wrapUp(GHUser[] users) {
|
*/
|
||||||
for (GHUser user : users) {
|
public PagedIterable<GHUser> listAssignees() throws IOException {
|
||||||
user.wrapUp(root);
|
return listUsers("assignees");
|
||||||
}
|
}
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks if the given user is an assignee for this repository.
|
||||||
|
*/
|
||||||
|
public boolean hasAssignee(GHUser u) throws IOException {
|
||||||
|
return root.retrieve().asHttpStatusCode(getApiTailUrl("assignees/" + u.getLogin()))/100==2;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -214,4 +214,9 @@ public class GHUser extends GHPerson {
|
|||||||
if (tail.length()>0 && !tail.startsWith("/")) tail='/'+tail;
|
if (tail.length()>0 && !tail.startsWith("/")) tail='/'+tail;
|
||||||
return "/users/" + login + tail;
|
return "/users/" + login + tail;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*package*/ GHUser wrapUp(GitHub root) {
|
||||||
|
super.wrapUp(root);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -340,7 +340,7 @@ public class GitHub {
|
|||||||
/**
|
/**
|
||||||
* Interns the given {@link GHUser}.
|
* Interns the given {@link GHUser}.
|
||||||
*/
|
*/
|
||||||
protected GHUser getUser(GHUser orig) throws IOException {
|
protected GHUser getUser(GHUser orig) {
|
||||||
GHUser u = users.get(orig.getLogin());
|
GHUser u = users.get(orig.getLogin());
|
||||||
if (u==null) {
|
if (u==null) {
|
||||||
orig.root = this;
|
orig.root = this;
|
||||||
|
|||||||
@@ -81,6 +81,7 @@ class Requester {
|
|||||||
* Current connection.
|
* Current connection.
|
||||||
*/
|
*/
|
||||||
private HttpURLConnection uc;
|
private HttpURLConnection uc;
|
||||||
|
private boolean forceBody;
|
||||||
|
|
||||||
private static class Entry {
|
private static class Entry {
|
||||||
String key;
|
String key;
|
||||||
@@ -197,6 +198,16 @@ class Requester {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Small number of GitHub APIs use HTTP methods somewhat inconsistently, and use a body where it's not expected.
|
||||||
|
* Normally whether parameters go as query parameters or a body depends on the HTTP verb in use,
|
||||||
|
* but this method forces the parameters to be sent as a body.
|
||||||
|
*/
|
||||||
|
/*package*/ Requester inBody() {
|
||||||
|
forceBody = true;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
public void to(String tailApiUrl) throws IOException {
|
public void to(String tailApiUrl) throws IOException {
|
||||||
to(tailApiUrl,null);
|
to(tailApiUrl,null);
|
||||||
}
|
}
|
||||||
@@ -230,7 +241,7 @@ class Requester {
|
|||||||
|
|
||||||
@SuppressFBWarnings("SBSC_USE_STRINGBUFFER_CONCATENATION")
|
@SuppressFBWarnings("SBSC_USE_STRINGBUFFER_CONCATENATION")
|
||||||
private <T> T _to(String tailApiUrl, Class<T> type, T instance) throws IOException {
|
private <T> T _to(String tailApiUrl, Class<T> type, T instance) throws IOException {
|
||||||
if (METHODS_WITHOUT_BODY.contains(method) && !args.isEmpty()) {
|
if (!isMethodWithBody() && !args.isEmpty()) {
|
||||||
boolean questionMarkFound = tailApiUrl.indexOf('?') != -1;
|
boolean questionMarkFound = tailApiUrl.indexOf('?') != -1;
|
||||||
tailApiUrl += questionMarkFound ? '&' : '?';
|
tailApiUrl += questionMarkFound ? '&' : '?';
|
||||||
for (Iterator<Entry> it = args.listIterator(); it.hasNext();) {
|
for (Iterator<Entry> it = args.listIterator(); it.hasNext();) {
|
||||||
@@ -340,7 +351,7 @@ class Requester {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private boolean isMethodWithBody() {
|
private boolean isMethodWithBody() {
|
||||||
return !METHODS_WITHOUT_BODY.contains(method);
|
return forceBody || !METHODS_WITHOUT_BODY.contains(method);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -888,6 +888,21 @@ public class AppTest extends AbstractGitHubApiTestBase {
|
|||||||
a.delete();
|
a.delete();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void listOrgMemberships() throws Exception {
|
||||||
|
GHMyself me = gitHub.getMyself();
|
||||||
|
for (GHMembership m : me.listOrgMemberships()) {
|
||||||
|
assertThat(m.getUser(), is((GHUser)me));
|
||||||
|
assertNotNull(m.getState());
|
||||||
|
assertNotNull(m.getRole());
|
||||||
|
|
||||||
|
System.out.printf("%s %s %s\n",
|
||||||
|
m.getOrganization().getLogin(),
|
||||||
|
m.getState(),
|
||||||
|
m.getRole());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
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"));
|
||||||
|
|||||||
Reference in New Issue
Block a user