From c7fc89adf644f11225d5dc7554184512d8eefcbf Mon Sep 17 00:00:00 2001 From: David Mas Date: Wed, 15 Jan 2025 13:13:44 +0100 Subject: [PATCH 1/3] Allow to configure the plugin to fail on any severity of detected issues. This is necessary to ease the integration to cicd with requirements to not pass if any static code violation is found, without needing to configure the checkstyle, pmd and findbugs in depth to produce only error level, which is hard to do and can be difficult to maintain regarding future versions. Signed-off-by: David Mas --- docs/maven-plugin.md | 14 +++--- .../tools/analysis/report/ReportMojo.java | 45 ++++++++++++++++--- .../tools/analysis/report/ReportMojoTest.java | 38 +++++++++------- 3 files changed, 69 insertions(+), 28 deletions(-) diff --git a/docs/maven-plugin.md b/docs/maven-plugin.md index 24000412..f4d718c1 100644 --- a/docs/maven-plugin.md +++ b/docs/maven-plugin.md @@ -129,12 +129,14 @@ Description: Parameters: -| Name | Type| Description | -| ------ | ------| -------- | -| **report.targetDir** | String | The directory where the individual report will be generated (default value is **${project.build.directory}/code-analysis**) | -| **report.summary.targetDir** | String | The directory where the summary report, containing links to the individual reports will be generated (Default value is **${session.executionRootDirectory}/target**)| -| **report.fail.on.error** | Boolean | Describes of the build should fail if high priority error is found (Default value is **true**)| -| **report.in.maven** | Boolean | Enable/Disable maven console logging of all messages (Default value is **true**)| +| Name | Type| Description | +|------------------------------| ------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------| +| **report.targetDir** | String | The directory where the individual report will be generated (default value is **${project.build.directory}/code-analysis**) | +| **report.summary.targetDir** | String | The directory where the summary report, containing links to the individual reports will be generated (Default value is **${session.executionRootDirectory}/target**) | +| **report.fail.on.error** | Boolean | Describes of the build should fail if high priority error is found (Default value is **true**) | +| **report.fail.on.warning** | Boolean | Describes of the build should fail if warning is found (Default value is **false**) | +| **report.fail.on.debug** | Boolean | Describes of the build should fail if info is found (Default value is **false**) | +| **report.in.maven** | Boolean | Enable/Disable maven console logging of all messages (Default value is **true**) | ## Customization diff --git a/sat-plugin/src/main/java/org/openhab/tools/analysis/report/ReportMojo.java b/sat-plugin/src/main/java/org/openhab/tools/analysis/report/ReportMojo.java index 0183f282..26284266 100644 --- a/sat-plugin/src/main/java/org/openhab/tools/analysis/report/ReportMojo.java +++ b/sat-plugin/src/main/java/org/openhab/tools/analysis/report/ReportMojo.java @@ -55,7 +55,9 @@ import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatterBuilder; +import java.util.ArrayList; import java.util.LinkedList; +import java.util.List; import java.util.Queue; import javax.xml.parsers.DocumentBuilder; @@ -115,6 +117,18 @@ public class ReportMojo extends AbstractMojo { @Parameter(property = "report.fail.on.error", defaultValue = "true") private boolean failOnError; + /** + * Describes of the build should fail if medium priority error is found + */ + @Parameter(property = "report.fail.on.warning", defaultValue = "false") + private boolean failOnWarning; + + /** + * Describes of the build should fail if low priority error is found + */ + @Parameter(property = "report.fail.on.debug", defaultValue = "false") + private boolean failOnDebug; + /** * The directory where the summary report, containing links to the individual reports will be * generated @@ -142,6 +156,14 @@ public void setFailOnError(boolean failOnError) { this.failOnError = failOnError; } + public void setFailOnWarning(boolean failOnWarning) { + this.failOnWarning = failOnWarning; + } + + public void setFailOnDebug(boolean failOnDebug) { + this.failOnDebug = failOnDebug; + } + public void setSummaryReport(File summaryReport) { this.summaryReportDirectory = summaryReport; } @@ -222,8 +244,8 @@ public void execute() throws MojoFailureException { reportWarningsAndErrors(mergedReport, htmlOutputFileName); } - // 9. Fail the build if the option is enabled and high priority warnings are found - if (failOnError) { + // 9. Fail the build if any level error is enabled and configured error levels are found + if (failOnError || failOnWarning || failOnDebug) { failOnErrors(mergedReport); } @@ -342,12 +364,21 @@ private int countPriority(NodeList messages, String priority) { } private void failOnErrors(File mergedReport) throws MojoFailureException { - int errorCount = selectNodes(mergedReport, "/sca/file/message[@priority=1]").getLength(); + List errorPriorities = new ArrayList<>(); + addLevel(failOnError, "@priority=1", errorPriorities); + addLevel(failOnWarning, "@priority=2", errorPriorities); + addLevel(failOnDebug, "@priority=3", errorPriorities); + String xpathExpression = "/sca/file/message[" + String.join(" or ", errorPriorities) + "]"; + int errorCount = selectNodes(mergedReport, xpathExpression).getLength(); if (errorCount > 0) { - throw new MojoFailureException(String.format( - "%n" + "Code Analysis Tool has found %d error(s)! %n" - + "Please fix the errors and rerun the build. %n", - selectNodes(mergedReport, "/sca/file/message[@priority=1]").getLength())); + throw new MojoFailureException(String.format("%n" + "Code Analysis Tool has found %d error(s)! %n" + + "Please fix the errors and rerun the build. %n", errorCount)); + } + } + + private static void addLevel(boolean level, String levelValue, List levels) { + if (level) { + levels.add(levelValue); } } diff --git a/sat-plugin/src/test/java/org/openhab/tools/analysis/report/ReportMojoTest.java b/sat-plugin/src/test/java/org/openhab/tools/analysis/report/ReportMojoTest.java index d97666a3..459df4d2 100644 --- a/sat-plugin/src/test/java/org/openhab/tools/analysis/report/ReportMojoTest.java +++ b/sat-plugin/src/test/java/org/openhab/tools/analysis/report/ReportMojoTest.java @@ -13,16 +13,21 @@ package org.openhab.tools.analysis.report; import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.params.provider.Arguments.arguments; import static org.mockito.Mockito.verify; import static org.openhab.tools.analysis.report.ReportUtil.RESULT_FILE_NAME; import java.io.File; +import java.util.stream.Stream; import org.apache.maven.plugin.MojoFailureException; import org.apache.maven.plugin.logging.Log; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; @@ -57,29 +62,32 @@ public void setUp() throws Exception { } } - @Test - public void assertReportIsCreatedAndBuildFails() throws Exception { + @ParameterizedTest + @MethodSource("provideReportParameters") + public void assertReportIsCreatedAndBuildFailsAsExpected(boolean failOnError, boolean failOnWarning, + boolean failOnDebug, boolean buildFailExpected) { assertFalse(resultFile.exists()); - subject.setFailOnError(true); + subject.setFailOnError(failOnError); + subject.setFailOnWarning(failOnWarning); + subject.setFailOnDebug(failOnDebug); subject.setSummaryReport(null); subject.setTargetDirectory(new File(TARGET_ABSOLUTE_DIR)); - assertThrows(MojoFailureException.class, () -> subject.execute()); + if (buildFailExpected) { + assertThrows(MojoFailureException.class, () -> subject.execute()); + } else { + assertDoesNotThrow(() -> subject.execute()); + + } assertTrue(resultFile.exists()); } - @Test - public void assertReportISCreatedAndBuildCompletes() throws MojoFailureException { - assertFalse(resultFile.exists()); - - subject.setFailOnError(false); - subject.setSummaryReport(null); - subject.setTargetDirectory(new File(TARGET_ABSOLUTE_DIR)); - - subject.execute(); - - assertTrue(resultFile.exists()); + private static Stream provideReportParameters() { + return Stream.of(arguments(false, false, false, false), arguments(false, false, true, true), + arguments(false, true, false, true), arguments(false, true, true, true), + arguments(true, false, false, true), arguments(true, false, true, true), + arguments(true, true, false, true), arguments(true, true, true, true)); } @Test From dd3b0ac2a76ef71fb7a9e5fe3a81a92739efbd03 Mon Sep 17 00:00:00 2001 From: David Mas Date: Mon, 20 Jan 2025 10:54:13 +0100 Subject: [PATCH 2/3] Improve error message Signed-off-by: David Mas --- .../tools/analysis/report/ReportMojo.java | 47 ++++++++++++++----- 1 file changed, 35 insertions(+), 12 deletions(-) diff --git a/sat-plugin/src/main/java/org/openhab/tools/analysis/report/ReportMojo.java b/sat-plugin/src/main/java/org/openhab/tools/analysis/report/ReportMojo.java index 26284266..ed8accfb 100644 --- a/sat-plugin/src/main/java/org/openhab/tools/analysis/report/ReportMojo.java +++ b/sat-plugin/src/main/java/org/openhab/tools/analysis/report/ReportMojo.java @@ -364,24 +364,47 @@ private int countPriority(NodeList messages, String priority) { } private void failOnErrors(File mergedReport) throws MojoFailureException { - List errorPriorities = new ArrayList<>(); - addLevel(failOnError, "@priority=1", errorPriorities); - addLevel(failOnWarning, "@priority=2", errorPriorities); - addLevel(failOnDebug, "@priority=3", errorPriorities); - String xpathExpression = "/sca/file/message[" + String.join(" or ", errorPriorities) + "]"; - int errorCount = selectNodes(mergedReport, xpathExpression).getLength(); - if (errorCount > 0) { - throw new MojoFailureException(String.format("%n" + "Code Analysis Tool has found %d error(s)! %n" - + "Please fix the errors and rerun the build. %n", errorCount)); + List errorMessages = new ArrayList<>(); + if (failOnError) { + detectFailures(errorMessages, mergedReport, 1); + } + if (failOnWarning) { + detectFailures(errorMessages, mergedReport, 2); + } + if (failOnDebug) { + detectFailures(errorMessages, mergedReport, 3); + } + if (!errorMessages.isEmpty()) { + throw new MojoFailureException(String.join("\n", errorMessages)); + } + } + + private void detectFailures(List errorMessages, File mergedReport, int priority) { + NodeList messages = selectNodes(mergedReport, "/sca/file/message"); + int count = countPriority(messages, String.valueOf(priority)); + if (count > 0) { + errorMessages.add(failureMessage(priority(priority), count)); } } - private static void addLevel(boolean level, String levelValue, List levels) { - if (level) { - levels.add(levelValue); + private String priority(int priority) { + switch (priority) { + case 1: + return "error"; + case 2: + return "warning"; + case 3: + default: + return "info"; } } + private String failureMessage(String severity, int count) { + return String.format( + "%nCode Analysis Tool has found %d %s(s)! %nPlease fix the %s(s) and rerun the build.", + count, severity, severity); + } + private void report(String priority, String log) { switch (priority) { case "1": From 3ff3c6ee0fb2f3891d6cd4f9ed6eada6c3acfa5b Mon Sep 17 00:00:00 2001 From: David Mas Date: Mon, 20 Jan 2025 13:22:30 +0100 Subject: [PATCH 3/3] Fix style Signed-off-by: David Mas --- .../java/org/openhab/tools/analysis/report/ReportMojo.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/sat-plugin/src/main/java/org/openhab/tools/analysis/report/ReportMojo.java b/sat-plugin/src/main/java/org/openhab/tools/analysis/report/ReportMojo.java index ed8accfb..6020647b 100644 --- a/sat-plugin/src/main/java/org/openhab/tools/analysis/report/ReportMojo.java +++ b/sat-plugin/src/main/java/org/openhab/tools/analysis/report/ReportMojo.java @@ -400,8 +400,7 @@ private String priority(int priority) { } private String failureMessage(String severity, int count) { - return String.format( - "%nCode Analysis Tool has found %d %s(s)! %nPlease fix the %s(s) and rerun the build.", + return String.format("%nCode Analysis Tool has found %d %s(s)! %nPlease fix the %s(s) and rerun the build.", count, severity, severity); }