Skip to content

Commit 6d6832e

Browse files
authored
Merge pull request #632 from ryandens/ryandens/github-customizer
Add GitHubCustomizer
2 parents a3e81cd + 97244d2 commit 6d6832e

File tree

3 files changed

+68
-3
lines changed

3 files changed

+68
-3
lines changed

docs/modules/ROOT/pages/developer-reference.adoc

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,25 @@ You can use the constants present in `io.quarkiverse.githubapp.Credentials` for
370370
That is all you need to do:
371371
Quarkus GitHub App will then automatically use the values provided by the `CredentialsProvider` for the private key and the webhook secret.
372372

373+
== Customizing the GitHub clients
374+
375+
You can customize the GitHub clients initialized by Quarkus GitHub App
376+
by creating a CDI bean implementing `io.quarkiverse.githubapp.GitHubCustomizer`.
377+
378+
For instance:
379+
380+
[source,java]
381+
----
382+
@Singleton
383+
public class MyGitHubCustomizer implements GitHubCustomizer {
384+
385+
@Override
386+
public void customize(GitHubBuilder builder) {
387+
// call methods of the builder
388+
}
389+
}
390+
----
391+
373392
== Architecture Overview
374393

375394
image::architecture.png[Architecture]
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package io.quarkiverse.githubapp;
2+
3+
import org.kohsuke.github.GitHubBuilder;
4+
5+
/**
6+
* Provide a CDI bean implementing this interface to customize {@link GitHubBuilder} used to create the
7+
* {@link org.kohsuke.github.GitHub} instances.
8+
*/
9+
public interface GitHubCustomizer {
10+
11+
/**
12+
* Customize the {@link GitHubBuilder} with various options.
13+
* <p>
14+
* Note that customizations that use {@link GitHubBuilder#withAppInstallationToken(String)} and
15+
* {@link GitHubBuilder#withEndpoint(String)} are eventually overridden by the
16+
* {@link io.quarkiverse.githubapp.runtime.github.GitHubService}. Installation tokens are created and cached by the service.
17+
* <p>
18+
* To specify a custom endpoint, use configuration properties {@code quarkus.github-app.instance-endpoint} or
19+
* {@code quarkus.github-app.rest-api-endpoint}.
20+
*
21+
* @param builder to customize
22+
*/
23+
void customize(GitHubBuilder builder);
24+
}

runtime/src/main/java/io/quarkiverse/githubapp/runtime/github/GitHubService.java

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import java.util.concurrent.TimeUnit;
1010

1111
import jakarta.enterprise.context.ApplicationScoped;
12+
import jakarta.enterprise.inject.Instance;
1213
import jakarta.inject.Inject;
1314

1415
import org.kohsuke.github.GHAppInstallationToken;
@@ -23,6 +24,7 @@
2324
import com.github.benmanes.caffeine.cache.LoadingCache;
2425

2526
import io.quarkiverse.githubapp.GitHubClientProvider;
27+
import io.quarkiverse.githubapp.GitHubCustomizer;
2628
import io.quarkiverse.githubapp.runtime.config.CheckedConfigProvider;
2729
import io.quarkiverse.githubapp.runtime.signing.JwtTokenCreator;
2830
import io.smallrye.graphql.client.dynamic.api.DynamicGraphQLClient;
@@ -33,16 +35,26 @@ public class GitHubService implements GitHubClientProvider {
3335

3436
private static final String AUTHORIZATION_HEADER = "Authorization";
3537
private static final String AUTHORIZATION_HEADER_BEARER = "Bearer %s";
38+
private static final GitHubCustomizer NOOP_GITHUB_CUSTOMIZER = new GitHubCustomizer() {
39+
40+
@Override
41+
public void customize(GitHubBuilder builder) {
42+
}
43+
};
3644

3745
private final CheckedConfigProvider checkedConfigProvider;
3846

3947
private final LoadingCache<Long, CachedInstallationToken> installationTokenCache;
4048

4149
private final JwtTokenCreator jwtTokenCreator;
4250
private final GitHubConnector gitHubConnector;
51+
private final GitHubCustomizer githubCustomizer;
4352

4453
@Inject
45-
public GitHubService(CheckedConfigProvider checkedConfigProvider, JwtTokenCreator jwtTokenCreator) {
54+
public GitHubService(
55+
CheckedConfigProvider checkedConfigProvider,
56+
JwtTokenCreator jwtTokenCreator,
57+
Instance<GitHubCustomizer> gitHubCustomizer) {
4658
this.checkedConfigProvider = checkedConfigProvider;
4759
this.jwtTokenCreator = jwtTokenCreator;
4860
this.installationTokenCache = Caffeine.newBuilder()
@@ -74,6 +86,8 @@ public long expireAfterRead(Long installationId, CachedInstallationToken cachedI
7486
.build(new CreateInstallationToken());
7587
this.gitHubConnector = new HttpClientGitHubConnector(
7688
HttpClient.newBuilder().version(Version.HTTP_1_1).followRedirects(HttpClient.Redirect.NEVER).build());
89+
// if the customizer is not resolvable, we use a no-op customizer
90+
githubCustomizer = gitHubCustomizer.isResolvable() ? gitHubCustomizer.get() : NOOP_GITHUB_CUSTOMIZER;
7791
}
7892

7993
@Override
@@ -126,7 +140,11 @@ private GitHub createInstallationClient(long installationId) throws IOException
126140
CachedInstallationToken installationToken = installationTokenCache.get(installationId);
127141

128142
final GitHubBuilder gitHubBuilder = new GitHubBuilder()
129-
.withConnector(gitHubConnector)
143+
.withConnector(gitHubConnector);
144+
// apply customizations
145+
githubCustomizer.customize(gitHubBuilder);
146+
// configure mandatory defaults
147+
gitHubBuilder
130148
.withAppInstallationToken(installationToken.getToken())
131149
.withEndpoint(checkedConfigProvider.restApiEndpoint());
132150

@@ -194,7 +212,11 @@ private GitHub createApplicationGitHub() {
194212

195213
try {
196214
final GitHubBuilder gitHubBuilder = new GitHubBuilder()
197-
.withConnector(gitHubConnector)
215+
.withConnector(gitHubConnector);
216+
// apply customizations
217+
githubCustomizer.customize(gitHubBuilder);
218+
// configure mandatory defaults
219+
gitHubBuilder
198220
.withJwtToken(jwtToken)
199221
.withEndpoint(checkedConfigProvider.restApiEndpoint());
200222

0 commit comments

Comments
 (0)