Skip to content

Commit

Permalink
Add non static alternative to GitHubClient scopeForInstallationId (#161)
Browse files Browse the repository at this point in the history
* Add non-static alternative to GitHubClient scopeForInstallationId
* Add small testcase
  • Loading branch information
dennisgranath authored Dec 12, 2023
1 parent 90adff7 commit 063e918
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/main/java/com/spotify/github/v3/clients/GitHubClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
16 changes: 16 additions & 0 deletions src/test/java/com/spotify/github/v3/clients/GitHubClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {

Expand Down

0 comments on commit 063e918

Please sign in to comment.