Skip to content

Commit

Permalink
[feature] implement Repository Invitations API
Browse files Browse the repository at this point in the history
fixes #374
  • Loading branch information
Rechi committed May 29, 2018
1 parent d61697a commit fe5ea52
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 0 deletions.
45 changes: 45 additions & 0 deletions src/main/java/org/kohsuke/github/GHInvitation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package org.kohsuke.github;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

public class GHInvitation extends GHObject {
/*package almost final*/ GitHub root;

private int id;
private GHRepository repository;
private GHUser invitee, inviter;
private String permissions;
private String html_url;

/*package*/ GHInvitation wrapUp(GitHub root) {
this.root = root;
return this;
}

/**
* Accept a repository invitation.
*/
public void accept() throws IOException {
root.retrieve().method("PATCH").to("/user/repository_invitations/" + id);
}

/**
* Decline a repository invitation.
*/
public void decline() throws IOException {
root.retrieve().method("DELETE").to("/user/repository_invitations/" + id);
}

@Override
public URL getHtmlUrl() {
return GitHub.parseURL(html_url);
}
}
16 changes: 16 additions & 0 deletions src/main/java/org/kohsuke/github/GHRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -1137,6 +1137,22 @@ public GHLabel createLabel(String name, String color) throws IOException {
.to(getApiTailUrl("labels"), GHLabel.class).wrapUp(this);
}

/**
* Lists all the invitations.
*/
public PagedIterable<GHInvitation> listInvitations() {
return new PagedIterable<GHInvitation>() {
public PagedIterator<GHInvitation> _iterator(int pageSize) {
return new PagedIterator<GHInvitation>(root.retrieve().asIterator(String.format("/repos/%s/%s/invitations", getOwnerName(), name), GHInvitation[].class, pageSize)) {
protected void wrapUp(GHInvitation[] page) {
for (GHInvitation c : page)
c.wrapUp(root);
}
};
}
};
}

/**
* Lists all the subscribers (aka watchers.)
*
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/org/kohsuke/github/GitHub.java
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,15 @@ public GHLicense getLicense(String key) throws IOException {
}

/**
* Gets complete list of open invitations for current user.
*/
public List<GHInvitation> getMyInvitations() throws IOException {
GHInvitation[] invitations = retrieve().to("/user/repository_invitations", GHInvitation[].class);
for (GHInvitation i : invitations) {
i.wrapUp(this);
}
return Arrays.asList(invitations);
}

/**
* This method returns a shallowly populated organizations.
Expand Down

0 comments on commit fe5ea52

Please sign in to comment.