Skip to content

Commit

Permalink
Allow to configure the plugin to fail on any severity of detected iss…
Browse files Browse the repository at this point in the history
…ues. 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.
  • Loading branch information
David Mas committed Jan 15, 2025
1 parent 73572e4 commit a3fd025
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 28 deletions.
14 changes: 8 additions & 6 deletions docs/maven-plugin.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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<String> 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<String> levels) {
if (level) {
levels.add(levelValue);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<Arguments> 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
Expand Down

0 comments on commit a3fd025

Please sign in to comment.