From 392b5059ece0280f02e1fe33901ced19e6c23656 Mon Sep 17 00:00:00 2001 From: Annelle de Jager <137896331+annelled@users.noreply.github.com> Date: Fri, 14 Jun 2024 13:53:45 +0100 Subject: [PATCH] Add support to get a user installation for the authenticated app (#191) * Fix merge with master conflicts * Add support to get a user installation for the authenticated app * Revert tag changes in pom after master merge * Create the github app client from the user client and add tests --- .../github/v3/clients/GitHubClient.java | 9 ++++-- .../github/v3/clients/GithubAppClient.java | 10 ++++++ .../spotify/github/v3/clients/UserClient.java | 12 +++++-- .../github/v3/clients/UserClientTest.java | 31 +++++++++++++++++-- 4 files changed, 55 insertions(+), 7 deletions(-) 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 f32dccdd..81a72bf9 100644 --- a/src/main/java/com/spotify/github/v3/clients/GitHubClient.java +++ b/src/main/java/com/spotify/github/v3/clients/GitHubClient.java @@ -431,8 +431,13 @@ public OrganisationClient createOrganisationClient(final String org) { return OrganisationClient.create(this, org); } - public UserClient createUserClient() { - return UserClient.create(this); + /** + * Create user API client + * + * @return user API client + */ + public UserClient createUserClient(final String owner) { + return UserClient.create(this, owner); } Json json() { diff --git a/src/main/java/com/spotify/github/v3/clients/GithubAppClient.java b/src/main/java/com/spotify/github/v3/clients/GithubAppClient.java index 9198fc60..02845801 100644 --- a/src/main/java/com/spotify/github/v3/clients/GithubAppClient.java +++ b/src/main/java/com/spotify/github/v3/clients/GithubAppClient.java @@ -45,6 +45,7 @@ public class GithubAppClient { refer to the organisation in the installation endpoint */ private static final String GET_INSTALLATION_ORG_URL = "/orgs/%s/installation"; + private static final String GET_INSTALLATION_USER_URL = "/users/%s/installation"; private final GitHubClient github; private final String owner; @@ -114,6 +115,15 @@ private CompletableFuture getOrgInstallation() { String.format(GET_INSTALLATION_ORG_URL, owner), Installation.class); } + /** + * Get an installation of a user + * @return an Installation + */ + public CompletableFuture getUserInstallation() { + return github.request( + String.format(GET_INSTALLATION_USER_URL, owner), Installation.class); + } + /** * Authenticates as an installation * diff --git a/src/main/java/com/spotify/github/v3/clients/UserClient.java b/src/main/java/com/spotify/github/v3/clients/UserClient.java index 3ea24979..0d48e027 100644 --- a/src/main/java/com/spotify/github/v3/clients/UserClient.java +++ b/src/main/java/com/spotify/github/v3/clients/UserClient.java @@ -27,15 +27,21 @@ public class UserClient { public static final int NO_CONTENT = 204; private final GitHubClient github; + private final String owner; private static final String SUSPEND_USER_TEMPLATE = "/users/%s/suspended"; - UserClient(final GitHubClient github) { + UserClient(final GitHubClient github, final String owner) { this.github = github; + this.owner = owner; } - static UserClient create(final GitHubClient github) { - return new UserClient(github); + static UserClient create(final GitHubClient github, final String owner) { + return new UserClient(github, owner); + } + + public GithubAppClient createGithubAppClient() { + return new GithubAppClient(this.github, this.owner); } /** diff --git a/src/test/java/com/spotify/github/v3/clients/UserClientTest.java b/src/test/java/com/spotify/github/v3/clients/UserClientTest.java index 0211769b..ce8e7259 100644 --- a/src/test/java/com/spotify/github/v3/clients/UserClientTest.java +++ b/src/test/java/com/spotify/github/v3/clients/UserClientTest.java @@ -19,16 +19,25 @@ */ package com.spotify.github.v3.clients; +import static com.google.common.io.Resources.getResource; +import static java.nio.charset.Charset.defaultCharset; import static java.util.concurrent.CompletableFuture.completedFuture; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.core.Is.is; import static org.junit.jupiter.api.Assertions.*; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; +import com.google.common.io.Resources; import com.spotify.github.jackson.Json; +import com.spotify.github.v3.checks.Installation; import com.spotify.github.v3.user.requests.ImmutableSuspensionReason; + +import java.io.IOException; import java.util.concurrent.CompletableFuture; + import okhttp3.Response; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -37,12 +46,17 @@ public class UserClientTest { private GitHubClient github; private UserClient userClient; + private String owner = "github"; + private Json json; + private static String getFixture(String resource) throws IOException { + return Resources.toString(getResource(TeamClientTest.class, resource), defaultCharset()); + } @BeforeEach public void setUp() { github = mock(GitHubClient.class); - userClient = new UserClient(github); - Json json = Json.create(); + userClient = new UserClient(github, owner); + json = Json.create(); when(github.json()).thenReturn(json); } @@ -81,4 +95,17 @@ public void testUnSuspendUserFailure() throws Exception { final CompletableFuture result = userClient.unSuspendUser("username", ImmutableSuspensionReason.builder().reason("That's why").build()); assertFalse(result.get()); } + + @Test + public void testAppClient() throws Exception { + final GithubAppClient githubAppClient = userClient.createGithubAppClient(); + final CompletableFuture fixture = + completedFuture(json.fromJson(getFixture("../githubapp/installation.json"), Installation.class)); + when(github.request("/users/github/installation", Installation.class)).thenReturn(fixture); + + final Installation installation = githubAppClient.getUserInstallation().get(); + + assertThat(installation.id(), is(1)); + assertThat(installation.account().login(), is("github")); + } }