Skip to content

Commit

Permalink
JENKINS-36517: Handle advanced graph layout in pipeline
Browse files Browse the repository at this point in the history
  • Loading branch information
mheinzerling committed Oct 12, 2016
1 parent 14d6f13 commit 2dc0664
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 63 deletions.
29 changes: 24 additions & 5 deletions src/main/java/hudson/plugins/jacoco/JacocoBuildAction.java
@@ -1,5 +1,6 @@
package hudson.plugins.jacoco;

import java.awt.Color;
import java.io.IOException;
import java.io.PrintStream;
import java.io.Serializable;
Expand All @@ -10,7 +11,9 @@
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Nonnull;

import hudson.model.AbstractBuild;
import hudson.model.Action;
import hudson.model.Run;
import hudson.model.TaskListener;
Expand All @@ -20,13 +23,13 @@
import org.jvnet.localizer.Localizable;
import org.kohsuke.stapler.StaplerProxy;

import hudson.model.AbstractBuild;
import hudson.model.HealthReport;
import hudson.model.HealthReportingAction;
import hudson.model.Result;
import hudson.plugins.jacoco.model.Coverage;
import hudson.plugins.jacoco.model.CoverageElement;
import hudson.plugins.jacoco.model.CoverageElement.Type;
import hudson.plugins.jacoco.model.CoverageGraphLayout;
import hudson.plugins.jacoco.model.CoverageObject;
import hudson.plugins.jacoco.report.CoverageReport;

Expand All @@ -50,14 +53,16 @@ public final class JacocoBuildAction extends CoverageObject<JacocoBuildAction> i
private transient WeakReference<CoverageReport> report;
private final String[] inclusions;
private final String[] exclusions;

/**
* The thresholds that applied when this build was built.
* TODO: add ability to trend thresholds on the graph
*/
private final JacocoHealthReportThresholds thresholds;
private transient List<JacocoProjectAction> jacocoProjectActions=Collections.emptyList();

private CoverageGraphLayout coverageGraphLayout;

/**
*
* @param ratios
Expand All @@ -67,7 +72,7 @@ public final class JacocoBuildAction extends CoverageObject<JacocoBuildAction> i
*/
public JacocoBuildAction(
Map<CoverageElement.Type, Coverage> ratios,
JacocoHealthReportThresholds thresholds, TaskListener listener, String[] inclusions, String[] exclusions) {
JacocoHealthReportThresholds thresholds, TaskListener listener, String[] inclusions, String[] exclusions, CoverageGraphLayout coverageGraphLayout) {
logger = listener.getLogger();
if (ratios == null) {
ratios = Collections.emptyMap();
Expand All @@ -81,6 +86,7 @@ public JacocoBuildAction(
this.branch = getOrCreateRatio(ratios, CoverageElement.Type.BRANCH);
this.instruction = getOrCreateRatio(ratios, CoverageElement.Type.INSTRUCTION);
this.complexity = getOrCreateRatio(ratios, CoverageElement.Type.COMPLEXITY);
this.coverageGraphLayout = coverageGraphLayout;
}

private Coverage getOrCreateRatio(Map<CoverageElement.Type, Coverage> ratios, CoverageElement.Type type) {
Expand Down Expand Up @@ -296,12 +302,12 @@ public Map<Coverage,Boolean> getCoverageRatios(){
* @throws IOException
* if failed to parse the file.
*/
public static JacocoBuildAction load(Run<?,?> owner, JacocoHealthReportThresholds thresholds, TaskListener listener, JacocoReportDir layout, String[] includes, String[] excludes) throws IOException {
public static JacocoBuildAction load(Run<?,?> owner, JacocoHealthReportThresholds thresholds, TaskListener listener, JacocoReportDir layout, String[] includes, String[] excludes, @Nonnull CoverageGraphLayout coverageGraphLayout) throws IOException {
//PrintStream logger = listener.getLogger();
Map<CoverageElement.Type,Coverage> ratios = null;

ratios = loadRatios(layout, ratios, includes, excludes);
return new JacocoBuildAction(ratios, thresholds, listener, includes, excludes);
return new JacocoBuildAction(ratios, thresholds, listener, includes, excludes, coverageGraphLayout);
}


Expand Down Expand Up @@ -378,6 +384,19 @@ public void onLoad(Run<?, ?> run) {
setOwner(run);
}

public CoverageGraphLayout getCoverageGraphLayout() {
if (coverageGraphLayout == null) coverageGraphLayout = createDefaultCoverageGraphLayout();
return coverageGraphLayout;
}

private CoverageGraphLayout createDefaultCoverageGraphLayout() {
return new CoverageGraphLayout()
.baseStroke(4f)
.axis()
.plot().type(CoverageGraphLayout.CoverageType.LINE).value(CoverageGraphLayout.CoverageValue.MISSED).color(Color.RED)
.plot().type(CoverageGraphLayout.CoverageType.LINE).value(CoverageGraphLayout.CoverageValue.COVERED).color(Color.GREEN);
}

@Override
public Collection<? extends Action> getProjectActions() {
return jacocoProjectActions;
Expand Down
18 changes: 15 additions & 3 deletions src/main/java/hudson/plugins/jacoco/JacocoPublisher.java
Expand Up @@ -11,6 +11,7 @@
import hudson.model.AbstractProject;
import hudson.model.Run;
import hudson.model.TaskListener;
import hudson.plugins.jacoco.model.CoverageGraphLayout;
import hudson.plugins.jacoco.report.CoverageReport;
import hudson.remoting.VirtualChannel;
import hudson.tasks.BuildStepDescriptor;
Expand Down Expand Up @@ -83,6 +84,7 @@ public class JacocoPublisher extends Recorder implements SimpleBuildStep {
private String maximumMethodCoverage;
private String maximumClassCoverage;
private boolean changeBuildStatus;
private CoverageGraphLayout coverageGraphLayout;

private static final String DIR_SEP = "\\s*,\\s*";

Expand All @@ -106,6 +108,7 @@ public JacocoPublisher() {
this.maximumMethodCoverage = "0";
this.maximumClassCoverage = "0";
this.changeBuildStatus = false;
this.coverageGraphLayout = null;
}

/**
Expand Down Expand Up @@ -267,10 +270,14 @@ public String getMaximumClassCoverage() {
public boolean isChangeBuildStatus() {
return changeBuildStatus;
}

public boolean getChangeBuildStatus() {
return changeBuildStatus;
}
public CoverageGraphLayout getCoverageGraphLayout() {
return coverageGraphLayout;
}

@DataBoundSetter
public void setExecPattern(String execPattern) {
this.execPattern = execPattern;
Expand Down Expand Up @@ -361,7 +368,12 @@ public void setExclusionPattern(String exclusionPattern) {
this.exclusionPattern = exclusionPattern;
}

protected static void saveCoverageReports(FilePath destFolder, FilePath sourceFolder) throws IOException, InterruptedException {
@DataBoundSetter
public void setCoverageGraphLayout(CoverageGraphLayout coverageGraphLayout) {
this.coverageGraphLayout = coverageGraphLayout;
}

protected static void saveCoverageReports(FilePath destFolder, FilePath sourceFolder) throws IOException, InterruptedException {
destFolder.mkdirs();

sourceFolder.copyRecursiveTo(destFolder);
Expand Down Expand Up @@ -473,7 +485,7 @@ public void perform(@Nonnull Run<?, ?> run, @Nonnull FilePath filePath, @Nonnull
logger.println("[JaCoCo plugin] exclusions: " + Arrays.toString(excludes));
}

final JacocoBuildAction action = JacocoBuildAction.load(run, healthReports, taskListener, dir, includes, excludes);
final JacocoBuildAction action = JacocoBuildAction.load(run, healthReports, taskListener, dir, includes, excludes, getCoverageGraphLayout());
action.getThresholds().ensureValid();
logger.println("[JaCoCo plugin] Thresholds: " + action.getThresholds());
run.addAction(action);
Expand Down
17 changes: 12 additions & 5 deletions src/main/java/hudson/plugins/jacoco/model/CoverageGraphLayout.java
Expand Up @@ -3,6 +3,7 @@
import hudson.plugins.jacoco.Messages;
import java.awt.BasicStroke;
import java.awt.Color;
import java.io.Serializable;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
Expand All @@ -17,9 +18,11 @@
/**
* @author Martin Heinzerling
*/
public class CoverageGraphLayout
public class CoverageGraphLayout implements Serializable
{
enum CoverageType
static final long serialVersionUID = 1L;

public enum CoverageType
{
INSTRUCTION(Messages.CoverageObject_Legend_Instructions())
{
Expand Down Expand Up @@ -101,7 +104,7 @@ public Number getValue(CoverageObject<?> a, CoverageValue value)

}

enum CoverageValue
public enum CoverageValue
{
MISSED
{
Expand Down Expand Up @@ -154,8 +157,10 @@ public Number getValue(Coverage c)
public abstract Number getValue(Coverage c);
}

static class Axis
static class Axis implements Serializable
{
static final long serialVersionUID = 1L;

private String label = null;
private int crop = -1;
private boolean skipZero = false;
Expand All @@ -181,8 +186,10 @@ public int getCrop()
}
}

static class Plot
static class Plot implements Serializable
{
static final long serialVersionUID = 1L;

private CoverageValue value;
private CoverageType type;
private Axis axis;
Expand Down
21 changes: 6 additions & 15 deletions src/main/java/hudson/plugins/jacoco/model/CoverageObject.java
@@ -1,22 +1,19 @@
package hudson.plugins.jacoco.model;

import hudson.Util;
import hudson.model.AbstractBuild;
import hudson.model.Api;
import hudson.model.Run;
import hudson.plugins.jacoco.Messages;
import hudson.plugins.jacoco.JacocoBuildAction;
import hudson.plugins.jacoco.Rule;
import hudson.plugins.jacoco.model.CoverageGraphLayout.Axis;
import hudson.plugins.jacoco.model.CoverageGraphLayout.CoverageType;
import hudson.plugins.jacoco.model.CoverageGraphLayout.CoverageValue;
import hudson.plugins.jacoco.model.CoverageGraphLayout.Plot;
import hudson.plugins.jacoco.report.AggregatedReport;
import hudson.util.ChartUtil;
import hudson.util.ChartUtil.NumberOnlyBuildLabel;
import hudson.util.DataSetBuilder;
import hudson.util.Graph;
import hudson.util.ShiftedCategoryAxis;
import java.awt.Color;

import java.io.IOException;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
Expand All @@ -29,6 +26,9 @@
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;

import javax.annotation.Nonnull;

import org.jacoco.core.analysis.ICoverageNode;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
Expand Down Expand Up @@ -62,8 +62,6 @@ public abstract class CoverageObject<SELF extends CoverageObject<SELF>> {
public Coverage instruction = new Coverage();
public Coverage branch = new Coverage();



/**
* Variables used to store which child has to highest coverage for each coverage type.
*/
Expand All @@ -76,7 +74,6 @@ public abstract class CoverageObject<SELF extends CoverageObject<SELF>> {

private volatile boolean failed = false;


/**
* @return the maxClazz
*/
Expand Down Expand Up @@ -376,13 +373,7 @@ public void doGraph(StaplerRequest req, StaplerResponse rsp) throws IOException
int width = (w != null) ? Integer.valueOf(w) : 500;
int height = (h != null) ? Integer.valueOf(h) : 200;

CoverageGraphLayout layout = new CoverageGraphLayout()
.baseStroke(4f)
.axis()
.plot().type(CoverageType.LINE).value(CoverageValue.MISSED).color(Color.RED)
.plot().type(CoverageType.LINE).value(CoverageValue.COVERED).color(Color.GREEN);

createGraph(t, width, height,layout).doPng(req, rsp);
createGraph(t, width, height, build.getAction(JacocoBuildAction.class).getCoverageGraphLayout()).doPng(req, rsp);
}

GraphImpl createGraph(final Calendar t, final int width, final int height, final CoverageGraphLayout layout) throws IOException
Expand Down
16 changes: 5 additions & 11 deletions src/test/java/hudson/plugins/jacoco/report/AbstractReportTest.java
@@ -1,21 +1,15 @@
package hudson.plugins.jacoco.report;

import static org.junit.Assert.*;
import hudson.console.ConsoleNote;
import hudson.model.BuildListener;
import hudson.model.Result;
import hudson.model.Cause;
import hudson.model.TaskListener;
import hudson.plugins.jacoco.JacocoBuildAction;

import java.io.IOException;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.util.List;

import hudson.plugins.jacoco.model.CoverageGraphLayout;
import hudson.util.StreamTaskListener;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;

public class AbstractReportTest {

Expand All @@ -32,7 +26,7 @@ public void test() throws Exception {

TaskListener taskListener = StreamTaskListener.fromStdout();

JacocoBuildAction action = new JacocoBuildAction(null, null, taskListener, null, null);
JacocoBuildAction action = new JacocoBuildAction(null, null, taskListener, null, null, new CoverageGraphLayout());
report.getParent().getParent().setParent(new CoverageReport(action, null));
assertNull(report.getBuild());

Expand Down
17 changes: 5 additions & 12 deletions src/test/java/hudson/plugins/jacoco/report/CoverageReportTest.java
@@ -1,23 +1,16 @@
package hudson.plugins.jacoco.report;

import static org.junit.Assert.*;
import hudson.console.ConsoleNote;
import hudson.model.BuildListener;
import hudson.model.Result;
import hudson.model.Cause;
import hudson.model.TaskListener;
import hudson.plugins.jacoco.ExecutionFileLoader;
import hudson.plugins.jacoco.JacocoBuildAction;
import hudson.plugins.jacoco.JacocoHealthReportThresholds;

import java.io.IOException;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.util.List;

import hudson.plugins.jacoco.model.CoverageGraphLayout;
import hudson.util.StreamTaskListener;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;

public class CoverageReportTest {
@Test
Expand Down Expand Up @@ -46,5 +39,5 @@ public void testThresholds() throws Exception {
report.setThresholds(new JacocoHealthReportThresholds());
}

private JacocoBuildAction action = new JacocoBuildAction(null, null, StreamTaskListener.fromStdout(), null, null);
private JacocoBuildAction action = new JacocoBuildAction(null, null, StreamTaskListener.fromStdout(), null, null, new CoverageGraphLayout());
}

0 comments on commit 2dc0664

Please sign in to comment.