From 063e918d324c40d67a482a1d9e6b666342d57efa Mon Sep 17 00:00:00 2001 From: Dennis Granath Date: Tue, 12 Dec 2023 10:44:12 +0100 Subject: [PATCH] Add non static alternative to GitHubClient scopeForInstallationId (#161) * Add non-static alternative to GitHubClient scopeForInstallationId * Add small testcase --- .../spotify/github/v3/clients/GitHubClient.java | 13 +++++++++++++ .../github/v3/clients/GitHubClientTest.java | 16 ++++++++++++++++ 2 files changed, 29 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 13d50be3..4f627434 100644 --- a/src/main/java/com/spotify/github/v3/clients/GitHubClient.java +++ b/src/main/java/com/spotify/github/v3/clients/GitHubClient.java @@ -319,6 +319,19 @@ static String responseBodyUnchecked(final Response response) { } } + public GitHubClient withScopeForInstallationId(final int installationId) { + if (Optional.ofNullable(privateKey).isEmpty()) { + throw new RuntimeException("Installation ID scoped client needs a private key"); + } + return new GitHubClient( + client, + baseUrl, + null, + privateKey, + appId, + installationId); + } + public GitHubClient withTracer(final Tracer tracer) { this.tracer = tracer; return this; diff --git a/src/test/java/com/spotify/github/v3/clients/GitHubClientTest.java b/src/test/java/com/spotify/github/v3/clients/GitHubClientTest.java index d030b17b..19bae27d 100644 --- a/src/test/java/com/spotify/github/v3/clients/GitHubClientTest.java +++ b/src/test/java/com/spotify/github/v3/clients/GitHubClientTest.java @@ -36,8 +36,11 @@ import com.spotify.github.v3.exceptions.RequestNotOkException; import com.spotify.github.v3.repos.CommitItem; import com.spotify.github.v3.repos.RepositoryInvitation; + +import java.io.File; import java.io.IOException; import java.net.URI; +import java.net.URISyntaxException; import java.util.Optional; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutionException; @@ -70,6 +73,19 @@ public void setUp() { github = GitHubClient.create(client, URI.create("http://bogus"), "token"); } + @Test + public void withScopedInstallationIdShouldFailWhenMissingPrivateKey() { + assertThrows(RuntimeException.class, () -> github.withScopeForInstallationId(1)); + } + + @Test + public void testWithScopedInstallationId() throws URISyntaxException { + GitHubClient org = GitHubClient.create(new URI("http://apa.bepa.cepa"), "some_key_content".getBytes(), null, null); + GitHubClient scoped = org.withScopeForInstallationId(1); + Assertions.assertTrue(scoped.getPrivateKey().isPresent()); + Assertions.assertEquals(org.getPrivateKey().get(), scoped.getPrivateKey().get()); + } + @Test public void testSearchIssue() throws Throwable {