Skip to content

Commit 4d6c0db

Browse files
authored
add removeInvitation and listInvitation methods to repositoryClient (#113)
Signed-off-by: Ainhoa Larumbe <[email protected]>
1 parent 4595f39 commit 4d6c0db

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

src/main/java/com/spotify/github/v3/clients/GitHubClient.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import com.spotify.github.v3.repos.FolderContent;
4040
import com.spotify.github.v3.repos.Repository;
4141
import com.spotify.github.v3.repos.Status;
42+
import com.spotify.github.v3.repos.RepositoryInvitation;
4243

4344
import java.io.*;
4445
import java.lang.invoke.MethodHandles;
@@ -94,6 +95,7 @@ public class GitHubClient {
9495
new TypeReference<>() {};
9596
static final TypeReference<List<Reference>> LIST_REFERENCES =
9697
new TypeReference<>() {};
98+
static final TypeReference<List<RepositoryInvitation>> LIST_REPOSITORY_INVITATION = new TypeReference<>() {};
9799

98100
private static final String GET_ACCESS_TOKEN_URL = "app/installations/%s/access_tokens";
99101

src/main/java/com/spotify/github/v3/clients/RepositoryClient.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import static com.spotify.github.v3.clients.GitHubClient.LIST_PR_TYPE_REFERENCE;
2828
import static com.spotify.github.v3.clients.GitHubClient.LIST_REPOSITORY;
2929
import static com.spotify.github.v3.clients.GitHubClient.LIST_STATUS_TYPE_REFERENCE;
30+
import static com.spotify.github.v3.clients.GitHubClient.LIST_REPOSITORY_INVITATION;
3031

3132
import com.google.common.base.Strings;
3233
import com.google.common.collect.ImmutableMap;
@@ -88,6 +89,8 @@ public class RepositoryClient {
8889
private static final String LIST_REPOSITORY_TEMPLATE = "/orgs/%s/repos";
8990
private static final String LIST_REPOSITORIES_FOR_AUTHENTICATED_USER = "/user/repos";
9091
private static final String REPOSITORY_COLLABORATOR = "/repos/%s/%s/collaborators/%s";
92+
private static final String REPOSITORY_INVITATION = "/repos/%s/%s/invitations/%s";
93+
private static final String REPOSITORY_INVITATIONS = "/repos/%s/%s/invitations";
9194
private final String owner;
9295
private final String repo;
9396
private final GitHubClient github;
@@ -229,6 +232,16 @@ public CompletableFuture<Void> removeCollaborator(final String user) {
229232
return github.delete(path).thenAccept(IGNORE_RESPONSE_CONSUMER);
230233
}
231234

235+
public CompletableFuture<Void> removeInvite(final String invitationId) {
236+
final String path = String.format(REPOSITORY_INVITATION, owner, repo, invitationId);
237+
return github.delete(path).thenAccept(IGNORE_RESPONSE_CONSUMER);
238+
}
239+
240+
public CompletableFuture<List<RepositoryInvitation>> listInvitations() {
241+
final String path = String.format(REPOSITORY_INVITATIONS, owner, repo);
242+
return github.request(path, LIST_REPOSITORY_INVITATION);
243+
}
244+
232245
/**
233246
* Create a webhook.
234247
*

src/test/java/com/spotify/github/v3/clients/RepositoryClientTest.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import static com.spotify.github.v3.clients.GitHubClient.LIST_FOLDERCONTENT_TYPE_REFERENCE;
2929
import static com.spotify.github.v3.clients.GitHubClient.LIST_PR_TYPE_REFERENCE;
3030
import static com.spotify.github.v3.clients.GitHubClient.LIST_REPOSITORY;
31+
import static com.spotify.github.v3.clients.GitHubClient.LIST_REPOSITORY_INVITATION;
3132
import static com.spotify.github.v3.clients.MockHelper.createMockResponse;
3233
import static com.spotify.github.v3.clients.RepositoryClient.STATUS_URI_TEMPLATE;
3334
import static java.lang.String.format;
@@ -207,6 +208,33 @@ public void removeCollaborator() throws Exception {
207208
assertThat(capture.getValue(), is("/repos/someowner/somerepo/collaborators/user"));
208209
}
209210

211+
@Test
212+
public void removeInvite() throws Exception {
213+
CompletableFuture<Response> response = completedFuture(mock(Response.class));
214+
final ArgumentCaptor<String> capture = ArgumentCaptor.forClass(String.class);
215+
when(github.delete(capture.capture())).thenReturn(response);
216+
217+
CompletableFuture<Void> deleteResponse = repoClient.removeInvite("invitation1");
218+
deleteResponse.get();
219+
220+
assertThat(capture.getValue(), is("/repos/someowner/somerepo/invitations/invitation1"));
221+
}
222+
223+
@Test
224+
public void listInvites() throws Exception {
225+
final CompletableFuture<List<RepositoryInvitation>> fixture =
226+
completedFuture(
227+
json.fromJson("[" + getFixture("repository_invitation.json") + "]", LIST_REPOSITORY_INVITATION));
228+
when(github.request("/repos/someowner/somerepo/invitations", LIST_REPOSITORY_INVITATION))
229+
.thenReturn(fixture);
230+
231+
final List<RepositoryInvitation> invitations = repoClient.listInvitations().get();
232+
assertThat(invitations.size(), is(1));
233+
assertThat(invitations.get(0).repository().name(), is("Hello-World"));
234+
assertThat(
235+
invitations.get(0).inviter().login(), is("octocat"));
236+
}
237+
210238
@Test
211239
public void listCommits() throws Exception {
212240
final CompletableFuture<List<CommitItem>> fixture =

0 commit comments

Comments
 (0)