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 b7d2737fa..0b6f88bb8 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; @@ -96,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().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..29617fd08 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,60 @@ 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().back() + .fetch().as(RestResponse.class) + .assertStatus(HttpURLConnection.HTTP_OK) + .as(JsonResponse.class) + .json().readArray().iterator(); + + Set coordsSet = new HashSet(); + while (iter.hasNext()) { + JsonObject invite = (JsonObject) iter.next(); + coordsSet.add( + new Coordinates.Simple( + invite.getString("name"), + invite.getJsonObject("owner").getString("login") + ) + ); + } + return coordsSet; + } + + @Override + public boolean acceptInvitation(final Coordinates coords) throws IOException { + Iterator iter = this.github().entry().uri().path("/user/repository_invitations").back().method(Request.GET) + .body().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().concat("/").concat(coords.repo()); + while (iter.hasNext() && !match) { + JsonObject invitation = (JsonObject) iter.next(); + JsonObject repository = invitation.getJsonObject("repository"); + String fullRepoName = repository.getString("full_name"); + if (fullRepoName.equals(thisCoord)) { + match = true; + idToAccept = invitation.getInt("id"); + } + } + if (match) { + RestResponse resp = this.github().entry().uri().path("/user/repository_invitations/" + idToAccept).back().method(Request.PATCH) + .body().back() + .fetch().as(RestResponse.class); + + return resp.status() == HttpURLConnection.HTTP_NO_CONTENT; + } + return false; + } + /** * 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..d7cdd9719 100644 --- a/src/main/java/com/jcabi/github/mock/MkRepo.java +++ b/src/main/java/com/jcabi/github/mock/MkRepo.java @@ -322,4 +322,12 @@ private String xpath() { ); } + /** + * Not yet implemented + */ + @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..1d3a18cba 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,20 @@ private String xpath() { return String.format("/github/users/user[login='%s']", this.self); } + /** + * Not yet implemented + */ + @Override + public Iterable invitations() throws IOException { + throw new UnsupportedOperationException(); + } + + /** + * Not yet implemented + */ + @Override + public boolean acceptInvitation(Coordinates coords) throws IOException { + throw new UnsupportedOperationException(); + } + }