From eed348f5dc02295fd91158b3aa258da8243b549a Mon Sep 17 00:00:00 2001 From: Ainhoa Larumbe Date: Tue, 9 Aug 2022 18:39:35 +0100 Subject: [PATCH] add removeInvitation and listInvitation methods to repositoryClient Signed-off-by: Ainhoa Larumbe --- .../github/v3/clients/GitHubClient.java | 2 ++ .../github/v3/clients/RepositoryClient.java | 13 +++++++++ .../v3/clients/RepositoryClientTest.java | 28 +++++++++++++++++++ 3 files changed, 43 insertions(+) diff --git a/src/main/java/com/spotify/github/v3/clients/GitHubClient.java b/src/main/java/com/spotify/github/v3/clients/GitHubClient.java index fc2969cb..e75321ca 100644 --- a/src/main/java/com/spotify/github/v3/clients/GitHubClient.java +++ b/src/main/java/com/spotify/github/v3/clients/GitHubClient.java @@ -39,6 +39,7 @@ import com.spotify.github.v3.repos.FolderContent; import com.spotify.github.v3.repos.Repository; import com.spotify.github.v3.repos.Status; +import com.spotify.github.v3.repos.RepositoryInvitation; import java.io.*; import java.lang.invoke.MethodHandles; @@ -94,6 +95,7 @@ public class GitHubClient { new TypeReference<>() {}; static final TypeReference> LIST_REFERENCES = new TypeReference<>() {}; + static final TypeReference> LIST_REPOSITORY_INVITATION = new TypeReference<>() {}; private static final String GET_ACCESS_TOKEN_URL = "app/installations/%s/access_tokens"; diff --git a/src/main/java/com/spotify/github/v3/clients/RepositoryClient.java b/src/main/java/com/spotify/github/v3/clients/RepositoryClient.java index 8d167446..f9ee2a60 100644 --- a/src/main/java/com/spotify/github/v3/clients/RepositoryClient.java +++ b/src/main/java/com/spotify/github/v3/clients/RepositoryClient.java @@ -27,6 +27,7 @@ import static com.spotify.github.v3.clients.GitHubClient.LIST_PR_TYPE_REFERENCE; import static com.spotify.github.v3.clients.GitHubClient.LIST_REPOSITORY; import static com.spotify.github.v3.clients.GitHubClient.LIST_STATUS_TYPE_REFERENCE; +import static com.spotify.github.v3.clients.GitHubClient.LIST_REPOSITORY_INVITATION; import com.google.common.base.Strings; import com.google.common.collect.ImmutableMap; @@ -88,6 +89,8 @@ public class RepositoryClient { private static final String LIST_REPOSITORY_TEMPLATE = "/orgs/%s/repos"; private static final String LIST_REPOSITORIES_FOR_AUTHENTICATED_USER = "/user/repos"; private static final String REPOSITORY_COLLABORATOR = "/repos/%s/%s/collaborators/%s"; + private static final String REPOSITORY_INVITATION = "/repos/%s/%s/invitations/%s"; + private static final String REPOSITORY_INVITATIONS = "/repos/%s/%s/invitations"; private final String owner; private final String repo; private final GitHubClient github; @@ -229,6 +232,16 @@ public CompletableFuture removeCollaborator(final String user) { return github.delete(path).thenAccept(IGNORE_RESPONSE_CONSUMER); } + public CompletableFuture removeInvite(final String invitationId) { + final String path = String.format(REPOSITORY_INVITATION, owner, repo, invitationId); + return github.delete(path).thenAccept(IGNORE_RESPONSE_CONSUMER); + } + + public CompletableFuture> listInvitations() { + final String path = String.format(REPOSITORY_INVITATIONS, owner, repo); + return github.request(path, LIST_REPOSITORY_INVITATION); + } + /** * Create a webhook. * diff --git a/src/test/java/com/spotify/github/v3/clients/RepositoryClientTest.java b/src/test/java/com/spotify/github/v3/clients/RepositoryClientTest.java index a79a0fe1..f2eea5ef 100644 --- a/src/test/java/com/spotify/github/v3/clients/RepositoryClientTest.java +++ b/src/test/java/com/spotify/github/v3/clients/RepositoryClientTest.java @@ -28,6 +28,7 @@ import static com.spotify.github.v3.clients.GitHubClient.LIST_FOLDERCONTENT_TYPE_REFERENCE; import static com.spotify.github.v3.clients.GitHubClient.LIST_PR_TYPE_REFERENCE; import static com.spotify.github.v3.clients.GitHubClient.LIST_REPOSITORY; +import static com.spotify.github.v3.clients.GitHubClient.LIST_REPOSITORY_INVITATION; import static com.spotify.github.v3.clients.MockHelper.createMockResponse; import static com.spotify.github.v3.clients.RepositoryClient.STATUS_URI_TEMPLATE; import static java.lang.String.format; @@ -207,6 +208,33 @@ public void removeCollaborator() throws Exception { assertThat(capture.getValue(), is("/repos/someowner/somerepo/collaborators/user")); } + @Test + public void removeInvite() throws Exception { + CompletableFuture response = completedFuture(mock(Response.class)); + final ArgumentCaptor capture = ArgumentCaptor.forClass(String.class); + when(github.delete(capture.capture())).thenReturn(response); + + CompletableFuture deleteResponse = repoClient.removeInvite("invitation1"); + deleteResponse.get(); + + assertThat(capture.getValue(), is("/repos/someowner/somerepo/invitations/invitation1")); + } + + @Test + public void listInvites() throws Exception { + final CompletableFuture> fixture = + completedFuture( + json.fromJson("[" + getFixture("repository_invitation.json") + "]", LIST_REPOSITORY_INVITATION)); + when(github.request("/repos/someowner/somerepo/invitations", LIST_REPOSITORY_INVITATION)) + .thenReturn(fixture); + + final List invitations = repoClient.listInvitations().get(); + assertThat(invitations.size(), is(1)); + assertThat(invitations.get(0).repository().name(), is("Hello-World")); + assertThat( + invitations.get(0).inviter().login(), is("octocat")); + } + @Test public void listCommits() throws Exception { final CompletableFuture> fixture =