diff --git a/src/main/java/hudson/plugins/pmd/PmdCutoff.java b/src/main/java/hudson/plugins/pmd/PmdCutoff.java new file mode 100644 index 0000000..c18c598 --- /dev/null +++ b/src/main/java/hudson/plugins/pmd/PmdCutoff.java @@ -0,0 +1,54 @@ +package hudson.plugins.pmd; + +import org.apache.commons.lang.StringUtils; +import org.kohsuke.stapler.export.Exported; +import org.kohsuke.stapler.export.ExportedBean; + +import java.io.Serializable; + +/** + * Holds the cutoff values for high and medium priority. + */ +@ExportedBean +public class PmdCutoff + implements Serializable { + private static final long serialVersionUID = 1L; + + /** + * PMD priority 0..5 for high jenkins priority. + *

+ * Any PMD message with a priority lower (inclusive) than this value is mapped to "high priority". + * If this field is set to "0", then no message is mapped to "high priority". + * If it is set to "5", every message is mapped to "high priority". + */ + @Exported + public String cutoffHighPriority = StringUtils.EMPTY; + + /** + * PMD priority 0..5 for normal jenkins priority. + *

+ * Any PMD message with a priority lower (inclusive) than this value is mapped to "normal priority". + * If this field is set to the same value as cutoffHighPriority, then no message is mapped to "normal priority". + * If this field is set to 5, then no message is mapped to "low priority". + */ + @Exported + public String cutoffNormalPriority = StringUtils.EMPTY; + + public boolean isValid() { + return isValidValue(cutoffHighPriority) + && isValidValue(cutoffNormalPriority) + // high and normal can be the same (then no values are mapped to "normal"), but 'high' must be greater than 'normal'. + && Integer.valueOf(cutoffHighPriority) <= Integer.valueOf(cutoffNormalPriority); + } + + private static boolean isValidValue(String value) { + if (StringUtils.isNotBlank(value)) { + try { + int x = Integer.valueOf(value); + return x >= 0 && x <= 5; + } catch (NumberFormatException ignored) { + } + } + return false; + } +} diff --git a/src/main/java/hudson/plugins/pmd/PmdPublisher.java b/src/main/java/hudson/plugins/pmd/PmdPublisher.java index a8ad634..031e5b7 100644 --- a/src/main/java/hudson/plugins/pmd/PmdPublisher.java +++ b/src/main/java/hudson/plugins/pmd/PmdPublisher.java @@ -23,6 +23,8 @@ import hudson.plugins.analysis.util.PluginLogger; import hudson.plugins.pmd.parser.PmdParser; +import javax.annotation.CheckForNull; + /** * Publishes the results of the PMD analysis (freestyle project type). * @@ -38,6 +40,8 @@ public class PmdPublisher extends HealthAwarePublisher { /** Ant file-set pattern of files to work with. */ private String pattern; + private PmdCutoff pmdCutoff = new PmdCutoff(); + /** * Constructor used from methods like {@link StaplerRequest#bindJSON(Class, JSONObject)} (Class, JSONObject)} and * {@link StaplerRequest#bindParameters(Class, String)}. @@ -66,13 +70,37 @@ public void setPattern(final String pattern) { this.pattern = pattern; } + public PmdCutoff getPmdCutoff() { + return pmdCutoff; + } + + @CheckForNull + public String getCutoffHighPriority() { + return pmdCutoff.cutoffHighPriority; + } + + @DataBoundSetter + public void setCutoffHighPriority(String cutoffHighPriority) { + pmdCutoff.cutoffHighPriority = cutoffHighPriority; + } + + @CheckForNull + public String getCutoffNormalPriority() { + return pmdCutoff.cutoffNormalPriority; + } + + @DataBoundSetter + public void setCutoffNormalPriority(String cutoffNormalPriority) { + pmdCutoff.cutoffNormalPriority = cutoffNormalPriority; + } + @Override public BuildResult perform(final Run build, final FilePath workspace, final PluginLogger logger) throws InterruptedException, IOException { logger.log("Collecting PMD analysis files..."); FilesParser parser = new FilesParser(PLUGIN_NAME, StringUtils.defaultIfEmpty(expandFilePattern(getPattern(), build.getEnvironment(TaskListener.NULL)), DEFAULT_PATTERN), - new PmdParser(getDefaultEncoding()), shouldDetectModules(), isMavenBuild(build)); + new PmdParser(getDefaultEncoding(), pmdCutoff.cutoffHighPriority, pmdCutoff.cutoffNormalPriority), shouldDetectModules(), isMavenBuild(build)); ParserResult project = workspace.act(parser); logger.logLines(project.getLogMessages()); diff --git a/src/main/java/hudson/plugins/pmd/PmdReporter.java b/src/main/java/hudson/plugins/pmd/PmdReporter.java index 412357e..e8f3bae 100644 --- a/src/main/java/hudson/plugins/pmd/PmdReporter.java +++ b/src/main/java/hudson/plugins/pmd/PmdReporter.java @@ -26,12 +26,14 @@ * @author Ulli Hafner */ public class PmdReporter extends HealthAwareReporter { - private static final long serialVersionUID = 2272875032054063496L; + private static final long serialVersionUID = 2272875032054063497L; private static final String PLUGIN_NAME = "PMD"; /** Default PMD pattern. */ private static final String PMD_XML_FILE = "pmd.xml"; + private PmdCutoff pmdCutoff = new PmdCutoff(); + /** * Creates a new instance of PmdReporter. * @@ -99,16 +101,25 @@ public PmdReporter(final String healthy, final String unHealthy, final String th 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 usePreviousBuildAsReference, - final boolean useStableBuildAsReference, final boolean canComputeNew) { + final boolean useStableBuildAsReference, final boolean canComputeNew, + final String cutoffHighPriority, final String cutoffNormalPriority) { super(healthy, unHealthy, thresholdLimit, useDeltaValues, unstableTotalAll, unstableTotalHigh, unstableTotalNormal, unstableTotalLow, unstableNewAll, unstableNewHigh, unstableNewNormal, unstableNewLow, failedTotalAll, failedTotalHigh, failedTotalNormal, failedTotalLow, failedNewAll, failedNewHigh, failedNewNormal, failedNewLow, canRunOnFailed, usePreviousBuildAsReference, useStableBuildAsReference, canComputeNew, PLUGIN_NAME); + + pmdCutoff.cutoffHighPriority = cutoffHighPriority; + pmdCutoff.cutoffNormalPriority = cutoffNormalPriority; } + // CHECKSTYLE:ON + public PmdCutoff getPmdCutoff() { + return pmdCutoff; + } + @Override protected boolean acceptGoal(final String goal) { return "pmd".equals(goal) || "site".equals(goal) || "report".equals(goal) || "check".equals(goal); @@ -117,7 +128,7 @@ protected boolean acceptGoal(final String goal) { @Override public ParserResult perform(final MavenBuildProxy build, final MavenProject pom, final MojoInfo mojo, final PluginLogger logger) throws InterruptedException, IOException { FilesParser pmdCollector = new FilesParser(PLUGIN_NAME, PMD_XML_FILE, - new PmdParser(getDefaultEncoding()), getModuleName(pom)); + new PmdParser(getDefaultEncoding(), pmdCutoff.cutoffHighPriority, pmdCutoff.cutoffNormalPriority), getModuleName(pom)); return getTargetPath(pom).act(pmdCollector); } @@ -147,5 +158,18 @@ protected Class getResultActionClass() { @SuppressWarnings("PMD") @SuppressFBWarnings("") private transient String pattern; // obsolete since release 2.5 + + /** + * Initializes new fields that are not serialized yet. + * + * @return the object + */ + protected Object readResolve() { + if (pmdCutoff == null) { + pmdCutoff = new PmdCutoff(); + } + + return this; + } } diff --git a/src/main/java/hudson/plugins/pmd/parser/PmdParser.java b/src/main/java/hudson/plugins/pmd/parser/PmdParser.java index 314b732..9aa6e05 100644 --- a/src/main/java/hudson/plugins/pmd/parser/PmdParser.java +++ b/src/main/java/hudson/plugins/pmd/parser/PmdParser.java @@ -23,16 +23,19 @@ public class PmdParser extends AbstractAnnotationParser { /** Unique ID of this class. */ private static final long serialVersionUID = 6507147028628714706L; - /** PMD priorities smaller than this value are mapped to {@link Priority#HIGH}. */ - private static final int PMD_PRIORITY_MAPPED_TO_HIGH_PRIORITY = 3; - /** PMD priorities greater than this value are mapped to {@link Priority#LOW}. */ - private static final int PMD_PRIORITY_MAPPED_TO_LOW_PRIORITY = 4; + /** PMD priorities smaller or equal to this value are mapped to {@link Priority#HIGH}. */ + private static final int PMD_DEFAULT_CUTOFF_HIGH_PRIORITY = 2; + /** PMD priorities smaller or equal to this value are mapped to {@link Priority#NORMAL}. */ + private static final int PMD_DEFAULT_CUTOFF_NORMAL_PRIORITY = 4; + + private final int cutoffHighPriority; + private final int cutoffNormalPriority; /** * Creates a new instance of {@link PmdParser}. */ public PmdParser() { - super(StringUtils.EMPTY); + this(StringUtils.EMPTY, StringUtils.EMPTY, StringUtils.EMPTY); } /** @@ -40,9 +43,19 @@ public PmdParser() { * * @param defaultEncoding * the default encoding to be used when reading and parsing files + * + * @param cutoffHighPriority Cutoff for mapping to high priority. + * @param cutoffNormalPriority Cutoff for mapping to normal priority. */ - public PmdParser(final String defaultEncoding) { + public PmdParser(final String defaultEncoding, String cutoffHighPriority, String cutoffNormalPriority) { super(defaultEncoding); + + this.cutoffHighPriority = parseString(cutoffHighPriority, PMD_DEFAULT_CUTOFF_HIGH_PRIORITY); + this.cutoffNormalPriority = parseString(cutoffHighPriority, PMD_DEFAULT_CUTOFF_NORMAL_PRIORITY); + } + + private static final int parseString(String value, int defaultValue) { + return StringUtils.isNotEmpty(value) ? Integer.valueOf(value) : defaultValue; } @Override @@ -95,14 +108,14 @@ private Collection convert(final Pmd collection, final String mo for (hudson.plugins.pmd.parser.File file : collection.getFiles()) { for (Violation warning : file.getViolations()) { Priority priority; - if (warning.getPriority() < PMD_PRIORITY_MAPPED_TO_HIGH_PRIORITY) { + if (warning.getPriority() <= cutoffHighPriority) { priority = Priority.HIGH; } - else if (warning.getPriority() > PMD_PRIORITY_MAPPED_TO_LOW_PRIORITY) { - priority = Priority.LOW; + else if (warning.getPriority() <= cutoffNormalPriority) { + priority = Priority.NORMAL; } else { - priority = Priority.NORMAL; + priority = Priority.LOW; } Bug bug = new Bug(priority, createMessage(warning), warning.getRuleset(), warning.getRule(), warning.getBeginline(), warning.getEndline()); diff --git a/src/main/resources/hudson/plugins/pmd/PmdPublisher/config.jelly b/src/main/resources/hudson/plugins/pmd/PmdPublisher/config.jelly index 3ad39c8..408643d 100644 --- a/src/main/resources/hudson/plugins/pmd/PmdPublisher/config.jelly +++ b/src/main/resources/hudson/plugins/pmd/PmdPublisher/config.jelly @@ -1,11 +1,12 @@ + xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form" xmlns:u="/util" xmlns:pmd="/pmd"> - + + diff --git a/src/main/resources/hudson/plugins/pmd/PmdReporter/config.jelly b/src/main/resources/hudson/plugins/pmd/PmdReporter/config.jelly index ecb7398..1e7c3e0 100644 --- a/src/main/resources/hudson/plugins/pmd/PmdReporter/config.jelly +++ b/src/main/resources/hudson/plugins/pmd/PmdReporter/config.jelly @@ -1,7 +1,8 @@ - - - - - - \ No newline at end of file + + + + + + + diff --git a/src/main/resources/pmd/cutoff.jelly b/src/main/resources/pmd/cutoff.jelly new file mode 100644 index 0000000..a44029e --- /dev/null +++ b/src/main/resources/pmd/cutoff.jelly @@ -0,0 +1,27 @@ + + + + + + + + + + + + + +
+ 100% ${%field.high.value} + + + + + 0% ${%field.normal.value} + + +
+
+ +
diff --git a/src/main/resources/pmd/cutoff.properties b/src/main/resources/pmd/cutoff.properties new file mode 100644 index 0000000..0176e31 --- /dev/null +++ b/src/main/resources/pmd/cutoff.properties @@ -0,0 +1,6 @@ +description.high.value=PMD priority 0..5 for high jenkins priority.\nAny PMD message with a priority lower (inclusive) than this value is mapped to "high priority".\nIf this field is set to "0", then no message is mapped to "high priority".\nIf it is set to "5", every message is mapped to "high priority". +description.normal.value=PMD priority 0..5 for normal jenkins priority.\nAny PMD message with a priority lower (inclusive) than this value is mapped to "normal priority".\nIf this field is set to the same value as high priority, then no message is mapped to "normal priority".\nIf this field is set to 5, then no message is mapped to "low priority". +description.pmd.cutoff=Cutoff values to map PMD priorities (1-5) to Jenkins priorities (HIGH, NORMAL, LOW). Any value lower (inclusive) than the values configured here will be mapped to the respective priorities. +field.high.value=Priority high +field.normal.value=Priority normal +title.pmd.cutoff=PMD cutoff values diff --git a/src/main/resources/pmd/taglib b/src/main/resources/pmd/taglib new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/src/main/resources/pmd/taglib @@ -0,0 +1 @@ +