-
Notifications
You must be signed in to change notification settings - Fork 142
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
Get all invitees to a specific repository #1513
base: master
Are you sure you want to change the base?
Changes from all commits
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 |
---|---|---|
|
@@ -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,57 @@ 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().set(Json.createArrayBuilder().build()).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. @ShalomGottesman Same here, why the empty Json body? |
||
.fetch().as(RestResponse.class) | ||
.assertStatus(HttpURLConnection.HTTP_OK) | ||
.as(JsonResponse.class) | ||
.json().readArray().iterator(); | ||
|
||
Set<Coordinates> coordsSet = new HashSet<Coordinates>(); | ||
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); | ||
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. @ShalomGottesman I think you can simplify these 4 lines to just: coordsSet.add(
new Coordinates.Simple(
obj.getString("name"),
obj.getJsonObject("owner").getString("login")
)
); |
||
} | ||
return coordsSet; | ||
} | ||
|
||
public boolean acceptInvitation(Coordinates coords) throws IOException { | ||
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. @ShalomGottesman You forgot the |
||
Iterator<JsonValue> 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? | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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<Coordinates> invitations() throws IOException { | ||
throw new UnsupportedOperationException(); | ||
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. @ShalomGottesman Can you also specify "Not yet implemented."? |
||
} | ||
|
||
@Override | ||
public boolean acceptInvitation(Coordinates coords) throws IOException { | ||
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. @ShalomGottesman Can you make Final variables are immutable: this means that an object with immutable fields is thread-safe by default and also a method that works only with final variables will be easier to understand, there will be only one initialization per variable :) |
||
throw new UnsupportedOperationException(); | ||
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. @ShalomGottesman same here pls |
||
} | ||
|
||
} |
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.
@ShalomGottesman Why do you specify an empty Json body? I am pretty sure it is not needed :)