Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge branch 'JENKINS-29908' of https://github.com/recena/dependency-…
…check-plugin into recena-JENKINS-29908

# Conflicts:
#	pom.xml
  • Loading branch information
Steve Springett committed Sep 8, 2015
2 parents 930afe3 + ae0564a commit 6ba0325
Show file tree
Hide file tree
Showing 8 changed files with 3,013 additions and 15 deletions.
25 changes: 22 additions & 3 deletions pom.xml
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>1.554.3</version>
<version>1.596.1</version>
</parent>

<!--
Expand Down Expand Up @@ -49,6 +49,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<dependency.check.version>1.3.1-SNAPSHOT</dependency.check.version>
<workflow-jenkins-plugin.version>1.4</workflow-jenkins-plugin.version>
</properties>

<build>
Expand Down Expand Up @@ -84,7 +85,7 @@
<dependency>
<groupId>org.jvnet.hudson.plugins</groupId>
<artifactId>analysis-core</artifactId>
<version>1.72</version>
<version>1.73</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
Expand Down Expand Up @@ -118,14 +119,32 @@
<artifactId>maven-plugin</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-job</artifactId>
<version>${workflow-jenkins-plugin.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-cps</artifactId>
<version>${workflow-jenkins-plugin.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-basic-steps</artifactId>
<version>${workflow-jenkins-plugin.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

<scm>
<connection>scm:git:git@github.com:jenkinsci/dependency-check-plugin.git</connection>
<url>https://github.com/jenkinsci/dependency-check-plugin.git</url>
<developerConnection>scm:git:git@github.com:jenkinsci/dependency-check-plugin.git</developerConnection>
<tag>HEAD</tag>
</scm>
</scm>

<issueManagement>
<system>JIRA</system>
Expand Down
Expand Up @@ -30,6 +30,7 @@
import hudson.tasks.Builder;
import hudson.triggers.SCMTrigger;
import jenkins.model.Jenkins;
import jenkins.security.MasterToSlaveCallable;
import org.apache.commons.lang.StringUtils;

import java.io.File;
Expand Down Expand Up @@ -77,22 +78,22 @@ public boolean perform(final AbstractBuild build, final Launcher launcher, final
}

// Get the version of the plugin and print it out
final PluginWrapper wrapper = Hudson.getInstance().getPluginManager().getPlugin(DependencyCheckDescriptor.PLUGIN_ID);
final PluginWrapper wrapper = Jenkins.getInstance().getPluginManager().getPlugin(DependencyCheckDescriptor.PLUGIN_ID);
listener.getLogger().println(OUT_TAG + wrapper.getLongName() + " v" + wrapper.getVersion());

final ClassLoader classLoader = wrapper.classLoader;
final boolean isMaster = (build.getBuiltOn() == Hudson.getInstance());
final boolean isMaster = (build.getBuiltOn() == Jenkins.getInstance());

// Node-agnostic execution of Dependency-Check
if (isMaster) {
return launcher.getChannel().call(new Callable<Boolean, IOException>() {
return launcher.getChannel().call(new MasterToSlaveCallable<Boolean, IOException>() {
public Boolean call() throws IOException {
final DependencyCheckExecutor executor = new DependencyCheckExecutor(options, listener, classLoader);
return executor.performBuild();
}
});
} else {
return launcher.getChannel().call(new Callable<Boolean, IOException>() {
return launcher.getChannel().call(new MasterToSlaveCallable<Boolean, IOException>() {
public Boolean call() throws IOException {
final DependencyCheckExecutor executor = new DependencyCheckExecutor(options, listener);
return executor.performBuild();
Expand Down
Expand Up @@ -15,13 +15,14 @@
*/
package org.jenkinsci.plugins.DependencyCheck;

import hudson.FilePath;
import hudson.Launcher;
import hudson.matrix.MatrixAggregator;
import hudson.matrix.MatrixBuild;
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.model.Action;
import hudson.model.BuildListener;
import hudson.model.Run;
import hudson.plugins.analysis.core.BuildResult;
import hudson.plugins.analysis.core.FilesParser;
import hudson.plugins.analysis.core.HealthAwarePublisher;
Expand All @@ -30,6 +31,7 @@
import org.apache.commons.lang.StringUtils;
import org.jenkinsci.plugins.DependencyCheck.parser.ReportParser;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;

import java.io.IOException;

Expand All @@ -46,7 +48,7 @@ public class DependencyCheckPublisher extends HealthAwarePublisher {
private static final String DEFAULT_PATTERN = "**/dependency-check-report.xml";

// Ant file-set pattern of files to work with.
private final String pattern;
private String pattern;

/**
* Creates a new instance of <code>DependencyCheckPublisher</code>.
Expand Down Expand Up @@ -111,10 +113,12 @@ public class DependencyCheckPublisher extends HealthAwarePublisher {
* determines whether module names should be derived from Maven POM or Ant build files
* @param pattern
* Ant file-set pattern to scan for PMD files
*
* @deprecated see {@link #DependencyCheckPublisher()}
*/
// CHECKSTYLE:OFF
@SuppressWarnings("PMD.ExcessiveParameterList")
@DataBoundConstructor
@Deprecated
public DependencyCheckPublisher(final String healthy, final String unHealthy, final String thresholdLimit,
final String defaultEncoding, final boolean useDeltaValues,
final String unstableTotalAll, final String unstableTotalHigh, final String unstableTotalNormal, final String unstableTotalLow,
Expand All @@ -134,6 +138,16 @@ public DependencyCheckPublisher(final String healthy, final String unHealthy, fi
}
// CHECKSTYLE:ON


/**
* Constructor used from methods like {@link StaplerRequest#bindJSON(Class, JSONObject)} and
* {@link StaplerRequest#bindParameters(Class, String)}.
*/
@DataBoundConstructor
public DependencyCheckPublisher() {
super(DependencyCheckPlugin.PLUGIN_NAME);
}

/**
* Returns the Ant file-set pattern of files to work with.
*
Expand All @@ -143,21 +157,31 @@ public String getPattern() {
return pattern;
}

/**
* Sets the Ant file-set pattern of files to work with.
*/
@DataBoundSetter
public void setPattern(final String pattern) {
this.pattern = pattern;
}

@Override
public Action getProjectAction(final AbstractProject<?, ?> project) {
return new DependencyCheckProjectAction(project);
}

@Override
public BuildResult perform(final AbstractBuild<?, ?> build, final PluginLogger logger) throws InterruptedException, IOException {
public BuildResult perform(final Run<?, ?> build, final FilePath workspace, final PluginLogger logger) throws InterruptedException, IOException {
logger.log("Collecting Dependency-Check analysis files...");
final FilesParser dcCollector = new FilesParser(DependencyCheckPlugin.PLUGIN_NAME, StringUtils.defaultIfEmpty(getPattern(), DEFAULT_PATTERN),

FilesParser parser = new FilesParser(DependencyCheckPlugin.PLUGIN_NAME, StringUtils.defaultIfEmpty(getPattern(), DEFAULT_PATTERN),
new ReportParser(getDefaultEncoding()), shouldDetectModules(), isMavenBuild(build));
final ParserResult project = build.getWorkspace().act(dcCollector);

ParserResult project = workspace.act(parser);
logger.logLines(project.getLogMessages());

final DependencyCheckResult result = new DependencyCheckResult(build, getDefaultEncoding(), project, usePreviousBuildAsReference(), useOnlyStableBuildsAsReference());
build.getActions().add(new DependencyCheckResultAction(build, this, result));
DependencyCheckResult result = new DependencyCheckResult(build, getDefaultEncoding(), project, usePreviousBuildAsReference(), useOnlyStableBuildsAsReference());
build.addAction(new DependencyCheckResultAction(build, this, result));

return result;
}
Expand Down
Expand Up @@ -17,6 +17,7 @@

import com.thoughtworks.xstream.XStream;
import hudson.model.AbstractBuild;
import hudson.model.Run;
import hudson.plugins.analysis.core.BuildHistory;
import hudson.plugins.analysis.core.BuildResult;
import hudson.plugins.analysis.core.ParserResult;
Expand Down Expand Up @@ -49,9 +50,33 @@ public class DependencyCheckResult extends BuildResult {
* @param useStableBuildAsReference
* determines whether only stable builds should be used as
* reference builds or not
*
* @deprecated use {@link #DependencyCheckResult(Run, String, ParserResult, boolean, boolean)}
*/
@Deprecated
public DependencyCheckResult(final AbstractBuild<?, ?> build, final String defaultEncoding, final ParserResult result,
final boolean usePreviousBuildAsReference, final boolean useStableBuildAsReference) {
this((Run<?, ?>) build, defaultEncoding, result, usePreviousBuildAsReference, useStableBuildAsReference, DependencyCheckResultAction.class);
}

/**
* Creates a new instance of {@link DependencyCheckResult}.
*
* @param build
* the current build as owner of this action
* @param defaultEncoding
* the default encoding to be used when reading and parsing files
* @param result
* the parsed result with all annotations
* @param usePreviousBuildAsReference
* determines whether to use the previous build as the reference
* build
* @param useStableBuildAsReference
* determines whether only stable builds should be used as
* reference builds or not
*/
public DependencyCheckResult(final Run<?, ?> build, final String defaultEncoding, final ParserResult result,
final boolean usePreviousBuildAsReference, final boolean useStableBuildAsReference) {
this(build, defaultEncoding, result, usePreviousBuildAsReference, useStableBuildAsReference, DependencyCheckResultAction.class);
}

Expand All @@ -64,15 +89,44 @@ public DependencyCheckResult(final AbstractBuild<?, ?> build, final String defau
* @param usePreviousBuildAsReference the value of usePreviousBuildAsReference
* @param useStableBuildAsReference determines whether only stable builds should be used as reference builds or not
* @param actionType the type of the result action
*
* @deprecated use {@link #DependencyCheckResult(Run, BuildHistory, ParserResult, String, boolean)}
*/
@Deprecated
protected DependencyCheckResult(final AbstractBuild<?, ?> build, final String defaultEncoding, final ParserResult result,
final boolean usePreviousBuildAsReference, final boolean useStableBuildAsReference,
final Class<? extends ResultAction<DependencyCheckResult>> actionType) {
this((Run<?, ?>) build, new BuildHistory(build, actionType, usePreviousBuildAsReference, useStableBuildAsReference), result, defaultEncoding, true);
}

/**
* Creates a new instance of {@link DependencyCheckResult}.
*
* @param build the current build as owner of this action
* @param defaultEncoding the default encoding to be used when reading and parsing files
* @param result the parsed result with all annotations
* @param usePreviousBuildAsReference the value of usePreviousBuildAsReference
* @param useStableBuildAsReference determines whether only stable builds should be used as reference builds or not
* @param actionType the type of the result action
*/
protected DependencyCheckResult(final Run<?, ?> build, final String defaultEncoding, final ParserResult result,
final boolean usePreviousBuildAsReference, final boolean useStableBuildAsReference,
final Class<? extends ResultAction<DependencyCheckResult>> actionType) {
this(build, new BuildHistory(build, actionType, usePreviousBuildAsReference, useStableBuildAsReference), result, defaultEncoding, true);
}

/**
* @deprecated see {@link #DependencyCheckResult(Run, BuildHistory, ParserResult, String, boolean)}
*/
@Deprecated
DependencyCheckResult(final AbstractBuild<?, ?> build, final BuildHistory history, final ParserResult result,
final String defaultEncoding, final boolean canSerialize) {

this((Run<?, ?>) build, history, result, defaultEncoding, canSerialize);
}

DependencyCheckResult(final Run<?, ?> build, final BuildHistory history, final ParserResult result,
final String defaultEncoding, final boolean canSerialize) {
super(build, history, result, defaultEncoding);

if (canSerialize) {
Expand Down
Expand Up @@ -16,6 +16,7 @@
package org.jenkinsci.plugins.DependencyCheck;

import hudson.model.AbstractBuild;
import hudson.model.Run;
import hudson.plugins.analysis.core.AbstractResultAction;
import hudson.plugins.analysis.core.HealthDescriptor;
import hudson.plugins.analysis.core.PluginDescriptor;
Expand All @@ -39,8 +40,22 @@ public class DependencyCheckResultAction extends AbstractResultAction<Dependency
* @param owner the associated build of this action
* @param healthDescriptor health descriptor to use
* @param result the result in this build
*
* @deprecated see {@link #DependencyCheckResultAction(Run, HealthDescriptor, DependencyCheckResult)}
*/
@Deprecated
public DependencyCheckResultAction(final AbstractBuild<?, ?> owner, final HealthDescriptor healthDescriptor, final DependencyCheckResult result) {
this((Run<?, ?>) owner, healthDescriptor, result);
}

/**
* Creates a new instance of <code>DependencyCheckResultAction</code>.
*
* @param owner the associated build of this action
* @param healthDescriptor health descriptor to use
* @param result the result in this build
*/
public DependencyCheckResultAction(final Run<?, ?> owner, final HealthDescriptor healthDescriptor, final DependencyCheckResult result) {
super(owner, new DependencyCheckHealthDescriptor(healthDescriptor), result);
}

Expand Down
@@ -0,0 +1,79 @@
package org.jenkinsci.plugins.dependencycheck;

import hudson.FilePath;
import hudson.model.Result;
import org.jenkinsci.plugins.DependencyCheck.DependencyCheckPublisher;
import org.jenkinsci.plugins.DependencyCheck.DependencyCheckResultAction;
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;

import static org.junit.Assert.*;

public class DependencyCheckWorkflowTest {

@Rule
public JenkinsRule jenkinsRule = new JenkinsRule();

/**
* Run a workflow job using {@link org.jenkinsci.plugins.DependencyCheck.DependencyCheckPublisher} and check for success.
*/
@Test
public void dependencyCheckPublisherWorkflowStep() throws Exception {
WorkflowJob job = jenkinsRule.jenkins.createProject(WorkflowJob.class, "dependencyCheckWorkPublisherWorkflowStep");
FilePath workspace = jenkinsRule.jenkins.getWorkspaceFor(job);
FilePath report = workspace.child("target").child("dependency-check-report.xml");
report.copyFrom(DependencyCheckWorkflowTest.class.getResourceAsStream("/org/jenkinsci/plugins/dependencycheck/parser/dependency-check-report2.xml"));
job.setDefinition(new CpsFlowDefinition(""
+ "node {\n"
+ " step([$class: 'DependencyCheckPublisher'])\n"
+ "}\n", true)
);
jenkinsRule.assertBuildStatusSuccess(job.scheduleBuild2(0));
DependencyCheckResultAction result = job.getLastBuild().getAction(DependencyCheckResultAction.class);
assertTrue(result.getResult().getAnnotations().size() == 2);
}

/**
* Run a workflow job using {@link DependencyCheckPublisher} with a failing threshold of 0, so the given example file
* "/org/jenkinsci/plugins/dependencycheck/parser/dependency-check-report2.xml" will make the build to fail.
*/
@Test
public void dependencyCheckPublisherWorkflowStepSetLimits() throws Exception {
WorkflowJob job = jenkinsRule.jenkins.createProject(WorkflowJob.class, "dependencyCheckPublisherWorkflowStepSetLimits");
FilePath workspace = jenkinsRule.jenkins.getWorkspaceFor(job);
FilePath report = workspace.child("target").child("dependency-check-report.xml");
report.copyFrom(DependencyCheckWorkflowTest.class.getResourceAsStream("/org/jenkinsci/plugins/dependencycheck/parser/dependency-check-report2.xml"));
job.setDefinition(new CpsFlowDefinition(""
+ "node {\n"
+ " step([$class: 'DependencyCheckPublisher', pattern: '**/dependency-check-report.xml', failedTotalAll: '0', usePreviousBuildAsReference: false])\n"
+ "}\n", true)
);
jenkinsRule.assertBuildStatus(Result.FAILURE, job.scheduleBuild2(0).get());
DependencyCheckResultAction result = job.getLastBuild().getAction(DependencyCheckResultAction.class);
assertTrue(result.getResult().getAnnotations().size() == 2);
}

/**
* Run a workflow job using {@link DependencyCheckPublisher} with a unstable threshold of 0, so the given example file
* "/org/jenkinsci/plugins/dependencycheck/parser/dependency-check-report2.xml" will make the build to fail.
*/
@Test
public void dependencyCheckPublisherWorkflowStepFailure() throws Exception {
WorkflowJob job = jenkinsRule.jenkins.createProject(WorkflowJob.class, "dependencyCheckPublisherWorkflowStepFailure");
FilePath workspace = jenkinsRule.jenkins.getWorkspaceFor(job);
FilePath report = workspace.child("target").child("dependency-check-report.xml");
report.copyFrom(DependencyCheckWorkflowTest.class.getResourceAsStream("/org/jenkinsci/plugins/dependencycheck/parser/dependency-check-report2.xml"));
job.setDefinition(new CpsFlowDefinition(""
+ "node {\n"
+ " step([$class: 'DependencyCheckPublisher', pattern: '**/dependency-check-report.xml', unstableTotalAll: '0', usePreviousBuildAsReference: false])\n"
+ "}\n")
);
jenkinsRule.assertBuildStatus(Result.UNSTABLE, job.scheduleBuild2(0).get());
DependencyCheckResultAction result = job.getLastBuild().getAction(DependencyCheckResultAction.class);
assertTrue(result.getResult().getAnnotations().size() == 2);
}
}

0 comments on commit 6ba0325

Please sign in to comment.