Skip to content
Permalink
Browse files

JENKINS-30700 / #50 Jenkins pipeline support

  • Loading branch information...
mbarrien committed Jun 9, 2016
1 parent 270872b commit 85fd6380809a45dff5319d8eb385920d2163fe10
33 pom.xml
@@ -4,7 +4,7 @@
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>1.480.3</version>
<version>1.609.2</version>
</parent>

<artifactId>cobertura</artifactId>
@@ -15,6 +15,7 @@

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<workflow-jenkins-plugin.version>1.11</workflow-jenkins-plugin.version>
</properties>

<scm>
@@ -43,18 +44,6 @@
</developers>

<dependencies>
<dependency>
<groupId>org.jenkins-ci.main</groupId>
<artifactId>maven-plugin</artifactId>
<scope>compile</scope>
<!-- Exclude a dependency that conflicts with easymock dependencies -->
<exclusions>
<exclusion>
<groupId>org.sonatype.sisu.inject</groupId>
<artifactId>cglib</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.easymock</groupId>
<artifactId>easymockclassextension</artifactId>
@@ -85,6 +74,24 @@
<version>4.0</version>
<scope>test</scope>
</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>

<build>
@@ -1,9 +1,9 @@
package hudson.plugins.cobertura;

import hudson.model.AbstractBuild;
import hudson.model.Run;

public class BuildUtils {
public static AbstractBuild<?, ?> getPreviousNotFailedCompletedBuild(AbstractBuild<?, ?> b) {
public static Run<?, ?> getPreviousNotFailedCompletedBuild(Run<?, ?> b) {
while (true) {
b = b.getPreviousNotFailedBuild();
if (b == null) {
@@ -1,6 +1,6 @@
package hudson.plugins.cobertura;

import hudson.model.AbstractBuild;
import hudson.model.Run;
import hudson.plugins.cobertura.targets.CoverageMetric;

import java.util.Map;
@@ -12,6 +12,6 @@

Map<CoverageMetric, Ratio> getResults();

AbstractBuild< ? , ? > getOwner();
Run<?, ?> getOwner();

}
@@ -1,19 +1,24 @@
package hudson.plugins.cobertura;

import hudson.model.Action;
import hudson.model.HealthReport;
import hudson.model.HealthReportingAction;
import hudson.model.Result;
import hudson.model.AbstractBuild;
import hudson.model.Run;
import hudson.plugins.cobertura.targets.CoverageMetric;
import hudson.plugins.cobertura.targets.CoverageTarget;
import hudson.plugins.cobertura.targets.CoverageResult;
import hudson.util.ChartUtil;
import hudson.util.DescribableList;
import jenkins.model.RunAction2;
import jenkins.tasks.SimpleBuildStep;

import java.io.File;
import java.io.IOException;
import java.lang.ref.WeakReference;
import java.util.Calendar;
import java.util.Collection;
import java.util.Collections;
import java.util.EnumMap;
import java.util.Map;
import java.util.logging.Level;
@@ -31,15 +36,17 @@
* @author connollys
* @since 03-Jul-2007 08:43:08
*/
public class CoberturaBuildAction implements HealthReportingAction, StaplerProxy, Chartable {
public class CoberturaBuildAction implements HealthReportingAction, StaplerProxy, Chartable, SimpleBuildStep.LastBuildAction, RunAction2 {

private final AbstractBuild<?, ?> owner;
private Run<?, ?> owner;
private CoverageTarget healthyTarget;
private CoverageTarget unhealthyTarget;
private boolean failUnhealthy;
private boolean failUnstable;
private boolean autoUpdateHealth;
private boolean autoUpdateStability;
private boolean zoomCoverageChart;
private int maxNumberOfBuilds;
/**
* Overall coverage result.
*/
@@ -56,17 +63,6 @@ public HealthReport getBuildHealth() {
if (health != null) {
return health;
}
//try to get targets from root project (for maven modules targets are null)
DescribableList rootpublishers = owner.getProject().getRootProject().getPublishersList();

if (rootpublishers != null) {
CoberturaPublisher publisher = (CoberturaPublisher) rootpublishers.get(CoberturaPublisher.class);
if (publisher != null) {
healthyTarget = publisher.getHealthyTarget();
unhealthyTarget = publisher.getUnhealthyTarget();
}
}

if (healthyTarget == null || unhealthyTarget == null) {
return null;
}
@@ -135,10 +131,20 @@ public Object getTarget() {
return getResult(); //To change body of implemented methods use File | Settings | File Templates.
}

public AbstractBuild<?, ?> getOwner() {
public Run<?, ?> getOwner() {
return owner;
}

private void setOwner(Run<?, ?> owner) {
this.owner = owner;
if (report != null) {
CoverageResult r = report.get();
if (r != null) {
r.setOwner(owner);
}
}
}

public Map<CoverageMetric, Ratio> getResults() {
return result;
}
@@ -156,8 +162,8 @@ public CoberturaBuildAction getPreviousResult() {
* Gets the previous {@link CoberturaBuildAction} of the given build.
*/
/*package*/
static CoberturaBuildAction getPreviousResult(AbstractBuild<?, ?> start) {
AbstractBuild<?, ?> b = start;
static CoberturaBuildAction getPreviousResult(Run<?, ?> start) {
Run<?, ?> b = start;
while (true) {
b = BuildUtils.getPreviousNotFailedCompletedBuild(b);
if (b == null) {
@@ -178,9 +184,10 @@ private boolean includeOnlyStable() {
return onlyStable;
}

CoberturaBuildAction(AbstractBuild<?, ?> owner, CoverageResult r, CoverageTarget healthyTarget,
CoverageTarget unhealthyTarget, boolean onlyStable, boolean failUnhealthy, boolean failUnstable, boolean autoUpdateHealth, boolean autoUpdateStability) {
this.owner = owner;
CoberturaBuildAction(CoverageResult r, CoverageTarget healthyTarget,
CoverageTarget unhealthyTarget, boolean onlyStable, boolean failUnhealthy,
boolean failUnstable, boolean autoUpdateHealth, boolean autoUpdateStability,
boolean zoomCoverageChart, int maxNumberOfBuilds) {
this.report = new WeakReference<CoverageResult>(r);
this.healthyTarget = healthyTarget;
this.unhealthyTarget = unhealthyTarget;
@@ -189,7 +196,8 @@ private boolean includeOnlyStable() {
this.failUnstable = failUnstable;
this.autoUpdateHealth = autoUpdateHealth;
this.autoUpdateStability = autoUpdateStability;
r.setOwner(owner);
this.zoomCoverageChart = zoomCoverageChart;
this.maxNumberOfBuilds = maxNumberOfBuilds;
if (result == null) {
result = new EnumMap<CoverageMetric, Ratio>(CoverageMetric.class);
result.putAll(r.getResults());
@@ -227,9 +235,11 @@ public synchronized CoverageResult getResult() {
}
private static final Logger logger = Logger.getLogger(CoberturaBuildAction.class.getName());

public static CoberturaBuildAction load(AbstractBuild<?, ?> build, CoverageResult result, CoverageTarget healthyTarget,
CoverageTarget unhealthyTarget, boolean onlyStable, boolean failUnhealthy, boolean failUnstable, boolean autoUpdateHealth, boolean autoUpdateStability) {
return new CoberturaBuildAction(build, result, healthyTarget, unhealthyTarget, onlyStable, failUnhealthy, failUnstable, autoUpdateHealth, autoUpdateStability);
public static CoberturaBuildAction load(CoverageResult result, CoverageTarget healthyTarget,
CoverageTarget unhealthyTarget, boolean onlyStable, boolean failUnhealthy, boolean failUnstable,
boolean autoUpdateHealth, boolean autoUpdateStability, boolean zoomCoverageChart, int maxNumberOfBuilds) {
return new CoberturaBuildAction(result, healthyTarget, unhealthyTarget, onlyStable,
failUnhealthy, failUnstable, autoUpdateHealth, autoUpdateStability, zoomCoverageChart, maxNumberOfBuilds);
}

/**
@@ -250,4 +260,27 @@ public void doGraph(StaplerRequest req, StaplerResponse rsp) throws IOException
JFreeChart chart = new CoverageChart(this).createChart();
ChartUtil.generateGraph(req, rsp, chart, 500, 200);
}

public boolean getZoomCoverageChart() {
return zoomCoverageChart;
}

public int getMaxNumberOfBuilds() {
return maxNumberOfBuilds;
}

@Override
public Collection<? extends Action> getProjectActions() {
return Collections.singleton(new CoberturaProjectAction(owner, onlyStable));
}

@Override
public void onAttached(Run<?, ?> r) {
setOwner(r);
}

@Override
public void onLoad(Run<?,?> r) {
setOwner(r);
}
}
@@ -1,6 +1,10 @@
package hudson.plugins.cobertura;

import hudson.model.*;
import hudson.model.Actionable;
import hudson.model.Job;
import hudson.model.ProminentProjectAction;
import hudson.model.Result;
import hudson.model.Run;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;

@@ -13,25 +17,16 @@
*/
public class CoberturaProjectAction extends Actionable implements ProminentProjectAction {

private final AbstractProject<?, ?> project;
private final Run<?, ?> run;
private boolean onlyStable;

public CoberturaProjectAction(AbstractProject<?, ?> project, boolean onlyStable) {
this.project = project;
public CoberturaProjectAction(Run<?, ?> run, boolean onlyStable) {
this.run = run;
this.onlyStable = onlyStable;
}

public CoberturaProjectAction(AbstractProject<?, ?> project) {
this.project = project;

CoberturaPublisher cp = (CoberturaPublisher) project.getPublishersList().get(CoberturaPublisher.DESCRIPTOR);
if (cp != null) {
onlyStable = cp.getOnlyStable();
}
}

public AbstractProject<?, ?> getProject() {
return project;
public CoberturaProjectAction(Run<?, ?> run) {
this(run, false);
}

/**
@@ -61,7 +56,7 @@ public String getUrlName() {
* @return Value for property 'lastResult'.
*/
public CoberturaBuildAction getLastResult() {
for (AbstractBuild<?, ?> b = getLastBuildToBeConsidered(); b != null; b = BuildUtils.getPreviousNotFailedCompletedBuild(b)) {
for (Run<?, ?> b = getLastBuildToBeConsidered(); b != null; b = BuildUtils.getPreviousNotFailedCompletedBuild(b)) {
if (b.getResult() == Result.FAILURE || (b.getResult() != Result.SUCCESS && onlyStable))
continue;
CoberturaBuildAction r = b.getAction(CoberturaBuildAction.class);
@@ -70,16 +65,16 @@ public CoberturaBuildAction getLastResult() {
}
return null;
}
private AbstractBuild<?, ?> getLastBuildToBeConsidered(){
return onlyStable ? project.getLastStableBuild() : project.getLastSuccessfulBuild();
private Run<?, ?> getLastBuildToBeConsidered(){
return onlyStable ? run.getParent().getLastStableBuild() : run.getParent().getLastSuccessfulBuild();
}
/**
* Getter for property 'lastResult'.
*
* @return Value for property 'lastResult'.
*/
public Integer getLastResultBuild() {
for (AbstractBuild<?, ?> b = getLastBuildToBeConsidered(); b != null; b = BuildUtils.getPreviousNotFailedCompletedBuild(b)) {
for (Run<?, ?> b = getLastBuildToBeConsidered(); b != null; b = BuildUtils.getPreviousNotFailedCompletedBuild(b)) {
if (b.getResult() == Result.FAILURE || (b.getResult() != Result.SUCCESS && onlyStable))
continue;
CoberturaBuildAction r = b.getAction(CoberturaBuildAction.class);
Oops, something went wrong.

0 comments on commit 85fd638

Please sign in to comment.
You can’t perform that action at this time.