From fe5ea52cdf9d2af3b434ac28428fab78270d7bdd Mon Sep 17 00:00:00 2001 From: Rechi Date: Tue, 29 May 2018 22:00:00 +0200 Subject: [PATCH 1/3] [feature] implement Repository Invitations API fixes #374 --- .../java/org/kohsuke/github/GHInvitation.java | 45 +++++++++++++++++++ .../java/org/kohsuke/github/GHRepository.java | 16 +++++++ src/main/java/org/kohsuke/github/GitHub.java | 9 ++++ 3 files changed, 70 insertions(+) create mode 100644 src/main/java/org/kohsuke/github/GHInvitation.java 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..6c285fefd1 --- /dev/null +++ b/src/main/java/org/kohsuke/github/GHInvitation.java @@ -0,0 +1,45 @@ +package org.kohsuke.github; + +import java.io.IOException; +import java.net.URL; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.TreeMap; + +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 3c80c499ff..d72cba14d3 100644 --- a/src/main/java/org/kohsuke/github/GHRepository.java +++ b/src/main/java/org/kohsuke/github/GHRepository.java @@ -1137,6 +1137,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. From 3b2802e36da20247bafa8bfe5da723c965a355e5 Mon Sep 17 00:00:00 2001 From: Kohsuke Kawaguchi Date: Wed, 29 Aug 2018 20:18:00 -0700 Subject: [PATCH 2/3] minor doc improvement --- src/main/java/org/kohsuke/github/GHInvitation.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/main/java/org/kohsuke/github/GHInvitation.java b/src/main/java/org/kohsuke/github/GHInvitation.java index 6c285fefd1..fa0cacc67c 100644 --- a/src/main/java/org/kohsuke/github/GHInvitation.java +++ b/src/main/java/org/kohsuke/github/GHInvitation.java @@ -10,6 +10,10 @@ import java.util.Map; import java.util.TreeMap; +/** + * @see GitHub#getMyInvitations() + * @see GHRepository#listInvitations() + */ public class GHInvitation extends GHObject { /*package almost final*/ GitHub root; From 92a015ca4df1ccdf0d0b7b049e951bfe571fb09c Mon Sep 17 00:00:00 2001 From: Kohsuke Kawaguchi Date: Wed, 29 Aug 2018 20:18:10 -0700 Subject: [PATCH 3/3] Clear up import statements --- src/main/java/org/kohsuke/github/GHInvitation.java | 7 ------- 1 file changed, 7 deletions(-) diff --git a/src/main/java/org/kohsuke/github/GHInvitation.java b/src/main/java/org/kohsuke/github/GHInvitation.java index fa0cacc67c..213b7af3f1 100644 --- a/src/main/java/org/kohsuke/github/GHInvitation.java +++ b/src/main/java/org/kohsuke/github/GHInvitation.java @@ -2,13 +2,6 @@ import java.io.IOException; import java.net.URL; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; -import java.util.Collections; -import java.util.List; -import java.util.Map; -import java.util.TreeMap; /** * @see GitHub#getMyInvitations()