Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add options to define the repository where we read config files #327

Merged
merged 1 commit into from
Jul 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import org.objectweb.asm.Opcodes;

import io.quarkiverse.githubapi.deployment.GitHubApiClassWithBridgeMethodsBuildItem;
import io.quarkiverse.githubapp.ConfigFile;
import io.quarkiverse.githubapp.GitHubEvent;
import io.quarkiverse.githubapp.deployment.DispatchingConfiguration.EventAnnotation;
import io.quarkiverse.githubapp.deployment.DispatchingConfiguration.EventAnnotationLiteral;
Expand Down Expand Up @@ -237,7 +238,7 @@ void generateClasses(CombinedIndexBuildItem combinedIndex, LaunchModeBuildItem l

ClassOutput beanClassOutput = new GeneratedBeanGizmoAdaptor(generatedBeans);
generateDispatcher(beanClassOutput, launchMode, dispatchingConfiguration, reflectiveClasses);
generateMultiplexers(beanClassOutput, dispatchingConfiguration, reflectiveClasses);
generateMultiplexers(beanClassOutput, index, dispatchingConfiguration, reflectiveClasses);
}

@BuildStep
Expand Down Expand Up @@ -551,6 +552,7 @@ private static void generateDispatcher(ClassOutput beanClassOutput,
* </ul>
*/
private static void generateMultiplexers(ClassOutput beanClassOutput,
IndexView index,
DispatchingConfiguration dispatchingConfiguration,
BuildProducer<ReflectiveClassBuildItem> reflectiveClasses) {
for (Entry<DotName, TreeSet<EventDispatchingMethod>> eventDispatchingMethodsEntry : dispatchingConfiguration
Expand Down Expand Up @@ -757,10 +759,12 @@ private static void generateMultiplexers(ClassOutput beanClassOutput,
payloadRh);
ResultHandle configObject = methodCreator.invokeVirtualMethod(
MethodDescriptor.ofMethod(ConfigFileReader.class, "getConfigObject", Object.class,
GHRepository.class, String.class, Class.class),
GHRepository.class, String.class, ConfigFile.Source.class, Class.class),
configFileReaderRh,
ghRepositoryRh,
methodCreator.load(configFileAnnotationInstance.value().asString()),
methodCreator.load(ConfigFile.Source
.valueOf(configFileAnnotationInstance.valueWithDefault(index, "source").asEnum())),
methodCreator.loadClass(configObjectType));
configObject = methodCreator.checkCast(configObject, configObjectType);

Expand Down
2 changes: 1 addition & 1 deletion docs/modules/ROOT/pages/includes/attributes.adoc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
:quarkus-version: 2.10.1.Final
:quarkus-version: 2.10.2.Final
:quarkus-github-app-version: 1.9.0

:github-api-javadoc-root-url: https://github-api.kohsuke.org/apidocs/org/kohsuke/github
Expand Down
9 changes: 9 additions & 0 deletions docs/modules/ROOT/pages/includes/quarkus-github-app.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ The GitHub name of the application.
|


a| [[quarkus-github-app_quarkus.github-app.read-config-files-from-source-repository]]`link:#quarkus-github-app_quarkus.github-app.read-config-files-from-source-repository[quarkus.github-app.read-config-files-from-source-repository]`

[.description]
--
Read the configuration files from the source repository in case of a fork.
--|boolean
|`false`


a| [[quarkus-github-app_quarkus.github-app.private-key]]`link:#quarkus-github-app_quarkus.github-app.private-key[quarkus.github-app.private-key]`

[.description]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,12 @@
public @interface ConfigFile {

String value();

Source source() default Source.DEFAULT;

public enum Source {
DEFAULT,
SOURCE_REPOSITORY,
CURRENT_REPOSITORY;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.quarkiverse.githubapp.runtime;

import java.io.IOException;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
Expand All @@ -15,7 +16,9 @@

import com.fasterxml.jackson.databind.ObjectMapper;

import io.quarkiverse.githubapp.ConfigFile;
import io.quarkiverse.githubapp.runtime.UtilsProducer.Yaml;
import io.quarkiverse.githubapp.runtime.config.GitHubAppRuntimeConfig;
import io.quarkiverse.githubapp.runtime.github.GitHubFileDownloader;

@RequestScoped
Expand All @@ -31,6 +34,9 @@ public class ConfigFileReader {

private final Map<String, Object> cache = new ConcurrentHashMap<>();

@Inject
GitHubAppRuntimeConfig gitHubAppRuntimeConfig;

@Inject
GitHubFileDownloader gitHubFileDownloader;

Expand All @@ -41,16 +47,19 @@ public class ConfigFileReader {
@Yaml
ObjectMapper yamlObjectMapper;

public Object getConfigObject(GHRepository ghRepository, String path, Class<?> type) {
public Object getConfigObject(GHRepository ghRepository, String path, ConfigFile.Source source, Class<?> type) {
GHRepository configGHRepository = getConfigRepository(ghRepository, source,
gitHubAppRuntimeConfig.readConfigFilesFromSourceRepository, path);

String fullPath = getFilePath(path.trim());
String cacheKey = getCacheKey(ghRepository, fullPath);
String cacheKey = getCacheKey(configGHRepository, fullPath);

Object cachedObject = cache.get(cacheKey);
if (cachedObject != null) {
return cachedObject;
}

return cache.computeIfAbsent(cacheKey, k -> readConfigFile(ghRepository, fullPath, type));
return cache.computeIfAbsent(cacheKey, k -> readConfigFile(configGHRepository, fullPath, type));
}

private Object readConfigFile(GHRepository ghRepository, String fullPath, Class<?> type) {
Expand Down Expand Up @@ -79,6 +88,37 @@ private Object readConfigFile(GHRepository ghRepository, String fullPath, Class<
}
}

private static GHRepository getConfigRepository(GHRepository ghRepository,
ConfigFile.Source source,
boolean readConfigFilesFromSourceRepository,
String path) {
ConfigFile.Source effectiveSource = (source == ConfigFile.Source.DEFAULT)
? (readConfigFilesFromSourceRepository ? ConfigFile.Source.SOURCE_REPOSITORY
: ConfigFile.Source.CURRENT_REPOSITORY)
: source;

if (effectiveSource == ConfigFile.Source.CURRENT_REPOSITORY) {
return ghRepository;
}
if (!ghRepository.isFork()) {
return ghRepository;
}

try {
GHRepository sourceRepository = ghRepository.getSource();

if (sourceRepository == null) {
throw new IllegalStateException("Unable to get the source repository for fork " + ghRepository.getFullName()
+ ": unable to read config file " + path);
}

return sourceRepository;
} catch (IOException e) {
throw new IllegalStateException("Unable to get the source repository for fork " + ghRepository.getFullName()
+ ": unable to read config file " + path, e);
}
}

private static String getCacheKey(GHRepository ghRepository, String fullPath) {
// 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ public class GitHubAppRuntimeConfig {
@ConfigItem
public Optional<String> appName;

/**
* Read the configuration files from the source repository in case of a fork.
*/
@ConfigItem(defaultValue = "false")
public boolean readConfigFilesFromSourceRepository;

/**
* The RSA private key.
*/
Expand Down