Skip to content

Commit

Permalink
Merge pull request jboss#106 from xstefank/mp-config
Browse files Browse the repository at this point in the history
Add TyrConfiguration and enable MP config in WebHookEndpoint
  • Loading branch information
xstefank authored Mar 26, 2020
2 parents 27d30e0 + 9909a74 commit e37f33d
Show file tree
Hide file tree
Showing 15 changed files with 101 additions and 78 deletions.
10 changes: 5 additions & 5 deletions tyr-runner/src/main/java/org/jboss/tyr/check/SkipCheck.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import org.jboss.tyr.InvalidPayloadException;
import org.jboss.tyr.model.Utils;
import org.jboss.tyr.model.yaml.FormatConfig;
import org.jboss.tyr.model.yaml.FormatYaml;
import org.jboss.tyr.model.yaml.RegexDefinition;

import javax.json.JsonObject;
Expand All @@ -26,14 +26,14 @@

public class SkipCheck {

public static boolean shouldSkip(JsonObject payload, FormatConfig config) throws InvalidPayloadException {
public static boolean shouldSkip(JsonObject payload, FormatYaml config) throws InvalidPayloadException {
if (payload == null || config == null) {
throw new IllegalArgumentException("Input arguments cannot be null");
}
return skipByTitle(payload, config) || skipByCommit(payload, config) || skipByDescriptionFirstRow(payload, config);
}

private static boolean skipByTitle(JsonObject payload, FormatConfig config) {
private static boolean skipByTitle(JsonObject payload, FormatYaml config) {
Pattern titlePattern = config.getFormat().getSkipPatterns().getTitle();
if (titlePattern != null) {
Matcher titleMatcher = titlePattern.matcher(payload.getJsonObject(Utils.PULL_REQUEST).getString(Utils.TITLE));
Expand All @@ -43,7 +43,7 @@ private static boolean skipByTitle(JsonObject payload, FormatConfig config) {
}


private static boolean skipByCommit(JsonObject payload, FormatConfig config) throws InvalidPayloadException {
private static boolean skipByCommit(JsonObject payload, FormatYaml config) throws InvalidPayloadException {
Pattern commitPattern = config.getFormat().getSkipPatterns().getCommit();
if (commitPattern != null) {
RegexDefinition commitRegexDefinition = new RegexDefinition();
Expand All @@ -54,7 +54,7 @@ private static boolean skipByCommit(JsonObject payload, FormatConfig config) thr
return false;
}

private static boolean skipByDescriptionFirstRow(JsonObject payload, FormatConfig config) {
private static boolean skipByDescriptionFirstRow(JsonObject payload, FormatYaml config) {
Pattern descriptionPattern = config.getFormat().getSkipPatterns().getDescription();
if (descriptionPattern != null) {
String description = payload.getJsonObject(Utils.PULL_REQUEST).getString(Utils.BODY);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import org.jboss.tyr.model.AdditionalResourcesLoader;
import org.jboss.tyr.model.Utils;
import org.jboss.tyr.model.yaml.Format;
import org.jboss.tyr.model.yaml.FormatConfig;
import org.jboss.tyr.model.yaml.FormatYaml;

import javax.json.JsonObject;
import java.util.ArrayList;
Expand All @@ -33,7 +33,7 @@ public class TemplateChecker {

private List<Check> checks;

public TemplateChecker(FormatConfig config) {
public TemplateChecker(FormatYaml config) {
if (config == null || config.getFormat() == null) {
throw new IllegalArgumentException("Input argument cannot be null");
}
Expand Down
17 changes: 17 additions & 0 deletions tyr-runner/src/main/java/org/jboss/tyr/model/TyrConfiguration.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.jboss.tyr.model;

import io.quarkus.arc.config.ConfigProperties;
import org.eclipse.microprofile.config.inject.ConfigProperty;

import java.util.Optional;

@ConfigProperties(prefix = "tyr")
public interface TyrConfiguration {

@ConfigProperty(name = "template.format.url")
Optional<String> formatFileUrl();

@ConfigProperty(name = "template.format.file")
Optional<String> configFileName();

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
package org.jboss.tyr.model.yaml;

public class FormatConfig {
public class FormatYaml {

private String repository;
private String statusUrl;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
*/
package org.jboss.tyr.verification;

import org.jboss.tyr.model.yaml.FormatConfig;
import org.jboss.tyr.model.yaml.FormatYaml;

public class FormatElementVerification implements Verification {

@Override
public void verify(FormatConfig formatConfig) throws InvalidConfigurationException {
if (formatConfig.getFormat() == null)
public void verify(FormatYaml formatYaml) throws InvalidConfigurationException {
if (formatYaml.getFormat() == null)
throw new InvalidConfigurationException("Element 'format' is not specified");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
*/
package org.jboss.tyr.verification;

import org.jboss.tyr.model.yaml.FormatConfig;
import org.jboss.tyr.model.yaml.FormatYaml;

public class RepositoryFormatVerification implements Verification {

@Override
public void verify(FormatConfig formatConfig) throws InvalidConfigurationException {
if (!formatConfig.getRepository().matches("^[a-zA-Z0-9_]*/[a-zA-Z0-9_-]*$"))
public void verify(FormatYaml formatYaml) throws InvalidConfigurationException {
if (!formatYaml.getRepository().matches("^[a-zA-Z0-9_]*/[a-zA-Z0-9_-]*$"))
throw new InvalidConfigurationException("Wrong repository format in configuration file");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
package org.jboss.tyr.verification;

import org.jboss.tyr.model.yaml.FormatConfig;
import org.jboss.tyr.model.yaml.FormatYaml;

/**
* Verifies structure of the format.yaml configuration file
Expand All @@ -25,8 +25,8 @@ public interface Verification {
/**
* Verify the provided format.yaml structure
*
* @param formatConfig object representing format.yaml file
* @param formatYaml object representing format.yaml file
* @throws InvalidConfigurationException if any validation error is encountered
*/
void verify(FormatConfig formatConfig) throws InvalidConfigurationException;
void verify(FormatYaml formatYaml) throws InvalidConfigurationException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
package org.jboss.tyr.verification;

import org.jboss.tyr.model.yaml.FormatConfig;
import org.jboss.tyr.model.yaml.FormatYaml;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -24,9 +24,9 @@ public class VerificationHandler {

private static final List<Verification> verifications = registerVerifications();

public static void verifyConfiguration(FormatConfig formatConfig) throws InvalidConfigurationException {
public static void verifyConfiguration(FormatYaml formatYaml) throws InvalidConfigurationException {
for (Verification verification : verifications) {
verification.verify(formatConfig);
verification.verify(formatYaml);
}
}

Expand Down
52 changes: 29 additions & 23 deletions tyr-runner/src/main/java/org/jboss/tyr/webhook/WebHookEndpoint.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,16 @@
import org.jboss.tyr.check.SkipCheck;
import org.jboss.tyr.check.TemplateChecker;
import org.jboss.tyr.model.CommitStatus;
import org.jboss.tyr.model.TyrConfiguration;
import org.jboss.tyr.model.Utils;
import org.jboss.tyr.model.yaml.FormatConfig;
import org.jboss.tyr.model.yaml.FormatYaml;
import org.jboss.tyr.verification.InvalidConfigurationException;
import org.jboss.tyr.verification.VerificationHandler;
import org.jboss.tyr.whitelist.WhitelistProcessing;

import javax.annotation.PostConstruct;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import javax.json.JsonObject;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
Expand All @@ -37,16 +41,23 @@
import java.io.IOException;
import java.net.URL;

import static org.jboss.tyr.model.Utils.TEMPLATE_FORMAT_FILE;

@Path("/")
@ApplicationScoped
public class WebHookEndpoint {

private static final FormatConfig config = readConfig();
private static final WhitelistProcessing whitelistProcessing =
WhitelistProcessing.IS_WHITELISTING_ENABLED ? new WhitelistProcessing(config) : null;
@Inject
TyrConfiguration configuration;

private FormatYaml format;
private WhitelistProcessing whitelistProcessing;
private TemplateChecker templateChecker;

private TemplateChecker templateChecker = new TemplateChecker(config);
@PostConstruct
public void init() {
format = readConfig();
whitelistProcessing = WhitelistProcessing.IS_WHITELISTING_ENABLED ? new WhitelistProcessing(format) : null;
templateChecker = new TemplateChecker(format);
}

@POST
@Path("/pull-request")
Expand All @@ -59,36 +70,31 @@ public void processRequest(JsonObject payload) throws InvalidPayloadException {
}
}

private static FormatConfig readConfig() {
private FormatYaml readConfig() {
ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
FormatConfig formatConfig;
FormatYaml formatYaml;
try {
URL formatFileUrl = Utils.getFormatUrl();
if (formatFileUrl != null)
formatConfig = mapper.readValue(formatFileUrl.openStream(), FormatConfig.class);
if (configuration.formatFileUrl().isPresent())
formatYaml = mapper.readValue(new URL(configuration.formatFileUrl().get()).openStream(), FormatYaml.class);
else {
String configFileName = System.getProperty(TEMPLATE_FORMAT_FILE);
if (configFileName == null) {
configFileName = Utils.getConfigDirectory() + "/format.yaml";
}
File configFile = new File(configFileName);
formatConfig = mapper.readValue(configFile, FormatConfig.class);
File configFile = new File(configuration.configFileName().orElse(Utils.getConfigDirectory() + "/format.yaml"));
formatYaml = mapper.readValue(configFile, FormatYaml.class);
}
VerificationHandler.verifyConfiguration(formatConfig);
return formatConfig;
VerificationHandler.verifyConfiguration(formatYaml);
return formatYaml;
} catch (IOException | InvalidConfigurationException e) {
throw new IllegalArgumentException("Cannot load configuration file", e);
}
}

private void processPullRequest(JsonObject prPayload) throws InvalidPayloadException {
if (!SkipCheck.shouldSkip(prPayload, config)) {
if (!SkipCheck.shouldSkip(prPayload, format)) {
String errorMessage = templateChecker.checkPR(prPayload);
if (errorMessage != null) {
GitHubAPI.updateCommitStatus(config.getRepository(),
GitHubAPI.updateCommitStatus(format.getRepository(),
prPayload.getJsonObject(Utils.PULL_REQUEST).getJsonObject(Utils.HEAD).getString(Utils.SHA),
errorMessage.isEmpty() ? CommitStatus.SUCCESS : CommitStatus.ERROR,
config.getStatusUrl(),
format.getStatusUrl(),
errorMessage.isEmpty() ? "valid" : errorMessage, "PR format");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import org.jboss.tyr.model.PersistentList;
import org.jboss.tyr.model.TyrProperties;
import org.jboss.tyr.model.Utils;
import org.jboss.tyr.model.yaml.FormatConfig;
import org.jboss.tyr.model.yaml.FormatYaml;

import javax.json.JsonObject;
import java.util.ArrayList;
Expand All @@ -45,7 +45,7 @@ public class WhitelistProcessing implements CIOperations {
private final List<Command> commands;
private final List<ContinuousIntegration> continuousIntegrations;

public WhitelistProcessing(FormatConfig config) {
public WhitelistProcessing(FormatYaml config) {
String dirName = Utils.getConfigDirectory();
userList = new PersistentList(dirName, Utils.USERLIST_FILE_NAME);
adminList = new PersistentList(dirName, Utils.ADMINLIST_FILE_NAME);
Expand Down Expand Up @@ -120,7 +120,7 @@ public boolean addUserToUserList(String username) {
return userList.add(username);
}

private List<Command> getCommands(FormatConfig config) {
private List<Command> getCommands(FormatYaml config) {
List<Command> commands = new ArrayList<>();

Map<String, String> regexMap = config.getFormat().getCommands();
Expand All @@ -141,7 +141,7 @@ private List<Command> getCommands(FormatConfig config) {
return commands;
}

private List<ContinuousIntegration> loadCIs(FormatConfig config) {
private List<ContinuousIntegration> loadCIs(FormatYaml config) {
List<ContinuousIntegration> continuousIntegrations = new ArrayList<>();
List<String> CIConfigList = config.getFormat().getCI();

Expand Down
Empty file.
8 changes: 4 additions & 4 deletions tyr-runner/src/test/java/org/jboss/tyr/TestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import org.jboss.tyr.model.yaml.FormatConfig;
import org.jboss.tyr.model.yaml.FormatYaml;

import javax.json.Json;
import javax.json.JsonArray;
Expand Down Expand Up @@ -50,13 +50,13 @@ public class TestUtils {

public static final String READ_TOKEN = "readToken";
public static final String GET_JSON_WITH_COMMITS = "getCommitsJSON";
public static final FormatConfig FORMAT_CONFIG = loadFormatFromYamlFile(YAML_DIR + "/testTemplate.yaml");
public static final FormatYaml FORMAT_CONFIG = loadFormatFromYamlFile(YAML_DIR + "/testTemplate.yaml");
public static final Path TEST_CONFIG_PATH = getFilePath("testConfig.properties");

public static FormatConfig loadFormatFromYamlFile(String fileName) {
public static FormatYaml loadFormatFromYamlFile(String fileName) {
try {
File file = getFile(fileName);
return new ObjectMapper(new YAMLFactory()).readValue(file, FormatConfig.class);
return new ObjectMapper(new YAMLFactory()).readValue(file, FormatYaml.class);
} catch (IOException e) {
throw new RuntimeException("Cannot load file " + fileName);
}
Expand Down
Loading

0 comments on commit e37f33d

Please sign in to comment.