Skip to content

Commit

Permalink
Merge pull request #29 from The-Huginn/fetch-config-file
Browse files Browse the repository at this point in the history
Fetch&Deserialize Config File from a public GitHub repo
  • Loading branch information
xstefank authored Dec 14, 2023
2 parents 79ccfda + 5e95e44 commit c6e5d55
Show file tree
Hide file tree
Showing 9 changed files with 175 additions and 6 deletions.
8 changes: 6 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@
<artifactId>quarkus-junit5</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-yaml</artifactId>
<version>2.16.0</version>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down Expand Up @@ -157,13 +162,12 @@
</executions>
<dependencies>
<dependency>
<artifactId>quarkus-ide-config</artifactId>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-ide-config</artifactId>
<version>${quarkus.platform.version}</version>
</dependency>
</dependencies>
<configuration>
<!-- store outside of target to speed up formatting when mvn clean is used -->
<cachedir>.cache/formatter-maven-plugin-${formatter-maven-plugin.version}</cachedir>
<configFile>eclipse-format.xml</configFile>
<lineEnding>LF</lineEnding>
Expand Down
19 changes: 18 additions & 1 deletion src/main/java/org/jboss/JiraIssueLotteryCommand.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.jboss;

import com.fasterxml.jackson.databind.ObjectMapper;
import io.quarkus.logging.Log;
import jakarta.annotation.PostConstruct;
import jakarta.inject.Inject;
Expand All @@ -9,11 +10,16 @@
import org.apache.camel.component.jira.JiraConfiguration;
import org.apache.camel.component.jira.JiraEndpoint;
import org.jboss.config.JiraLotteryAppConfig;
import org.jboss.processing.NewIssueCollector;
import org.jboss.config.LotteryConfig;
import org.jboss.processing.IssueProcessor;
import org.jboss.processing.NewIssueCollector;
import picocli.CommandLine.Command;
import picocli.CommandLine.Parameters;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

@Command(name = "jira-issue-lottery", mixinStandardHelpOptions = true)
public class JiraIssueLotteryCommand implements Runnable {

Expand Down Expand Up @@ -49,10 +55,21 @@ private JiraConfiguration setupJiraConfiguration() {
return jiraConfiguration;
}

@Inject
ObjectMapper objectMapper;

@Override
public void run() {
NewIssueCollector newIssueCollector = NewIssueCollector.getInstance(jiraEndpoint);
Exchange exchange = jiraEndpoint.createExchange();
try {
LotteryConfig lotteryConfig = objectMapper.readValue(
new URI(jiraLotteryAppConfig.configFileRepo().getRawContentsUrl()).toURL(),
LotteryConfig.class);
Log.info(lotteryConfig);
} catch (IOException | URISyntaxException e) {
throw new RuntimeException(e);
}
try {
newIssueCollector.execute().getIssueStates().forEach(Log::info);
new IssueProcessor(jiraEndpoint, "JBEAP-25900").process(exchange);
Expand Down
50 changes: 50 additions & 0 deletions src/main/java/org/jboss/config/GitHubRawUrl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package org.jboss.config;

import org.eclipse.microprofile.config.spi.Converter;

import java.net.URI;
import java.net.URISyntaxException;

/**
* Class containing only String but due to some preprocessing of this
* property we need to have it encapsulated. We expect the repository
* to be publicly available.
*/
public class GitHubRawUrl {

private final String rawContentsUrl;

private GitHubRawUrl(String rawContentsUrl) {
this.rawContentsUrl = rawContentsUrl;
}

public String getRawContentsUrl() {
return rawContentsUrl;
}

@Override
public String toString() {
return rawContentsUrl;
}

public static class GitHubRepoToRawUrlConverter implements Converter<GitHubRawUrl> {

private static final String RAW_CONTENTS = "https://raw.githubusercontent.com";
private static final String RELATIVE_PATH = "main/.github/" + LotteryConfig.FILE_NAME;

public GitHubRepoToRawUrlConverter() {
}

@Override
public GitHubRawUrl convert(String value) {
try {
String repo = new URI(value).getPath();
String rawContentsURI = RAW_CONTENTS + repo + (repo.endsWith("/") ? "" : "/") + RELATIVE_PATH;
return new GitHubRawUrl(rawContentsURI);

} catch (URISyntaxException e) {
throw new RuntimeException("Provided GitHub repo url is not valid");
}
}
}
}
7 changes: 7 additions & 0 deletions src/main/java/org/jboss/config/JiraLotteryAppConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,11 @@ public interface JiraLotteryAppConfig {

@WithDefault("50")
Integer maxResults();

/**
* URL of the public GitHub repository, must be public.
* The config file is then expected under .github/ directory
* called {@link LotteryConfig#FILE_NAME}
*/
GitHubRawUrl configFileRepo();
}
3 changes: 1 addition & 2 deletions src/main/java/org/jboss/config/LotteryConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

import java.time.DayOfWeek;
import java.time.Duration;
import java.util.HashMap;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
Expand All @@ -31,7 +30,7 @@ public record Participant(

public record Project(
@JsonProperty(required = true) String project,
@JsonDeserialize(as = HashMap.class) Set<String> components,
@JsonDeserialize(as = TreeSet.class) Set<String> components,
@JsonUnwrapped @JsonProperty(access = JsonProperty.Access.READ_ONLY) Participation participation) {
// https://stackoverflow.com/a/71539100/6692043
@JsonCreator
Expand Down
33 changes: 33 additions & 0 deletions src/main/java/org/jboss/config/YamlObjectMapper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package org.jboss.config;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.module.paramnames.ParameterNamesModule;
import io.quarkus.arc.All;
import io.quarkus.jackson.ObjectMapperCustomizer;
import jakarta.inject.Singleton;
import jakarta.ws.rs.Produces;

import java.util.List;

public class YamlObjectMapper {
@Singleton
@Produces
ObjectMapper objectMapper(@All List<ObjectMapperCustomizer> customizers) {
ObjectMapper objectMapper = new ObjectMapper(new YAMLFactory());
objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
objectMapper.registerModule(new JavaTimeModule());
objectMapper.registerModule(new Jdk8Module());
objectMapper.registerModule(new ParameterNamesModule());

// Apply all ObjectMapperCustomizer beans (incl. Quarkus)
for (ObjectMapperCustomizer customizer : customizers) {
customizer.customize(objectMapper);
}

return objectMapper;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.jboss.config.GitHubRawUrl$GitHubRepoToRawUrlConverter
3 changes: 2 additions & 1 deletion src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
camel.component.jira.jira-url=https://issues.redhat.com/
%test.jira-issue-lottery.access-token=ignored
%test.jira-issue-lottery.access-token=ignored
jira-issue-lottery.config-file-repo=https://github.com/The-Huginn/jira-issue-lottery
57 changes: 57 additions & 0 deletions src/test/java/org/jboss/set/state/ConfigDeserializationTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package org.jboss.set.state;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import io.quarkus.test.junit.QuarkusTest;
import jakarta.inject.Inject;
import org.jboss.config.GitHubRawUrl;
import org.jboss.config.LotteryConfig;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.io.IOException;

@QuarkusTest
public class ConfigDeserializationTest {

@Inject
ObjectMapper objectMapper;

@Test
void testDeserializingSimpleConfigFile() {
String configFile = """
delay: P14D
participants:
- username: The-Huginn
days: [MONDAY]
projects:
- project: WFLY
components: [Logging]
maxIssues: 5""";

Assertions.assertTrue(objectMapper.getFactory() instanceof YAMLFactory);
try {
objectMapper.readValue(configFile, LotteryConfig.class);
} catch (IOException e) {
Assertions.fail("Expecting no problem deserializing the config file %s");
}
}

@Test
void testConvertingGitHubRepoUrlWithoutTrailingSlash() {
String repoUrl = "https://github.com/The-Huginn/jira-issue-lottery";
GitHubRawUrl gitHubRawUrl = new GitHubRawUrl.GitHubRepoToRawUrlConverter().convert(repoUrl);
Assertions.assertEquals(
"https://raw.githubusercontent.com/The-Huginn/jira-issue-lottery/main/.github/jira-issue-lottery.yml",
gitHubRawUrl.getRawContentsUrl());
}

@Test
void testConvertingGitHubRepoUrlWithTrailingSlash() {
String repoUrl = "https://github.com/The-Huginn/jira-issue-lottery/";
GitHubRawUrl gitHubRawUrl = new GitHubRawUrl.GitHubRepoToRawUrlConverter().convert(repoUrl);
Assertions.assertEquals(
"https://raw.githubusercontent.com/The-Huginn/jira-issue-lottery/main/.github/jira-issue-lottery.yml",
gitHubRawUrl.getRawContentsUrl());
}
}

0 comments on commit c6e5d55

Please sign in to comment.