-
Notifications
You must be signed in to change notification settings - Fork 143
update to User interface to see an accept invitations #1514
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
61677eb
ccf2e93
21fcfbd
fe60bee
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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<String> invitees() throws IOException { | ||
| Iterator<JsonValue> 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<String> invitees = new HashSet<String>(); | ||
| while (iter.hasNext()) { | ||
| JsonObject val = (JsonObject) iter.next(); | ||
| invitees.add(val.getJsonObject("invitee").getString("login")); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Indentation in this method mixes tabs and spaces. The rest of the file uses four-space indent only; please normalize to match before merge. |
||
| } | ||
| return invitees; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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<Coordinates> 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<Coordinates> invitations() throws IOException { | ||
| Iterator<JsonValue> 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<Coordinates> coordsSet = new HashSet<Coordinates>(); | ||
| while (iter.hasNext()) { | ||
| JsonObject invite = (JsonObject) iter.next(); | ||
| coordsSet.add( | ||
| new Coordinates.Simple( | ||
| invite.getString("name"), | ||
| invite.getJsonObject("owner").getString("login") | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Arguments are swapped. Coordinates.Simple takes (user, repo), so this should be (owner.login, name), not (name, owner.login). The current order yields coordinates whose user() returns the repo name. |
||
| ) | ||
| ); | ||
| } | ||
| return coordsSet; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean acceptInvitation(final Coordinates coords) throws IOException { | ||
| Iterator<JsonValue> iter = this.github().entry().uri().path("/user/repository_invitations").back().method(Request.GET) | ||
| .body().back() | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. body().back() on a GET adds nothing. Drop the body() call here and in invitations() above; the request is already a GET and the body builder is no longer setting any payload. |
||
| .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? | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No test covers RtRepo.invitees(). Add an RtRepoTest case that mocks the /invitations endpoint with MkContainer and asserts the parsed login set, otherwise the parsing of invitee.login is unverified.