diff --git a/src/main/java/org/kohsuke/github/GHInvitation.java b/src/main/java/org/kohsuke/github/GHInvitation.java new file mode 100644 index 0000000000..213b7af3f1 --- /dev/null +++ b/src/main/java/org/kohsuke/github/GHInvitation.java @@ -0,0 +1,42 @@ +package org.kohsuke.github; + +import java.io.IOException; +import java.net.URL; + +/** + * @see GitHub#getMyInvitations() + * @see GHRepository#listInvitations() + */ +public class GHInvitation extends GHObject { + /*package almost final*/ GitHub root; + + private int id; + private GHRepository repository; + private GHUser invitee, inviter; + private String permissions; + private String html_url; + + /*package*/ GHInvitation wrapUp(GitHub root) { + this.root = root; + return this; + } + + /** + * Accept a repository invitation. + */ + public void accept() throws IOException { + root.retrieve().method("PATCH").to("/user/repository_invitations/" + id); + } + + /** + * Decline a repository invitation. + */ + public void decline() throws IOException { + root.retrieve().method("DELETE").to("/user/repository_invitations/" + id); + } + + @Override + public URL getHtmlUrl() { + return GitHub.parseURL(html_url); + } +} diff --git a/src/main/java/org/kohsuke/github/GHRepository.java b/src/main/java/org/kohsuke/github/GHRepository.java index 9d5dc5c52f..50b352834c 100644 --- a/src/main/java/org/kohsuke/github/GHRepository.java +++ b/src/main/java/org/kohsuke/github/GHRepository.java @@ -1135,6 +1135,22 @@ public GHLabel createLabel(String name, String color) throws IOException { .to(getApiTailUrl("labels"), GHLabel.class).wrapUp(this); } + /** + * Lists all the invitations. + */ + public PagedIterable listInvitations() { + return new PagedIterable() { + public PagedIterator _iterator(int pageSize) { + return new PagedIterator(root.retrieve().asIterator(String.format("/repos/%s/%s/invitations", getOwnerName(), name), GHInvitation[].class, pageSize)) { + protected void wrapUp(GHInvitation[] page) { + for (GHInvitation c : page) + c.wrapUp(root); + } + }; + } + }; + } + /** * Lists all the subscribers (aka watchers.) * diff --git a/src/main/java/org/kohsuke/github/GitHub.java b/src/main/java/org/kohsuke/github/GitHub.java index 52a16906da..d5d0a2be33 100644 --- a/src/main/java/org/kohsuke/github/GitHub.java +++ b/src/main/java/org/kohsuke/github/GitHub.java @@ -530,6 +530,15 @@ public GHLicense getLicense(String key) throws IOException { } /** + * Gets complete list of open invitations for current user. + */ + public List getMyInvitations() throws IOException { + GHInvitation[] invitations = retrieve().to("/user/repository_invitations", GHInvitation[].class); + for (GHInvitation i : invitations) { + i.wrapUp(this); + } + return Arrays.asList(invitations); + } /** * This method returns a shallowly populated organizations.