-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Expose config file retrieval through a service
- Loading branch information
Showing
6 changed files
with
131 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
runtime/src/main/java/io/quarkiverse/githubapp/GitHubConfigFileProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package io.quarkiverse.githubapp; | ||
|
||
import java.util.Optional; | ||
|
||
import org.kohsuke.github.GHRepository; | ||
|
||
/** | ||
* A provider of configuration files fetched from {@link org.kohsuke.github.GHRepository GitHub repositories}. | ||
* <p> | ||
* Inject as a CDI bean. | ||
* <p> | ||
* <strong>NOTE:</strong> You generally will not need this bean when processing events, | ||
* as configuration files can be automatically injected into event listener methods, | ||
* simply by annotating a parameter with {@link ConfigFile}. | ||
* This provider is mostly useful for non-event use cases (e.g. cron jobs). | ||
* | ||
* @see ConfigFile | ||
* @see ConfigFile.Source | ||
*/ | ||
public interface GitHubConfigFileProvider { | ||
|
||
/** | ||
* Fetches the configuration file at the given path from the main branch of the given repository, | ||
* optionally (if {@code type} is not just {@link String}) deserializing it to the given type using Jackson. | ||
* <p> | ||
* <strong>NOTE:</strong> You generally will not need this method when processing events, | ||
* as configuration files can be automatically injected into event listener methods, | ||
* simply by annotating a parameter with {@link ConfigFile}. | ||
* This provider is mostly useful for non-event use cases (e.g. cron jobs). | ||
* | ||
* @param repository The GitHub code repository to retrieve the file from. | ||
* @param path The path to the file in the code repository, | ||
* either absolute (if it starts with {@code /}) or relative to {@code /.github/} (if it doesn't start with | ||
* {@code /}). | ||
* @param source Which repository to extract the file from in the case of forked repositories. | ||
* @param type The type to deserialize the file to. | ||
* @return The configuration file wrapped in an {@link java.util.Optional}, or {@link Optional#empty()} if it is missing. | ||
* @throws java.io.IOException If the configuration file cannot be retrieved. | ||
* @throws IllegalStateException If the configuration file cannot be deserialized to the given type. | ||
* @see ConfigFile | ||
* @see ConfigFile.Source | ||
*/ | ||
<T> Optional<T> fetchConfigFile(GHRepository repository, String path, ConfigFile.Source source, Class<T> type); | ||
|
||
} |
48 changes: 48 additions & 0 deletions
48
...in/java/io/quarkiverse/githubapp/runtime/RequestScopeCachingGitHubConfigFileProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package io.quarkiverse.githubapp.runtime; | ||
|
||
import java.util.Map; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
|
||
import javax.enterprise.context.RequestScoped; | ||
import javax.inject.Inject; | ||
|
||
import org.kohsuke.github.GHRepository; | ||
|
||
import io.quarkiverse.githubapp.ConfigFile; | ||
import io.quarkiverse.githubapp.GitHubConfigFileProvider; | ||
import io.quarkiverse.githubapp.runtime.config.GitHubAppRuntimeConfig; | ||
import io.quarkiverse.githubapp.runtime.github.GitHubConfigFileProviderImpl; | ||
|
||
@RequestScoped | ||
public class RequestScopeCachingGitHubConfigFileProvider { | ||
|
||
@Inject | ||
GitHubAppRuntimeConfig gitHubAppRuntimeConfig; | ||
|
||
@Inject | ||
GitHubConfigFileProvider gitHubConfigFileProvider; | ||
|
||
private final Map<String, Object> cache = new ConcurrentHashMap<>(); | ||
|
||
public Object getConfigObject(GHRepository ghRepository, String path, ConfigFile.Source source, Class<?> type) { | ||
String cacheKey = getCacheKey(ghRepository, path, source); | ||
|
||
Object cachedObject = cache.get(cacheKey); | ||
if (cachedObject != null) { | ||
return cachedObject; | ||
} | ||
|
||
return cache.computeIfAbsent(cacheKey, | ||
k -> gitHubConfigFileProvider.fetchConfigFile(ghRepository, path, source, type).orElse(null)); | ||
} | ||
|
||
private String getCacheKey(GHRepository ghRepository, String path, | ||
ConfigFile.Source source) { | ||
String fullPath = GitHubConfigFileProviderImpl.getFilePath(path.trim()); | ||
ConfigFile.Source effectiveSource = gitHubAppRuntimeConfig.getEffectiveSource(source); | ||
// we should only handle the config files of one repository in a given ConfigFileReader | ||
// as it's request scoped but let's be on the safe side | ||
return ghRepository.getFullName() + ":" + effectiveSource.name() + ":" + fullPath; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters