From 33e858ff8cd10b9619b407e80e31df5daa42aa8c Mon Sep 17 00:00:00 2001 From: David Mas <43136762+david-m-s@users.noreply.github.com> Date: Tue, 28 Jan 2025 18:25:10 +0100 Subject: [PATCH] Allow to configure the plugin to fail on any severity (#478) 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 | 69 ++++++++++++++++--- .../tools/analysis/report/ReportMojoTest.java | 38 ++++++---- 3 files changed, 92 insertions(+), 29 deletions(-) diff --git a/docs/maven-plugin.md b/docs/maven-plugin.md index 24000412..65b3b4e7 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.info** | 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..def6f557 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.info", defaultValue = "false") + private boolean failOnInfo; + /** * 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 setFailOnInfo(boolean failOnInfo) { + this.failOnInfo = failOnInfo; + } + 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 || failOnInfo) { failOnErrors(mergedReport); } @@ -342,13 +364,44 @@ private int countPriority(NodeList messages, String priority) { } private void failOnErrors(File mergedReport) throws MojoFailureException { - int errorCount = selectNodes(mergedReport, "/sca/file/message[@priority=1]").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())); + List errorMessages = new ArrayList<>(); + if (failOnError) { + detectFailures(errorMessages, mergedReport, 1); + } + if (failOnWarning) { + detectFailures(errorMessages, mergedReport, 2); + } + if (failOnInfo) { + 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 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) { 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..f70ad217 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 failOnInfo, boolean buildFailExpected) { assertFalse(resultFile.exists()); - subject.setFailOnError(true); + subject.setFailOnError(failOnError); + subject.setFailOnWarning(failOnWarning); + subject.setFailOnInfo(failOnInfo); 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