Skip to content

Commit

Permalink
[Issue jboss#3] Add issue / feature distinction in checks application
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasburda committed Dec 31, 2018
1 parent 86cb3b4 commit 5551008
Show file tree
Hide file tree
Showing 11 changed files with 59 additions and 24 deletions.
12 changes: 6 additions & 6 deletions src/main/java/org/xstefank/check/SkipCheck.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,26 @@ public static boolean shouldSkip(JsonNode payload, FormatConfig config) {
}

private static boolean skipByTitle(JsonNode payload, FormatConfig config) {
if (config.getFormat().getSkipPatterns().getTitle() != null) {
Matcher titleMatcher = config.getFormat().getSkipPatterns().getTitle().matcher(payload.get(Utils.PULL_REQUEST).get(Utils.TITLE).asText());
if (config.getDefaultFormat().getSkipPatterns().getTitle() != null) {
Matcher titleMatcher = config.getDefaultFormat().getSkipPatterns().getTitle().matcher(payload.get(Utils.PULL_REQUEST).get(Utils.TITLE).asText());
return titleMatcher.matches();
}
return false;
}

private static boolean skipByCommit(JsonNode payload, FormatConfig config) {
if (config.getFormat().getSkipPatterns().getCommit() != null) {
LatestCommitCheck latestCommitCheck = new LatestCommitCheck(config.getFormat().getSkipPatterns().getCommit());
if (config.getDefaultFormat().getSkipPatterns().getCommit() != null) {
LatestCommitCheck latestCommitCheck = new LatestCommitCheck(config.getDefaultFormat().getSkipPatterns().getCommit());
return (latestCommitCheck.check(payload) == null);
}
return false;
}

private static boolean skipByDescriptionFirstRow(JsonNode payload, FormatConfig config) {
if (config.getFormat().getSkipPatterns().getDescription() != null) {
if (config.getDefaultFormat().getSkipPatterns().getDescription() != null) {
String description = payload.get(Utils.PULL_REQUEST).get(Utils.BODY).asText();
String firstRow = description.split(System.lineSeparator(), 2)[0];
Matcher descriptionMatcher = config.getFormat().getSkipPatterns().getDescription().matcher(firstRow);
Matcher descriptionMatcher = config.getDefaultFormat().getSkipPatterns().getDescription().matcher(firstRow);
return descriptionMatcher.matches();
}
return false;
Expand Down
25 changes: 21 additions & 4 deletions src/main/java/org/xstefank/check/TemplateChecker.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.fasterxml.jackson.databind.JsonNode;
import org.jboss.logging.Logger;
import org.xstefank.check.additional.AdditionalChecks;
import org.xstefank.model.Utils;
import org.xstefank.model.yaml.FormatConfig;
import org.xstefank.model.yaml.Format;

Expand All @@ -12,21 +13,36 @@
public class TemplateChecker {

public static final String TEMPLATE_FORMAT_FILE = "template.format.file";
private static final String IDENTIFY_ERROR_MESSAGE = "Pull request is not identified with [BUG] or [FEATURE]";
private static final String BUG = "[BUG]";
private static final String FEATURE = "[FEATURE]";
private static final Logger log = Logger.getLogger(TemplateChecker.class);

private List<Check> checks;
private List<Check> bugPRChecks;
private List<Check> featurePRChecks;
private FormatConfig config;

public TemplateChecker(FormatConfig config) {
if (config == null) {
throw new IllegalArgumentException("Argument config cannot be null");
}
this.config = config;
checks = registerChecks(config.getFormat());
bugPRChecks = registerChecks(config.getBugFormat());
featurePRChecks = registerChecks(config.getFeatureFormat());
}

public String checkPR(JsonNode payload) {
log.info("checking PR");
String prBody = payload.get(Utils.PULL_REQUEST).get(Utils.BODY).asText();
if (prBody.contains(BUG)) {
return runChecks(payload, bugPRChecks);
} else if (prBody.contains(FEATURE)) {
return runChecks(payload, featurePRChecks);
}
return IDENTIFY_ERROR_MESSAGE;
}

private String runChecks(JsonNode payload, List<Check> checks) {
String description = "";
for (Check check : checks) {
String message = check.check(payload);
Expand All @@ -35,13 +51,14 @@ public String checkPR(JsonNode payload) {
break;
}
}

return description;
}

private static List<Check> registerChecks(Format format) {
private List<Check> registerChecks(Format format) {
List<Check> checks = new ArrayList<>();

format = config.getDefaultFormat();

if (format.getTitle() != null) {
checks.add(new TitleCheck(format.getTitle()));
}
Expand Down
28 changes: 23 additions & 5 deletions src/main/java/org/xstefank/model/yaml/FormatConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ public class FormatConfig {

private String repository;
private String statusUrl;
private Format format;
private Format defaultFormat;
private Format bugFormat;
private Format featureFormat;

public String getRepository() {
return repository;
Expand All @@ -22,11 +24,27 @@ public void setStatusUrl(String statusUrl) {
this.statusUrl = statusUrl;
}

public Format getFormat() {
return format;
public Format getDefaultFormat() {
return defaultFormat;
}

public void setFormat(Format format) {
this.format = format;
public void setDefaultFormat(Format defaultFormat) {
this.defaultFormat = defaultFormat;
}

public Format getBugFormat() {
return bugFormat;
}

public void setBugFormat(Format bugFormat) {
this.bugFormat = bugFormat;
}

public Format getFeatureFormat() {
return featureFormat;
}

public void setFeatureFormat(Format featureFormat) {
this.featureFormat = featureFormat;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public class FormatElementVerification implements Verification {

@Override
public void verify(FormatConfig formatConfig) throws InvalidConfigurationException {
if (formatConfig.getFormat() == null)
if (formatConfig.getDefaultFormat() == null)
throw new InvalidConfigurationException("Element 'format' is not specified");
}
}
2 changes: 1 addition & 1 deletion src/main/resources/format.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
repository: "xstefank/test-repo"
statusUrl: "https://github.com/xstefank/test-repo/blob/master/.github/pull_request_template.md"

format:
defaultFormat:
skipPatterns:
title: "((.*)?NO JIRA REQUIRED(.*)\\s?)|(([^do not|don't]\\s)?skip.*template.*check(.*)\\s?)|(([^do not|don't]\\s)?bypass.*template.*check(.*)\\s?)"
commit: "((.*)?NO JIRA REQUIRED(.*)\\s?)|(([^do not|don't]\\s)?skip.*template.*check(.*)\\s?)|(([^do not|don't]\\s)?bypass.*template.*check(.*)\\s?)"
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/org/xstefank/TestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class TestUtils {
public static final JsonNode TEST_PAYLOAD = loadJson(JSON_DIR + "/testPayload.json");
public static final JsonNode BAD_TEST_PAYLOAD = loadJson(JSON_DIR + "/badTestPayload.json");
public static final JsonNode EMPTY_PAYLOAD = createEmptyJsonPayload();
public static final FormatConfig FOMAT_CONFIG = loadFormatFromYamlFile(YAML_DIR + "/testTemplate.yaml");
public static final FormatConfig FORMAT_CONFIG = loadFormatFromYamlFile(YAML_DIR + "/testTemplate.yaml");
public static final String TEST_CONFIG_PATH = ConfigTest.class.getClassLoader().getResource("testConfig.properties").getPath();

public static FormatConfig loadFormatFromYamlFile(String fileName) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class RequiredRowsCheckTest {
@BeforeClass
public static void beforeClass() {
row = new Row();
row.setPattern(Pattern.compile("^Test.*description$"));
row.setPattern(Pattern.compile(".*Test.*description"));
row.setMessage("Does not match");
}

Expand Down
2 changes: 1 addition & 1 deletion src/test/java/org/xstefank/model/ConfigTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class ConfigTest {

@Test
public void testValidTemplateConfig() {
TemplateChecker templateChecker = new TemplateChecker(TestUtils.FOMAT_CONFIG);
TemplateChecker templateChecker = new TemplateChecker(TestUtils.FORMAT_CONFIG);
Assert.assertTrue(templateChecker.checkPR(TestUtils.TEST_PAYLOAD).isEmpty());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import static org.xstefank.TestUtils.loadFormatFromYamlFile;
import static org.xstefank.TestUtils.YAML_DIR;
import static org.xstefank.TestUtils.FOMAT_CONFIG;
import static org.xstefank.TestUtils.FORMAT_CONFIG;

public class VerificationsTest {

Expand All @@ -19,7 +19,7 @@ public static void beforeClass() {

@Test
public void testReadValidFormatConfiguration() throws InvalidConfigurationException {
VerificationHandler.verifyConfiguration(FOMAT_CONFIG);
VerificationHandler.verifyConfiguration(FORMAT_CONFIG);
}

@Test(expected = InvalidConfigurationException.class)
Expand Down
2 changes: 1 addition & 1 deletion src/test/resources/json/testPayload.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"pull_request": {
"title": "Test PR",
"body": "Test description",
"body": "[BUG] Test description",
"commits": "1"
}
}
2 changes: 1 addition & 1 deletion src/test/resources/yaml/testTemplate.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
repository: "testuser/testrepo"
statusUrl: "https://github.com/testuser/test-repo/blob/master/.github/pull_request_template.md"

format:
defaultFormat:
skipPatterns:
title: "((.*)?NO JIRA REQUIRED(.*)\\s?)|(([^do not|don't]\\s)?skip.*template.*check(.*)\\s?)|(([^do not|don't]\\s)?bypass.*template.*check(.*)\\s?)"
description: "((.*)?NO JIRA REQUIRED(.*)\\s?)|(([^do not|don't]\\s)?skip.*template.*check(.*)\\s?)|(([^do not|don't]\\s)?bypass.*template.*check(.*)\\s?)"
Expand Down

0 comments on commit 5551008

Please sign in to comment.