Skip to content

Commit

Permalink
Add color configuration of trend chart (show passed tests in green or…
Browse files Browse the repository at this point in the history
… blue ) (#286)
  • Loading branch information
uhafner authored May 2, 2022
1 parent a244e69 commit 3963462
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 21 deletions.
14 changes: 9 additions & 5 deletions src/main/java/hudson/tasks/test/TestResultProjectAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import hudson.model.Job;
import hudson.model.Run;
import hudson.tasks.junit.JUnitResultArchiver;
import hudson.tasks.test.TestResultTrendChart.PassedColor;

import io.jenkins.plugins.echarts.AsyncConfigurableTrendChart;
import io.jenkins.plugins.echarts.AsyncTrendChart;
Expand All @@ -60,6 +61,8 @@
* @author Kohsuke Kawaguchi
*/
public class TestResultProjectAction implements Action, AsyncTrendChart, AsyncConfigurableTrendChart {
private static final JacksonFacade JACKSON_FACADE = new JacksonFacade();

/**
* Project that owns this action.
* @since 1.2-beta-1
Expand Down Expand Up @@ -119,23 +122,23 @@ public AbstractTestResultAction getLastTestResultAction() {

@Deprecated
protected LinesChartModel createChartModel() {
return createChartModel(new ChartModelConfiguration());
return createChartModel(new ChartModelConfiguration(), PassedColor.BLUE);
}

private LinesChartModel createChartModel(final ChartModelConfiguration configuration) {
private LinesChartModel createChartModel(ChartModelConfiguration configuration, PassedColor passedColor) {
Run<?, ?> lastCompletedBuild = job.getLastCompletedBuild();

JunitTestResultStorage storage = JunitTestResultStorage.find();
if (!(storage instanceof FileJunitTestResultStorage)) {
TestResultImpl pluggableStorage = storage.load(lastCompletedBuild.getParent().getFullName(), lastCompletedBuild.getNumber());
return new TestResultTrendChart().create(pluggableStorage.getTrendTestResultSummary());
return new TestResultTrendChart().create(pluggableStorage.getTrendTestResultSummary(), passedColor);
}

TestResultActionIterable buildHistory = createBuildHistory(lastCompletedBuild);
if (buildHistory == null) {
return new LinesChartModel();
}
return new TestResultTrendChart().create(buildHistory, configuration);
return new TestResultTrendChart().create(buildHistory, configuration, passedColor);
}

@CheckForNull
Expand Down Expand Up @@ -227,7 +230,8 @@ public String getBuildTrendModel() {
@JavaScriptMethod
@Override
public String getConfigurableBuildTrendModel(final String configuration) {
return new JacksonFacade().toJson(createChartModel(ChartModelConfiguration.fromJson(configuration)));
PassedColor useBlue = JACKSON_FACADE.getBoolean(configuration, "useBlue", false) ? PassedColor.BLUE : PassedColor.GREEN;
return new JacksonFacade().toJson(createChartModel(ChartModelConfiguration.fromJson(configuration), useBlue));
}

@Override
Expand Down
42 changes: 29 additions & 13 deletions src/main/java/hudson/tasks/test/TestResultTrendChart.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,46 +14,62 @@
import static hudson.tasks.test.TestResultTrendSeriesBuilder.*;

public class TestResultTrendChart {
enum PassedColor {GREEN, BLUE}

public LinesChartModel create(final List<TrendTestResultSummary> results) {
return create(results, PassedColor.BLUE);
}

public LinesChartModel create(final List<TrendTestResultSummary> results, final PassedColor passedColor) {
LinesDataSet dataset = new LinesDataSet();
results.forEach(result -> dataset.add(result.getDisplayName(), result.toMap(), result.getBuildNumber()));

return getLinesChartModel(dataset);
return getLinesChartModel(dataset, passedColor);
}

public LinesChartModel create(@NonNull final Iterable results,
final ChartModelConfiguration configuration) {
public LinesChartModel create(@NonNull final Iterable results, final ChartModelConfiguration configuration) {
return create(results, configuration, PassedColor.GREEN);
}

public LinesChartModel create(@NonNull final Iterable results, final ChartModelConfiguration configuration,
final PassedColor passedColor) {
TestResultTrendSeriesBuilder builder = new TestResultTrendSeriesBuilder();
LinesDataSet dataSet = builder.createDataSet(configuration, results);

return getLinesChartModel(dataSet);
return getLinesChartModel(dataSet, passedColor);
}

public LinesChartModel createFromTestObject(final Iterable results,
final ChartModelConfiguration configuration) {
final ChartModelConfiguration configuration) {
return createFromTestObject(results, configuration, PassedColor.GREEN);
}

public LinesChartModel createFromTestObject(final Iterable results, final ChartModelConfiguration configuration,
final PassedColor passedColor) {
TestObjectTrendSeriesBuilder builder = new TestObjectTrendSeriesBuilder();
LinesDataSet dataSet = builder.createDataSet(configuration, results);

return getLinesChartModel(dataSet);
return getLinesChartModel(dataSet, passedColor);
}

private LinesChartModel getLinesChartModel(final LinesDataSet dataSet) {
private LinesChartModel getLinesChartModel(final LinesDataSet dataSet, final PassedColor passedColor) {
LinesChartModel model = new LinesChartModel(dataSet);
LineSeries failed = new LineSeries("Failed", Palette.RED.getNormal(),

LineSeries passed = new LineSeries("Passed",
passedColor == PassedColor.BLUE ? Palette.BLUE.getNormal() : Palette.GREEN.getNormal(),
LineSeries.StackedMode.STACKED, LineSeries.FilledMode.FILLED);
failed.addAll(dataSet.getSeries(FAILED_KEY));
model.addSeries(failed);
passed.addAll(dataSet.getSeries(PASSED_KEY));
model.addSeries(passed);

LineSeries skipped = new LineSeries("Skipped", Palette.GRAY.getNormal(),
LineSeries.StackedMode.STACKED, LineSeries.FilledMode.FILLED);
skipped.addAll(dataSet.getSeries(SKIPPED_KEY));
model.addSeries(skipped);

LineSeries passed = new LineSeries("Passed", Palette.BLUE.getNormal(),
LineSeries failed = new LineSeries("Failed", Palette.RED.getNormal(),
LineSeries.StackedMode.STACKED, LineSeries.FilledMode.FILLED);
passed.addAll(dataSet.getSeries(PASSED_KEY));
model.addSeries(passed);
failed.addAll(dataSet.getSeries(FAILED_KEY));
model.addSeries(failed);

return model;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,36 @@ THE SOFTWARE.
-->

<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core"
xmlns:c="/charts">
<c:trend-chart it="${from}" title="${%Test Result Trend}" enableLinks="true" configurationId="default"/>
<j:jelly xmlns:j="jelly:core" xmlns:c="/charts">
<c:trend-setup suffix="junit">
<div class="mb-3">
<input class="form-check-input" type="checkbox" value="" id="junit-use-blue" />
<label class="form-check-label" for="junit-use-blue">
Show passed tests in blue (rather than in green)
</label>
</div>
<div class="mb-3">
<label class="form-label fw-bold">
General configuration
</label>
</div>
</c:trend-setup>

<c:trend-chart it="${from}" title="${%Test Result Trend}" enableLinks="true" configurationId="junit"/>

<script>
function fillJunit(trendConfiguration, jsonConfiguration) {
const useBlue = jsonConfiguration['useBlue'];
trendConfiguration.find('#junit-use-blue').prop('checked', !!useBlue);
}

function saveJunit(trendConfiguration) {
return {
'useBlue': trendConfiguration.find('#junit-use-blue').is(':checked')
};
}

echartsJenkinsApi.configureTrend('junit', fillJunit, saveJunit)
</script>

</j:jelly>

0 comments on commit 3963462

Please sign in to comment.