Skip to content

Commit

Permalink
[JENKINS-51363] Pipeline support
Browse files Browse the repository at this point in the history
- add @symbol on CoveragePublisher
- remove optional params in @DataBoundConstructor constructor, and using @DataBoundSetter public propery setter to config them.
- fix bug in XMLCoverageReportAdapter, which when plugin is packaged, xsl will not be propery find.
  • Loading branch information
cizezsy committed May 25, 2018
1 parent 3787513 commit c704920
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 39 deletions.
25 changes: 20 additions & 5 deletions src/main/java/io/jenkins/plugins/coverage/CoveragePublisher.java
Expand Up @@ -19,6 +19,7 @@
import jenkins.tasks.SimpleBuildStep;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.jenkinsci.Symbol;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;
import org.kohsuke.stapler.StaplerRequest;
Expand All @@ -33,12 +34,12 @@ public class CoveragePublisher extends Recorder implements SimpleBuildStep {
private List<CoverageReportAdapter> adapters;
private List<Threshold> globalThresholds;

private String autoDetectPath;
@Nonnull
private String autoDetectPath = CoveragePublisherDescriptor.AUTO_DETACT_PATH;

@DataBoundConstructor
public CoveragePublisher(List<CoverageReportAdapter> adapters, List<Threshold> globalThresholds) {
this.adapters = adapters;
this.globalThresholds = globalThresholds;
public CoveragePublisher(@Nonnull String autoDetectPath) {
this.autoDetectPath = autoDetectPath;
}

@Override
Expand All @@ -64,23 +65,36 @@ public List<CoverageReportAdapter> getAdapters() {
return adapters;
}

@DataBoundSetter
public void setAdapters(List<CoverageReportAdapter> adapters) {
this.adapters = adapters;
}

public List<Threshold> getGlobalThresholds() {
return globalThresholds;
}

@DataBoundSetter
public void setGlobalThresholds(List<Threshold> globalThresholds) {
this.globalThresholds = globalThresholds;
}

@Nonnull
public String getAutoDetectPath() {
return autoDetectPath;
}

@DataBoundSetter
public void setAutoDetectPath(String autoDetectPath) {
public void setAutoDetectPath(@Nonnull String autoDetectPath) {
this.autoDetectPath = autoDetectPath;
}

@Symbol("coverage")
@Extension
public static final class CoveragePublisherDescriptor extends BuildStepDescriptor<Publisher> {

public static final String AUTO_DETACT_PATH = "*.xml";

public CoveragePublisherDescriptor() {
super(CoveragePublisher.class);
load();
Expand All @@ -100,6 +114,7 @@ public DescriptorExtensionList<CoverageReportAdapter, CoverageReportAdapterDescr
return CoverageReportAdapterDescriptor.all();
}

@Nonnull
public CoverageMetric[] getAllCoverageMetrics() {
return CoverageMetric.all();
}
Expand Down
Expand Up @@ -7,9 +7,9 @@

import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
import javax.xml.transform.stream.StreamSource;
import java.io.File;
import java.io.FileNotFoundException;
import java.net.URISyntaxException;

public abstract class XMLCoverageReportAdapter extends CoverageReportAdapter {

Expand Down Expand Up @@ -39,13 +39,13 @@ public XMLCoverageReportAdapter(String path) {
*/
@Override
public Document convert(File source) throws ConversionException {
File xsl;
StreamSource xsl = null;
try {
xsl = getRealXSL();
} catch (FileNotFoundException e) {
e.printStackTrace();
throw new ConversionException(e);
}

try {
return XMLUtils.getInstance().convertToDocumentWithXSL(xsl, source);
} catch (FileNotFoundException e) {
Expand All @@ -55,23 +55,12 @@ public Document convert(File source) throws ConversionException {
}

@SuppressWarnings("WeakerAccess")
protected File getRealXSL() throws ConversionException, FileNotFoundException {
try {
String xsl = getXSL();
if (StringUtils.isEmpty(xsl)) {
throw new FileNotFoundException("xsl path must be no-empty");
}

File realXSL = new File(getXSLResourceClass().getResource(xsl).toURI());
if (!realXSL.exists() || !realXSL.isFile()) {
throw new FileNotFoundException("Cannot found xsl file");
} else {
return realXSL;
}
} catch (URISyntaxException e) {
e.printStackTrace();
throw new ConversionException(e);
protected StreamSource getRealXSL() throws FileNotFoundException {
String xsl = getXSL();
if (StringUtils.isEmpty(xsl)) {
throw new FileNotFoundException("Cannot found xsl file, xsl path must be no-empty");
}
return new StreamSource(getXSLResourceClass().getResourceAsStream(xsl));
}

private Class getXSLResourceClass() {
Expand Down
Expand Up @@ -35,7 +35,7 @@ private XMLUtils() {
* @param source Source xml file
* @return Converted document
*/
public Document convertToDocumentWithXSL(File xsl, File source)
public Document convertToDocumentWithXSL(StreamSource xsl, File source)
throws FileNotFoundException, ConversionException {
DOMResult result = convertToDOMResultWithXSL(xsl, source);

Expand All @@ -49,11 +49,8 @@ public Document convertToDocumentWithXSL(File xsl, File source)
* @param source Source file
* @param result Result that want to be written in
*/
private void convertWithXSL(File xsl, File source, Result result)
private void convertWithXSL(StreamSource xsl, File source, Result result)
throws FileNotFoundException, ConversionException {
if (!xsl.exists()) {
throw new FileNotFoundException("XSL File does not exist!");
}

if (!source.exists()) {
throw new FileNotFoundException("source File does not exist!");
Expand All @@ -64,7 +61,7 @@ private void convertWithXSL(File xsl, File source, Result result)
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer;
try {
transformer = transformerFactory.newTransformer(new StreamSource(xsl));
transformer = transformerFactory.newTransformer(xsl);
transformer.transform(new StreamSource(source), result);
} catch (TransformerException e) {
e.printStackTrace();
Expand All @@ -80,7 +77,7 @@ private void convertWithXSL(File xsl, File source, Result result)
* @param source Source xml file
* @return DOMResult
*/
public DOMResult convertToDOMResultWithXSL(File xsl, File source)
public DOMResult convertToDOMResultWithXSL(StreamSource xsl, File source)
throws FileNotFoundException, ConversionException {
DOMResult result = new DOMResult();
convertWithXSL(xsl, source, result);
Expand All @@ -94,7 +91,7 @@ public DOMResult convertToDOMResultWithXSL(File xsl, File source)
* @param source Source xml file
* @return SAXResult
*/
public SAXResult convertToSAXResultWithXSL(File xsl, File source)
public SAXResult convertToSAXResultWithXSL(StreamSource xsl, File source)
throws FileNotFoundException, ConversionException {
SAXResult result = new SAXResult();
convertWithXSL(xsl, source, result);
Expand Down
38 changes: 31 additions & 7 deletions src/main/java/io/jenkins/plugins/coverage/threshold/Threshold.java
@@ -1,27 +1,35 @@
package io.jenkins.plugins.coverage.threshold;

import hudson.Extension;
import hudson.ExtensionPoint;
import hudson.model.Describable;
import hudson.model.Descriptor;
import io.jenkins.plugins.coverage.targets.CoverageMetric;
import jenkins.model.Jenkins;
import org.jenkinsci.Symbol;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;

public class Threshold implements ExtensionPoint, Describable<Threshold> {

private final CoverageMetric threshTarget;

private final float healthyThresh;
private final float unstableThresh;
private final float unhealthyThresh;

// healthy when coverage large than healthy thresh
private float healthyThresh = 80.0f;

// unstable when coverage less than unstable thresh
private float unstableThresh = 0.0f;

// unhealthy when coverage less than unhealthy thresh
private float unhealthyThresh = 0.0f;

@DataBoundConstructor
public Threshold(CoverageMetric threshTarget, float healthyThresh, float unstableThresh, float unhealthyThresh) {
public Threshold(CoverageMetric threshTarget) {
this.threshTarget = threshTarget;
this.healthyThresh = healthyThresh;
this.unstableThresh = unstableThresh;
this.unhealthyThresh = unhealthyThresh;
}


public CoverageMetric getThreshTarget() {
return threshTarget;
}
Expand All @@ -30,20 +38,36 @@ public float getHealthyThresh() {
return healthyThresh;
}

@DataBoundSetter
public void setHealthyThresh(float healthyThresh) {
this.healthyThresh = healthyThresh;
}

public float getUnstableThresh() {
return unstableThresh;
}

@DataBoundSetter
public void setUnstableThresh(float unstableThresh) {
this.unstableThresh = unstableThresh;
}

public float getUnhealthyThresh() {
return unhealthyThresh;
}

@DataBoundSetter
public void setUnhealthyThresh(float unhealthyThresh) {
this.unhealthyThresh = unhealthyThresh;
}

@SuppressWarnings("unchecked")
@Override
public Descriptor<Threshold> getDescriptor() {
return Jenkins.getInstance().getDescriptorOrDie(getClass());
}

@Extension
public static final class ThreshHoldDescriptor<T extends Threshold> extends Descriptor<Threshold> {

public ThreshHoldDescriptor() {
Expand Down

0 comments on commit c704920

Please sign in to comment.