Skip to content

Commit

Permalink
Allow to configure the plugin to fail on any severity (#478)
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
david-m-s authored Jan 28, 2025
1 parent a14277b commit 33e858f
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 29 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.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

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.info", defaultValue = "false")
private boolean failOnInfo;

/**
* 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 setFailOnInfo(boolean failOnInfo) {
this.failOnInfo = failOnInfo;
}

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 || failOnInfo) {
failOnErrors(mergedReport);
}

Expand Down Expand Up @@ -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<String> 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<String> 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) {
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 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<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 33e858f

Please sign in to comment.