Skip to content

Commit 063e918

Browse files
Add non static alternative to GitHubClient scopeForInstallationId (#161)
* Add non-static alternative to GitHubClient scopeForInstallationId * Add small testcase
1 parent 90adff7 commit 063e918

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,19 @@ static String responseBodyUnchecked(final Response response) {
319319
}
320320
}
321321

322+
public GitHubClient withScopeForInstallationId(final int installationId) {
323+
if (Optional.ofNullable(privateKey).isEmpty()) {
324+
throw new RuntimeException("Installation ID scoped client needs a private key");
325+
}
326+
return new GitHubClient(
327+
client,
328+
baseUrl,
329+
null,
330+
privateKey,
331+
appId,
332+
installationId);
333+
}
334+
322335
public GitHubClient withTracer(final Tracer tracer) {
323336
this.tracer = tracer;
324337
return this;

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,11 @@
3636
import com.spotify.github.v3.exceptions.RequestNotOkException;
3737
import com.spotify.github.v3.repos.CommitItem;
3838
import com.spotify.github.v3.repos.RepositoryInvitation;
39+
40+
import java.io.File;
3941
import java.io.IOException;
4042
import java.net.URI;
43+
import java.net.URISyntaxException;
4144
import java.util.Optional;
4245
import java.util.concurrent.CompletableFuture;
4346
import java.util.concurrent.ExecutionException;
@@ -70,6 +73,19 @@ public void setUp() {
7073
github = GitHubClient.create(client, URI.create("http://bogus"), "token");
7174
}
7275

76+
@Test
77+
public void withScopedInstallationIdShouldFailWhenMissingPrivateKey() {
78+
assertThrows(RuntimeException.class, () -> github.withScopeForInstallationId(1));
79+
}
80+
81+
@Test
82+
public void testWithScopedInstallationId() throws URISyntaxException {
83+
GitHubClient org = GitHubClient.create(new URI("http://apa.bepa.cepa"), "some_key_content".getBytes(), null, null);
84+
GitHubClient scoped = org.withScopeForInstallationId(1);
85+
Assertions.assertTrue(scoped.getPrivateKey().isPresent());
86+
Assertions.assertEquals(org.getPrivateKey().get(), scoped.getPrivateKey().get());
87+
}
88+
7389
@Test
7490
public void testSearchIssue() throws Throwable {
7591

0 commit comments

Comments
 (0)