Skip to content

Commit

Permalink
Add the ability to ignore missing check results
Browse files Browse the repository at this point in the history
  • Loading branch information
ambassador86 authored and nfalco79 committed Nov 28, 2024
1 parent 4d8bf72 commit 8184287
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public class DependencyCheckPublisher extends AbstractThresholdPublisher impleme

private String pattern;
private boolean stopBuild = false;
private boolean skipNoReportFiles = false;

@DataBoundConstructor
public DependencyCheckPublisher() {
Expand Down Expand Up @@ -96,6 +97,15 @@ public boolean isStopBuild() {
return stopBuild;
}

public boolean isSkipNoReportFiles() {
return skipNoReportFiles;

Check warning on line 101 in src/main/java/org/jenkinsci/plugins/DependencyCheck/DependencyCheckPublisher.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 101 is not covered by tests
}

@DataBoundSetter
public void setSkipNoReportFiles(boolean skipNoReportFiles) {
this.skipNoReportFiles = skipNoReportFiles;
}

/**
* This method is called whenever the build step is executed.
*
Expand Down Expand Up @@ -133,13 +143,23 @@ public Result process(@NonNull final Run<?, ?> build,
}

Result result = Result.SUCCESS;
final FindingsAggregator findingsAggregator = new FindingsAggregator(build.getNumber());

final FilePath[] odcReportFiles = filePath.list(pattern);
if (ArrayUtils.isEmpty(odcReportFiles)) {
logger.println(Messages.Publisher_NoArtifactsFound());
// build action with empty result or the trend graph will be interrupted and disappear
final ResultAction projectAction = new ResultAction(build,
findingsAggregator.getAggregatedFindings(),
findingsAggregator.getSeverityDistribution());
build.addAction(projectAction);

if (skipNoReportFiles) {
return result;
}
return Result.UNSTABLE;
}

final FindingsAggregator findingsAggregator = new FindingsAggregator(build.getNumber());
for (FilePath odcReportFile : odcReportFiles) {
try {
logger.println(Messages.Publisher_ParsingFile() + " " + odcReportFile.getRemote());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public class DependencyCheckStep extends Step implements Serializable {

private String pattern;
private boolean stopBuild = false;
private boolean skipNoReportFiles = false;
private Integer unstableTotalCritical;
private Integer unstableTotalHigh;
private Integer unstableTotalMedium;
Expand Down Expand Up @@ -92,6 +93,15 @@ public boolean isStopBuild() {
return stopBuild;
}

public boolean isSkipNoReportFiles() {
return skipNoReportFiles;
}

@DataBoundSetter
public void setSkipNoReportFiles(boolean skipNoReportFiles) {
this.skipNoReportFiles = skipNoReportFiles;
}

@Override
public StepExecution start(StepContext context) throws Exception {
return new DependencyCheckStepExecutor(this, context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ protected Void run() throws Exception {
DependencyCheckPublisher publisher = new DependencyCheckPublisher();
publisher.setPattern(step.getPattern());
publisher.setStopBuild(step.isStopBuild());
publisher.setSkipNoReportFiles(step.isSkipNoReportFiles());

publisher.setTotalThresholdAnalysisExploitable(step.isTotalThresholdAnalysisExploitable());
publisher.setFailedTotalCritical(step.getFailedTotalCritical());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ limitations under the License.
<f:checkbox checked="${instance.isStopBuild()}" default="false" />
</f:entry>

<f:entry title="${%skipNoReportFiles.title}" field="skipNoReportFiles">
<f:checkbox checked="${instance.isSkipNoReportFiles()}" default="false" />
</f:entry>

<f:advanced title="Risk Gate Thresholds" align="left">
<f:section title="Risk Gate Thresholds">
<f:entry title="${%Total Findings}" help="/plugin/dependency-check-jenkins-plugin/help-thresholds-total.html">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@
# limitations under the License.

pattern.title=XML Report Pattern
stopBuild.title=Stop build when a failed threshold is violated
stopBuild.title=Stop build when a failed threshold is violated
skipNoReportFiles.title=Skip threshold validation if there are no report files
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div>
If enabled and no report file was found, the build result remains unchanged
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
import org.junit.Rule;
import org.junit.ClassRule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;

Expand All @@ -30,8 +30,8 @@

public class DependencyCheckStepTest {

@Rule
public JenkinsRule jenkinsRule = new JenkinsRule();
@ClassRule
public static JenkinsRule jenkinsRule = new JenkinsRule();

private WorkflowJob getBaseJob(String jobName) throws Exception {
WorkflowJob job = jenkinsRule.jenkins.createProject(WorkflowJob.class, jobName);
Expand Down Expand Up @@ -91,7 +91,7 @@ public void unstable_on_total_high() throws Exception {

@Test
public void stop_build_on_failed_threshold() throws Exception {
WorkflowJob job = getBaseJob("dependencyCheckPublisherWorkflowStepSetLimits");
WorkflowJob job = getBaseJob("dependencyCheckPublisherWorkflowStepStopBuild");
job.setDefinition(new CpsFlowDefinition(""
+ "node {\n"
+ " dependencyCheckPublisher(pattern: '**/dependency-check-report.xml', failedTotalHigh: 0, stopBuild:true)\n"
Expand All @@ -104,4 +104,22 @@ public void stop_build_on_failed_threshold() throws Exception {
assertThat(result.getSeverityDistribution().getHigh()).isPositive();
}

@Test
public void skip_threshold_if_no_report_files_has_been_found() throws Exception {
WorkflowJob job = getBaseJob("dependencyCheckPublisherWorkflowStepSkipNoReportFile");
job.setDefinition(
new CpsFlowDefinition("" + "node {\n" + " dependencyCheckPublisher(pattern: '**/definetlynothere.xml', skipNoReportFiles:false)\n"
+ " echo('Hello World')\n" + "}\n", true));

WorkflowRun run = job.scheduleBuild2(0).get();
jenkinsRule.assertBuildStatus(Result.UNSTABLE, run);

job = getBaseJob("dependencyCheckPublisherWorkflowStepIgnoreMissing2");
job.setDefinition(
new CpsFlowDefinition("" + "node {\n" + " dependencyCheckPublisher(pattern: '**/definetlynothere.xml', skipNoReportFiles:true)\n"
+ " echo('Hello World')\n" + "}\n", true));

run = job.scheduleBuild2(0).get();
jenkinsRule.assertBuildStatus(Result.SUCCESS, run);
}
}

0 comments on commit 8184287

Please sign in to comment.