From 555100805410fd4479355698ab1b8209c0e751b6 Mon Sep 17 00:00:00 2001 From: lukasburda Date: Tue, 11 Dec 2018 18:01:42 +0100 Subject: [PATCH] [Issue #3] Add issue / feature distinction in checks application --- .../java/org/xstefank/check/SkipCheck.java | 12 ++++---- .../org/xstefank/check/TemplateChecker.java | 25 ++++++++++++++--- .../org/xstefank/model/yaml/FormatConfig.java | 28 +++++++++++++++---- .../FormatElementVerification.java | 2 +- src/main/resources/format.yaml | 2 +- src/test/java/org/xstefank/TestUtils.java | 2 +- .../xstefank/check/RequiredRowsCheckTest.java | 2 +- .../java/org/xstefank/model/ConfigTest.java | 2 +- .../verification/VerificationsTest.java | 4 +-- src/test/resources/json/testPayload.json | 2 +- src/test/resources/yaml/testTemplate.yaml | 2 +- 11 files changed, 59 insertions(+), 24 deletions(-) diff --git a/src/main/java/org/xstefank/check/SkipCheck.java b/src/main/java/org/xstefank/check/SkipCheck.java index e845bfd9..980a9e41 100644 --- a/src/main/java/org/xstefank/check/SkipCheck.java +++ b/src/main/java/org/xstefank/check/SkipCheck.java @@ -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; diff --git a/src/main/java/org/xstefank/check/TemplateChecker.java b/src/main/java/org/xstefank/check/TemplateChecker.java index 518dfcb0..241593a3 100644 --- a/src/main/java/org/xstefank/check/TemplateChecker.java +++ b/src/main/java/org/xstefank/check/TemplateChecker.java @@ -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; @@ -12,9 +13,13 @@ 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 checks; + private List bugPRChecks; + private List featurePRChecks; private FormatConfig config; public TemplateChecker(FormatConfig config) { @@ -22,11 +27,22 @@ public TemplateChecker(FormatConfig config) { 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 checks) { String description = ""; for (Check check : checks) { String message = check.check(payload); @@ -35,13 +51,14 @@ public String checkPR(JsonNode payload) { break; } } - return description; } - private static List registerChecks(Format format) { + private List registerChecks(Format format) { List checks = new ArrayList<>(); + format = config.getDefaultFormat(); + if (format.getTitle() != null) { checks.add(new TitleCheck(format.getTitle())); } diff --git a/src/main/java/org/xstefank/model/yaml/FormatConfig.java b/src/main/java/org/xstefank/model/yaml/FormatConfig.java index 74918c89..db679ee8 100644 --- a/src/main/java/org/xstefank/model/yaml/FormatConfig.java +++ b/src/main/java/org/xstefank/model/yaml/FormatConfig.java @@ -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; @@ -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; } } diff --git a/src/main/java/org/xstefank/verification/FormatElementVerification.java b/src/main/java/org/xstefank/verification/FormatElementVerification.java index 4750e5d2..ae525437 100644 --- a/src/main/java/org/xstefank/verification/FormatElementVerification.java +++ b/src/main/java/org/xstefank/verification/FormatElementVerification.java @@ -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"); } } diff --git a/src/main/resources/format.yaml b/src/main/resources/format.yaml index 204d1e3f..786ab533 100644 --- a/src/main/resources/format.yaml +++ b/src/main/resources/format.yaml @@ -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?)" diff --git a/src/test/java/org/xstefank/TestUtils.java b/src/test/java/org/xstefank/TestUtils.java index eaebe6e0..5e41dc60 100644 --- a/src/test/java/org/xstefank/TestUtils.java +++ b/src/test/java/org/xstefank/TestUtils.java @@ -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) { diff --git a/src/test/java/org/xstefank/check/RequiredRowsCheckTest.java b/src/test/java/org/xstefank/check/RequiredRowsCheckTest.java index 25ed48ae..b604d001 100644 --- a/src/test/java/org/xstefank/check/RequiredRowsCheckTest.java +++ b/src/test/java/org/xstefank/check/RequiredRowsCheckTest.java @@ -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"); } diff --git a/src/test/java/org/xstefank/model/ConfigTest.java b/src/test/java/org/xstefank/model/ConfigTest.java index 65d6e31a..803c965b 100644 --- a/src/test/java/org/xstefank/model/ConfigTest.java +++ b/src/test/java/org/xstefank/model/ConfigTest.java @@ -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()); } diff --git a/src/test/java/org/xstefank/verification/VerificationsTest.java b/src/test/java/org/xstefank/verification/VerificationsTest.java index 3f436f9e..ef0f78f3 100644 --- a/src/test/java/org/xstefank/verification/VerificationsTest.java +++ b/src/test/java/org/xstefank/verification/VerificationsTest.java @@ -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 { @@ -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) diff --git a/src/test/resources/json/testPayload.json b/src/test/resources/json/testPayload.json index f03e68b7..4efe51ab 100644 --- a/src/test/resources/json/testPayload.json +++ b/src/test/resources/json/testPayload.json @@ -1,7 +1,7 @@ { "pull_request": { "title": "Test PR", - "body": "Test description", + "body": "[BUG] Test description", "commits": "1" } } diff --git a/src/test/resources/yaml/testTemplate.yaml b/src/test/resources/yaml/testTemplate.yaml index 15c3b6c4..159bd846 100644 --- a/src/test/resources/yaml/testTemplate.yaml +++ b/src/test/resources/yaml/testTemplate.yaml @@ -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?)"