diff --git a/src/main/java/com/jcabi/github/Repo.java b/src/main/java/com/jcabi/github/Repo.java index ec46ed5d6..596d02d0c 100644 --- a/src/main/java/com/jcabi/github/Repo.java +++ b/src/main/java/com/jcabi/github/Repo.java @@ -32,6 +32,8 @@ import com.jcabi.aspects.Immutable; import com.jcabi.aspects.Loggable; import java.io.IOException; +import java.util.Set; + import javax.json.JsonObject; import javax.json.JsonValue; import lombok.EqualsAndHashCode; @@ -187,6 +189,13 @@ public interface Repo extends JsonReadable, JsonPatchable, Comparable { * @since 0.15 */ Iterable languages() throws IOException; + + /** + * Get all invitees to this repository + * @return an iterable instance of all invitees to this repository in string form + * @throws IOException If there is any I/O problem + */ + public Iterable invitees() throws IOException; /** * Smart Repo with extra features. diff --git a/src/main/java/com/jcabi/github/RtRepo.java b/src/main/java/com/jcabi/github/RtRepo.java index e986ecadc..c48d0cb17 100644 --- a/src/main/java/com/jcabi/github/RtRepo.java +++ b/src/main/java/com/jcabi/github/RtRepo.java @@ -32,10 +32,19 @@ import com.jcabi.aspects.Immutable; import com.jcabi.aspects.Loggable; import com.jcabi.http.Request; +import com.jcabi.http.response.JsonResponse; +import com.jcabi.http.response.RestResponse; + import java.io.IOException; +import java.net.HttpURLConnection; import java.util.ArrayList; +import java.util.HashSet; +import java.util.Iterator; import java.util.List; import java.util.Map; +import java.util.Set; + +import javax.json.Json; import javax.json.JsonObject; import javax.json.JsonValue; import lombok.EqualsAndHashCode; @@ -79,8 +88,6 @@ final class RtRepo implements Repo { * Repository coordinates. */ private final transient Coordinates coords; - - /** * Public ctor. @@ -98,6 +105,22 @@ final class RtRepo implements Repo { .path(this.coords.repo()) .back(); } + + public Iterable invitees() throws IOException { + Iterator iter = this.request.uri().path("/invitations").back().method(Request.GET) + .body().set(Json.createArrayBuilder().build()).back() + .fetch().as(RestResponse.class) + .assertStatus(HttpURLConnection.HTTP_OK) + .as(JsonResponse.class) + .json().readArray().iterator(); + + Set invitees = new HashSet(); + while (iter.hasNext()) { + JsonObject val = (JsonObject) iter.next(); + invitees.add(val.getJsonObject("invitee").getString("login")); + } + return invitees; + } @Override public String toString() { diff --git a/src/main/java/com/jcabi/github/User.java b/src/main/java/com/jcabi/github/User.java index c8dfb5de0..9f42173dd 100644 --- a/src/main/java/com/jcabi/github/User.java +++ b/src/main/java/com/jcabi/github/User.java @@ -31,11 +31,23 @@ import com.jcabi.aspects.Immutable; import com.jcabi.aspects.Loggable; +import com.jcabi.http.Request; +import com.jcabi.http.response.JsonResponse; +import com.jcabi.http.response.RestResponse; + import java.io.IOException; +import java.net.HttpURLConnection; import java.net.URL; import java.text.ParseException; import java.util.Date; +import java.util.HashSet; +import java.util.Iterator; +import java.util.Set; + +import javax.json.Json; import javax.json.JsonObject; +import javax.json.JsonValue; + import lombok.EqualsAndHashCode; import lombok.ToString; @@ -105,6 +117,21 @@ public interface User extends JsonReadable, JsonPatchable { * receiving response occurs. */ void markAsRead(final Date lastread) throws IOException; + + /** + * Get all invitations of this user + * @return iterable list of repository coordinates that this user is invited to + * @throws IOException + */ + Iterable invitations() throws IOException; + + /** + * accept invitation to repository + * @param coords coordinates of repository to accept invitation to + * @return true if invitation was successfully accepted + * @throws IOException + */ + public boolean acceptInvitation(Coordinates coords) throws IOException; /** * Smart user with extra features. @@ -131,7 +158,57 @@ final class Smart implements User { public Smart(final User usr) { this.user = usr; this.jsn = new SmartJson(usr); - } + } + + @Override + public Iterable invitations() throws IOException { + Iterator iter = this.github().entry().uri().path("/user/repository_invitations").back().method(Request.GET) + .body().set(Json.createArrayBuilder().build()).back() + .fetch().as(RestResponse.class) + .assertStatus(HttpURLConnection.HTTP_OK) + .as(JsonResponse.class) + .json().readArray().iterator(); + + Set coordsSet = new HashSet(); + while (iter.hasNext()) { + JsonObject obj = (JsonObject) iter.next(); + String repoName = obj.getString("name"); + String owner = obj.getJsonObject("owner").getString("login"); + Coordinates coords = new Coordinates.Simple(owner, repoName); + coordsSet.add(coords); + } + return coordsSet; + } + + public boolean acceptInvitation(Coordinates coords) throws IOException { + Iterator iter = this.github().entry().uri().path("/user/repository_invitations").back().method(Request.GET) + .body().set(Json.createArrayBuilder().build()).back() + .fetch().as(RestResponse.class) + .assertStatus(HttpURLConnection.HTTP_OK) + .as(JsonResponse.class) + .json().readArray().iterator(); + int idToAccept = 0; + boolean match = false; + String thisCoord = coords.user() +"/"+ coords.repo(); + while (iter.hasNext() && !match) { + JsonObject obj = (JsonObject) iter.next(); + JsonObject repository = obj.getJsonObject("repository"); + String fullRepoName = repository.getString("full_name"); + if (fullRepoName.equals(thisCoord)) { + match = true; + idToAccept = obj.getInt("id"); + } + } + if (!match) { + return false; + } + RestResponse resp = this.github().entry().uri().path("/user/repository_invitations/" + idToAccept).back().method(Request.PATCH) + .body().set(Json.createObjectBuilder().build()).back() + .fetch().as(RestResponse.class); + + return resp.status() == HttpURLConnection.HTTP_NO_CONTENT; + } + /** * Does it exist in GitHub? diff --git a/src/main/java/com/jcabi/github/mock/MkRepo.java b/src/main/java/com/jcabi/github/mock/MkRepo.java index 9dccb3a7d..1fd97d589 100644 --- a/src/main/java/com/jcabi/github/mock/MkRepo.java +++ b/src/main/java/com/jcabi/github/mock/MkRepo.java @@ -322,4 +322,9 @@ private String xpath() { ); } + @Override + public Iterable invitees() throws IOException { + throw new UnsupportedOperationException(); + } + } diff --git a/src/main/java/com/jcabi/github/mock/MkUser.java b/src/main/java/com/jcabi/github/mock/MkUser.java index e7f557474..b16167566 100644 --- a/src/main/java/com/jcabi/github/mock/MkUser.java +++ b/src/main/java/com/jcabi/github/mock/MkUser.java @@ -31,6 +31,7 @@ import com.jcabi.aspects.Immutable; import com.jcabi.aspects.Loggable; +import com.jcabi.github.Coordinates; import com.jcabi.github.Github; import com.jcabi.github.Notifications; import com.jcabi.github.PublicKeys; @@ -169,4 +170,14 @@ private String xpath() { return String.format("/github/users/user[login='%s']", this.self); } + @Override + public Iterable invitations() throws IOException { + throw new UnsupportedOperationException(); + } + + @Override + public boolean acceptInvitation(Coordinates coords) throws IOException { + throw new UnsupportedOperationException(); + } + }