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

Get all invitees to a specific repository #1513

Open
wants to merge 2 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
27 changes: 25 additions & 2 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 @@ -79,8 +88,6 @@ final class RtRepo implements Repo {
* Repository coordinates.
*/
private final transient Coordinates coords;



/**
* Public ctor.
Expand All @@ -98,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().set(Json.createArrayBuilder().build()).back()
Copy link
Member

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 :)

.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
79 changes: 78 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,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()
Copy link
Member

Choose a reason for hiding this comment

The 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);
Copy link
Member

Choose a reason for hiding this comment

The 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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ShalomGottesman You forgot the @Override annotation

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?
Expand Down
5 changes: 5 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,9 @@ private String xpath() {
);
}

@Override
public Iterable<String> invitees() throws IOException {
throw new UnsupportedOperationException();
}

}
11 changes: 11 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,14 @@ private String xpath() {
return String.format("/github/users/user[login='%s']", this.self);
}

@Override
public Iterable<Coordinates> invitations() throws IOException {
throw new UnsupportedOperationException();
Copy link
Member

Choose a reason for hiding this comment

The 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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ShalomGottesman Can you make coorde final? Generally, it's good to work only with final variables and parameters.

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();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ShalomGottesman same here pls

}

}