Skip to content

Commit

Permalink
Merge pull request #5 from cizezsy/pipeline
Browse files Browse the repository at this point in the history
[JENKINS-51363] Pipeline support
  • Loading branch information
cizezsy committed May 28, 2018
2 parents c3a07da + ea04c38 commit c722e1f
Show file tree
Hide file tree
Showing 12 changed files with 241 additions and 45 deletions.
Expand Up @@ -107,6 +107,7 @@ private List<CoverageResult> convertToResults(List<CoverageReportAdapter> adapte

// If enable automatically detecting, it will try to find report and correspond adapter.
if (isEnableAutoDetect()) {
listener.getLogger().println("Auto Detect is enabled: Looking for reports...");
List<FilePath> detectedFilePaths = Arrays.stream(workspace.act(new FindReportCallable(autoDetectPath, null)))
.filter(filePath -> {
for (Map.Entry<CoverageReportAdapter, Set<FilePath>> entry : reports.entrySet()) {
Expand All @@ -115,6 +116,7 @@ private List<CoverageResult> convertToResults(List<CoverageReportAdapter> adapte
}
return true;
}).collect(Collectors.toList());
listener.getLogger().printf("Auto Detect was ended: Found %d report%n", detectedFilePaths.size());

try {
Map<CoverageReportAdapter, List<File>> detectedReportFiles = detectReports(detectedFilePaths);
Expand All @@ -132,6 +134,8 @@ private List<CoverageResult> convertToResults(List<CoverageReportAdapter> adapte

if (copiedReport.size() == 0) {
listener.getLogger().println("No reports were found in this path");
} else {
listener.getLogger().printf("A total of %d reports were found%n", copiedReport.size());
}

reports.clear();
Expand Down
26 changes: 21 additions & 5 deletions src/main/java/io/jenkins/plugins/coverage/CoveragePublisher.java
Expand Up @@ -5,6 +5,7 @@
import hudson.FilePath;
import hudson.Launcher;
import hudson.model.AbstractProject;
import hudson.model.Result;
import hudson.model.Run;
import hudson.model.TaskListener;
import hudson.tasks.BuildStepDescriptor;
Expand All @@ -19,6 +20,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,16 +35,17 @@ public class CoveragePublisher extends Recorder implements SimpleBuildStep {
private List<CoverageReportAdapter> adapters;
private List<Threshold> globalThresholds;

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

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

@Override
public void perform(@Nonnull Run<?, ?> run, @Nonnull FilePath workspace, @Nonnull Launcher launcher, @Nonnull TaskListener listener) throws InterruptedException, IOException {
listener.getLogger().println("Publishing Coverage report....");

CoverageProcessor processor = new CoverageProcessor(run, workspace, listener, adapters, globalThresholds);

if (!StringUtils.isEmpty(autoDetectPath)) {
Expand All @@ -64,23 +67,35 @@ 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;
}

public String getAutoDetectPath() {
return autoDetectPath;
}

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

@Symbol("publishCoverage")
@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 +115,7 @@ public DescriptorExtensionList<CoverageReportAdapter, CoverageReportAdapterDescr
return CoverageReportAdapterDescriptor.all();
}

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

import hudson.Extension;
import io.jenkins.plugins.coverage.adapter.util.XMLUtils;
import org.jenkinsci.Symbol;
import org.kohsuke.stapler.DataBoundConstructor;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
Expand Down Expand Up @@ -35,6 +36,7 @@ public String getXSD() {
return null;
}

@Symbol("cobertura")
@Extension
public static final class CoverturaReportAdapterDescriptor
extends CoverageReportAdapterDescriptor<CoberturaReportAdapter>
Expand All @@ -44,7 +46,6 @@ public CoverturaReportAdapterDescriptor() {
super(CoberturaReportAdapter.class, "Cobertura");
}


@Override
public boolean detect(final File file) {
if (!file.exists()) {
Expand Down
Expand Up @@ -4,6 +4,7 @@
import hudson.DescriptorExtensionList;
import hudson.model.Descriptor;
import jenkins.model.Jenkins;
import org.jenkinsci.Symbol;

import javax.annotation.Nonnull;

Expand Down
@@ -1,6 +1,7 @@
package io.jenkins.plugins.coverage.adapter;

import hudson.Extension;
import org.jenkinsci.Symbol;
import org.kohsuke.stapler.DataBoundConstructor;

public final class JacocoReportAdapter extends JavaXMLCoverageReportAdapter {
Expand All @@ -26,6 +27,7 @@ public String getXSD() {
return null;
}

@Symbol("jacoco")
@Extension
public static final class JacocoReportAdapterDescriptor extends CoverageReportAdapterDescriptor<CoverageReportAdapter> {

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,14 +39,8 @@ public XMLCoverageReportAdapter(String path) {
*/
@Override
public Document convert(File source) throws ConversionException {
File xsl;
try {
xsl = getRealXSL();
} catch (FileNotFoundException e) {
e.printStackTrace();
throw new ConversionException(e);
}
try {
StreamSource xsl = getRealXSL();
return XMLUtils.getInstance().convertToDocumentWithXSL(xsl, source);
} catch (FileNotFoundException e) {
e.printStackTrace();
Expand All @@ -55,23 +49,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
37 changes: 30 additions & 7 deletions src/main/java/io/jenkins/plugins/coverage/threshold/Threshold.java
@@ -1,27 +1,34 @@
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.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 +37,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 c722e1f

Please sign in to comment.