Skip to content
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

update to User interface to see an accept invitations #1514

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/main/java/com/jcabi/github/Repo.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -187,6 +189,13 @@ public interface Repo extends JsonReadable, JsonPatchable, Comparable<Repo> {
* @since 0.15
*/
Iterable<Language> 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<String> invitees() throws IOException;

/**
* Smart Repo with extra features.
Expand Down
25 changes: 25 additions & 0 deletions src/main/java/com/jcabi/github/RtRepo.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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"));
}
return invitees;
}

@Override
public String toString() {
Expand Down
82 changes: 81 additions & 1 deletion src/main/java/com/jcabi/github/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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.
Expand All @@ -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")
)
);
}
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()
.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?
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/com/jcabi/github/mock/MkRepo.java
Original file line number Diff line number Diff line change
Expand Up @@ -322,4 +322,12 @@ private String xpath() {
);
}

/**
* Not yet implemented
*/
@Override
public Iterable<String> invitees() throws IOException {
throw new UnsupportedOperationException();
}

}
17 changes: 17 additions & 0 deletions src/main/java/com/jcabi/github/mock/MkUser.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -169,4 +170,20 @@ private String xpath() {
return String.format("/github/users/user[login='%s']", this.self);
}

/**
* Not yet implemented
*/
@Override
public Iterable<Coordinates> invitations() throws IOException {
throw new UnsupportedOperationException();
}

/**
* Not yet implemented
*/
@Override
public boolean acceptInvitation(Coordinates coords) throws IOException {
throw new UnsupportedOperationException();
}

}