package hudson.plugins.dry;
import hudson.Launcher;
import hudson.matrix.MatrixAggregator;
import hudson.matrix.MatrixBuild;
import hudson.model.Action;
import hudson.model.BuildListener;
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.plugins.analysis.core.FilesParser;
import hudson.plugins.analysis.core.HealthAwarePublisher;
import hudson.plugins.analysis.core.ParserResult;
import hudson.plugins.analysis.core.BuildResult;
import hudson.plugins.analysis.util.PluginLogger;
import hudson.plugins.dry.parser.DuplicationParserRegistry;
import java.io.IOException;
import org.apache.commons.lang.StringUtils;
import org.kohsuke.stapler.DataBoundConstructor;
/**
* Publishes the results of the duplicate code analysis (freestyle project type).
*
* @author Ulli Hafner
*/
public class DryPublisher extends HealthAwarePublisher {
/** Unique ID of this class. */
private static final long serialVersionUID = 6711252664481150129L;
private static final String PLUGIN_NAME = "DRY";
/** Validates the thresholds user input. */
private static final ThresholdValidation THRESHOLD_VALIDATION = new ThresholdValidation();
private static final String DEFAULT_DRY_PATTERN = "**/cpd.xml";
/** Ant file-set pattern of files to work with. */
private final String pattern;
/** Minimum number of duplicate lines for high priority warnings. @since 2.5 */
private final int highThreshold;
/** Minimum number of duplicate lines for normal priority warnings. @since 2.5 */
private final int normalThreshold;
/**
* Creates a new instance of <code>PmdPublisher</code>.
*
* @param healthy
* Report health as 100% when the number of warnings is less than
* this value
* @param unHealthy
* Report health as 0% when the number of warnings is greater
* than this value
* @param thresholdLimit
* determines which warning priorities should be considered when
* evaluating the build stability and health
* @param defaultEncoding
* the default encoding to be used when reading and parsing files
* @param useDeltaValues
* determines whether the absolute annotations delta or the
* actual annotations set difference should be used to evaluate
* the build stability
* @param unstableTotalAll
* annotation threshold
* @param unstableTotalHigh
* annotation threshold
* @param unstableTotalNormal
* annotation threshold
* @param unstableTotalLow
* annotation threshold
* @param unstableNewAll
* annotation threshold
* @param unstableNewHigh
* annotation threshold
* @param unstableNewNormal
* annotation threshold
* @param unstableNewLow
* annotation threshold
* @param failedTotalAll
* annotation threshold
* @param failedTotalHigh
* annotation threshold
* @param failedTotalNormal
* annotation threshold
* @param failedTotalLow
* annotation threshold
* @param failedNewAll
* annotation threshold
* @param failedNewHigh
* annotation threshold
* @param failedNewNormal
* annotation threshold
* @param failedNewLow
* annotation threshold
* @param canRunOnFailed
* determines whether the plug-in can run for failed builds, too
* @param useStableBuildAsReference
* determines whether only stable builds should be used as reference builds or not
* @param shouldDetectModules
* determines whether module names should be derived from Maven POM or Ant build files
* @param canComputeNew
* determines whether new warnings should be computed (with
* respect to baseline)
* @param pattern
* Ant file-set pattern to scan for DRY files
* @param highThreshold
* minimum number of duplicate lines for high priority warnings
* @param normalThreshold
* minimum number of duplicate lines for normal priority warnings
*/
// CHECKSTYLE:OFF
@SuppressWarnings("PMD.ExcessiveParameterList")
@DataBoundConstructor
public DryPublisher(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,
final String unstableNewAll, final String unstableNewHigh, final String unstableNewNormal, final String unstableNewLow,
final String failedTotalAll, final String failedTotalHigh, final String failedTotalNormal, final String failedTotalLow,
final String failedNewAll, final String failedNewHigh, final String failedNewNormal, final String failedNewLow,
final boolean canRunOnFailed, final boolean useStableBuildAsReference, final boolean shouldDetectModules,
final boolean canComputeNew, final String pattern, final int highThreshold, final int normalThreshold) {
super(healthy, unHealthy, thresholdLimit, defaultEncoding, useDeltaValues,
unstableTotalAll, unstableTotalHigh, unstableTotalNormal, unstableTotalLow,
unstableNewAll, unstableNewHigh, unstableNewNormal, unstableNewLow,
failedTotalAll, failedTotalHigh, failedTotalNormal, failedTotalLow,
failedNewAll, failedNewHigh, failedNewNormal, failedNewLow,
canRunOnFailed, useStableBuildAsReference, shouldDetectModules, canComputeNew, true, PLUGIN_NAME);
this.pattern = pattern;
this.highThreshold = highThreshold;
this.normalThreshold = normalThreshold;
}
// CHECKSTYLE:ON
/**
* Returns the minimum number of duplicate lines for high priority warnings.
*
* @return the minimum number of duplicate lines for high priority warnings
*/
public int getHighThreshold() {
return THRESHOLD_VALIDATION.getHighThreshold(normalThreshold, highThreshold);
}
/**
* Returns the minimum number of duplicate lines for normal warnings.
*
* @return the minimum number of duplicate lines for normal warnings
*/
public int getNormalThreshold() {
return THRESHOLD_VALIDATION.getNormalThreshold(normalThreshold, highThreshold);
}
/**
* Returns the Ant file-set pattern of files to work with.
*
* @return Ant file-set pattern of files to work with
*/
public String getPattern() {
return pattern;
}
@Override
public Action getProjectAction(final AbstractProject<?, ?> project) {
return new DryProjectAction(project);
}
@Override
public BuildResult perform(final AbstractBuild<?, ?> build, final PluginLogger logger) throws InterruptedException, IOException {
logger.log("Collecting duplicate code analysis files...");
FilesParser dryCollector = new FilesParser(PLUGIN_NAME, StringUtils.defaultIfEmpty(getPattern(), DEFAULT_DRY_PATTERN),
new DuplicationParserRegistry(getNormalThreshold(), getHighThreshold(), build.getWorkspace().getRemote(),
getDefaultEncoding()),
shouldDetectModules(), isMavenBuild(build));
ParserResult project = build.getWorkspace().act(dryCollector);
logger.logLines(project.getLogMessages());
DryResult result = new DryResult(build, getDefaultEncoding(), project, useOnlyStableBuildsAsReference());
build.getActions().add(new DryResultAction(build, this, result));
return result;
}
@Override
public DryDescriptor getDescriptor() {
return (DryDescriptor)super.getDescriptor();
}
@Override
public MatrixAggregator createAggregator(final MatrixBuild build, final Launcher launcher,
final BuildListener listener) {
return new DryAnnotationsAggregator(build, launcher, listener, this, getDefaultEncoding(), useOnlyStableBuildsAsReference());
}
}