Skip to content

Commit a3fd025

Browse files
author
David Mas
committed
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.
1 parent 73572e4 commit a3fd025

File tree

3 files changed

+69
-28
lines changed

3 files changed

+69
-28
lines changed

docs/maven-plugin.md

+8-6
Original file line numberDiff line numberDiff line change
@@ -129,12 +129,14 @@ Description:
129129

130130
Parameters:
131131

132-
| Name | Type| Description |
133-
| ------ | ------| -------- |
134-
| **report.targetDir** | String | The directory where the individual report will be generated (default value is **${project.build.directory}/code-analysis**) |
135-
| **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**)|
136-
| **report.fail.on.error** | Boolean | Describes of the build should fail if high priority error is found (Default value is **true**)|
137-
| **report.in.maven** | Boolean | Enable/Disable maven console logging of all messages (Default value is **true**)|
132+
| Name | Type| Description |
133+
|------------------------------| ------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------|
134+
| **report.targetDir** | String | The directory where the individual report will be generated (default value is **${project.build.directory}/code-analysis**) |
135+
| **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**) |
136+
| **report.fail.on.error** | Boolean | Describes of the build should fail if high priority error is found (Default value is **true**) |
137+
| **report.fail.on.warning** | Boolean | Describes of the build should fail if warning is found (Default value is **false**) |
138+
| **report.fail.on.debug** | Boolean | Describes of the build should fail if info is found (Default value is **false**) |
139+
| **report.in.maven** | Boolean | Enable/Disable maven console logging of all messages (Default value is **true**) |
138140

139141
## Customization
140142

sat-plugin/src/main/java/org/openhab/tools/analysis/report/ReportMojo.java

+38-7
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@
5555
import java.time.ZonedDateTime;
5656
import java.time.format.DateTimeFormatter;
5757
import java.time.format.DateTimeFormatterBuilder;
58+
import java.util.ArrayList;
5859
import java.util.LinkedList;
60+
import java.util.List;
5961
import java.util.Queue;
6062

6163
import javax.xml.parsers.DocumentBuilder;
@@ -115,6 +117,18 @@ public class ReportMojo extends AbstractMojo {
115117
@Parameter(property = "report.fail.on.error", defaultValue = "true")
116118
private boolean failOnError;
117119

120+
/**
121+
* Describes of the build should fail if medium priority error is found
122+
*/
123+
@Parameter(property = "report.fail.on.warning", defaultValue = "false")
124+
private boolean failOnWarning;
125+
126+
/**
127+
* Describes of the build should fail if low priority error is found
128+
*/
129+
@Parameter(property = "report.fail.on.debug", defaultValue = "false")
130+
private boolean failOnDebug;
131+
118132
/**
119133
* The directory where the summary report, containing links to the individual reports will be
120134
* generated
@@ -142,6 +156,14 @@ public void setFailOnError(boolean failOnError) {
142156
this.failOnError = failOnError;
143157
}
144158

159+
public void setFailOnWarning(boolean failOnWarning) {
160+
this.failOnWarning = failOnWarning;
161+
}
162+
163+
public void setFailOnDebug(boolean failOnDebug) {
164+
this.failOnDebug = failOnDebug;
165+
}
166+
145167
public void setSummaryReport(File summaryReport) {
146168
this.summaryReportDirectory = summaryReport;
147169
}
@@ -222,8 +244,8 @@ public void execute() throws MojoFailureException {
222244
reportWarningsAndErrors(mergedReport, htmlOutputFileName);
223245
}
224246

225-
// 9. Fail the build if the option is enabled and high priority warnings are found
226-
if (failOnError) {
247+
// 9. Fail the build if any level error is enabled and configured error levels are found
248+
if (failOnError || failOnWarning || failOnDebug) {
227249
failOnErrors(mergedReport);
228250
}
229251

@@ -342,12 +364,21 @@ private int countPriority(NodeList messages, String priority) {
342364
}
343365

344366
private void failOnErrors(File mergedReport) throws MojoFailureException {
345-
int errorCount = selectNodes(mergedReport, "/sca/file/message[@priority=1]").getLength();
367+
List<String> errorPriorities = new ArrayList<>();
368+
addLevel(failOnError, "@priority=1", errorPriorities);
369+
addLevel(failOnWarning, "@priority=2", errorPriorities);
370+
addLevel(failOnDebug, "@priority=3", errorPriorities);
371+
String xpathExpression = "/sca/file/message[" + String.join(" or ", errorPriorities) + "]";
372+
int errorCount = selectNodes(mergedReport, xpathExpression).getLength();
346373
if (errorCount > 0) {
347-
throw new MojoFailureException(String.format(
348-
"%n" + "Code Analysis Tool has found %d error(s)! %n"
349-
+ "Please fix the errors and rerun the build. %n",
350-
selectNodes(mergedReport, "/sca/file/message[@priority=1]").getLength()));
374+
throw new MojoFailureException(String.format("%n" + "Code Analysis Tool has found %d error(s)! %n"
375+
+ "Please fix the errors and rerun the build. %n", errorCount));
376+
}
377+
}
378+
379+
private static void addLevel(boolean level, String levelValue, List<String> levels) {
380+
if (level) {
381+
levels.add(levelValue);
351382
}
352383
}
353384

sat-plugin/src/test/java/org/openhab/tools/analysis/report/ReportMojoTest.java

+23-15
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,21 @@
1313
package org.openhab.tools.analysis.report;
1414

1515
import static org.junit.jupiter.api.Assertions.*;
16+
import static org.junit.jupiter.params.provider.Arguments.arguments;
1617
import static org.mockito.Mockito.verify;
1718
import static org.openhab.tools.analysis.report.ReportUtil.RESULT_FILE_NAME;
1819

1920
import java.io.File;
21+
import java.util.stream.Stream;
2022

2123
import org.apache.maven.plugin.MojoFailureException;
2224
import org.apache.maven.plugin.logging.Log;
2325
import org.junit.jupiter.api.BeforeEach;
2426
import org.junit.jupiter.api.Test;
2527
import org.junit.jupiter.api.extension.ExtendWith;
28+
import org.junit.jupiter.params.ParameterizedTest;
29+
import org.junit.jupiter.params.provider.Arguments;
30+
import org.junit.jupiter.params.provider.MethodSource;
2631
import org.mockito.Mock;
2732
import org.mockito.junit.jupiter.MockitoExtension;
2833

@@ -57,29 +62,32 @@ public void setUp() throws Exception {
5762
}
5863
}
5964

60-
@Test
61-
public void assertReportIsCreatedAndBuildFails() throws Exception {
65+
@ParameterizedTest
66+
@MethodSource("provideReportParameters")
67+
public void assertReportIsCreatedAndBuildFailsAsExpected(boolean failOnError, boolean failOnWarning,
68+
boolean failOnDebug, boolean buildFailExpected) {
6269
assertFalse(resultFile.exists());
6370

64-
subject.setFailOnError(true);
71+
subject.setFailOnError(failOnError);
72+
subject.setFailOnWarning(failOnWarning);
73+
subject.setFailOnDebug(failOnDebug);
6574
subject.setSummaryReport(null);
6675
subject.setTargetDirectory(new File(TARGET_ABSOLUTE_DIR));
6776

68-
assertThrows(MojoFailureException.class, () -> subject.execute());
77+
if (buildFailExpected) {
78+
assertThrows(MojoFailureException.class, () -> subject.execute());
79+
} else {
80+
assertDoesNotThrow(() -> subject.execute());
81+
82+
}
6983
assertTrue(resultFile.exists());
7084
}
7185

72-
@Test
73-
public void assertReportISCreatedAndBuildCompletes() throws MojoFailureException {
74-
assertFalse(resultFile.exists());
75-
76-
subject.setFailOnError(false);
77-
subject.setSummaryReport(null);
78-
subject.setTargetDirectory(new File(TARGET_ABSOLUTE_DIR));
79-
80-
subject.execute();
81-
82-
assertTrue(resultFile.exists());
86+
private static Stream<Arguments> provideReportParameters() {
87+
return Stream.of(arguments(false, false, false, false), arguments(false, false, true, true),
88+
arguments(false, true, false, true), arguments(false, true, true, true),
89+
arguments(true, false, false, true), arguments(true, false, true, true),
90+
arguments(true, true, false, true), arguments(true, true, true, true));
8391
}
8492

8593
@Test

0 commit comments

Comments
 (0)