|
| 1 | +package io.jenkins.plugins.coverage; |
| 2 | + |
| 3 | +import com.google.gson.JsonArray; |
| 4 | +import com.google.gson.JsonObject; |
| 5 | +import hudson.Extension; |
| 6 | +import hudson.model.Run; |
| 7 | +import io.jenkins.plugins.coverage.targets.CoverageElement; |
| 8 | +import io.jenkins.plugins.coverage.targets.Ratio; |
| 9 | +import io.jenkins.plugins.monitoring.MonitorPortlet; |
| 10 | +import io.jenkins.plugins.monitoring.MonitorPortletFactory; |
| 11 | + |
| 12 | +import java.util.*; |
| 13 | + |
| 14 | +/** |
| 15 | + * A portlet that can be used for the |
| 16 | + * <a href="https://github.com/jenkinsci/pull-request-monitoring-plugin">pull-request-monitoring</a> dashboard. |
| 17 | + * |
| 18 | + * It renders the aggregated line and conditional coverage in a stacked bar chart and displays the delta, |
| 19 | + * if a reference build is found. |
| 20 | + * |
| 21 | + * @author Simon Symhoven |
| 22 | + */ |
| 23 | +public class CoveragePullRequestMonitoringPortlet extends MonitorPortlet { |
| 24 | + private final CoverageAction action; |
| 25 | + |
| 26 | + /** |
| 27 | + * Creates a new {@link CoveragePullRequestMonitoringPortlet}. |
| 28 | + * |
| 29 | + * @param action |
| 30 | + * the {@link CoverageAction} of corresponding run. |
| 31 | + */ |
| 32 | + public CoveragePullRequestMonitoringPortlet(final CoverageAction action) { |
| 33 | + super(); |
| 34 | + this.action = action; |
| 35 | + } |
| 36 | + |
| 37 | + @Override |
| 38 | + public String getTitle() { |
| 39 | + return action.getDisplayName(); |
| 40 | + } |
| 41 | + |
| 42 | + @Override |
| 43 | + public String getId() { |
| 44 | + return "code-coverage"; |
| 45 | + } |
| 46 | + |
| 47 | + @Override |
| 48 | + public boolean isDefault() { |
| 49 | + return true; |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public int getPreferredWidth() { |
| 54 | + return 600; |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + public int getPreferredHeight() { |
| 59 | + return 350; |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + public Optional<String> getIconUrl() { |
| 64 | + return Optional.of("/images/48x48/graph.png"); |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public Optional<String> getDetailViewUrl() { |
| 69 | + return Optional.ofNullable(action.getUrlName()); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Get the json data for the stacked bar chart. (used by jelly view) |
| 74 | + * |
| 75 | + * @return |
| 76 | + * the data as json string. |
| 77 | + */ |
| 78 | + public String getCoverageResultsAsJsonModel() { |
| 79 | + Ratio line = action.getResult().getResults().get(CoverageElement.LINE); |
| 80 | + Ratio conditional = action.getResult().getResults().get(CoverageElement.CONDITIONAL); |
| 81 | + |
| 82 | + JsonObject data = new JsonObject(); |
| 83 | + |
| 84 | + JsonArray metrics = new JsonArray(); |
| 85 | + metrics.add(CoverageElement.LINE.getName()); |
| 86 | + metrics.add(CoverageElement.CONDITIONAL.getName()); |
| 87 | + data.add("metrics", metrics); |
| 88 | + |
| 89 | + JsonArray covered = new JsonArray(); |
| 90 | + covered.add(line.numerator); |
| 91 | + covered.add(conditional.numerator); |
| 92 | + data.add("covered", covered); |
| 93 | + |
| 94 | + JsonArray missed = new JsonArray(); |
| 95 | + missed.add(line.denominator - line.numerator); |
| 96 | + missed.add(conditional.denominator - conditional.numerator); |
| 97 | + data.add("missed", missed); |
| 98 | + |
| 99 | + JsonArray coveredPercentage = new JsonArray(); |
| 100 | + coveredPercentage.add(line.denominator == 0 ? 0 : (double) (100 * (covered.get(0).getAsInt() / line.denominator))); |
| 101 | + coveredPercentage.add(conditional.denominator == 0 ? 0 : (double) (100 * (covered.get(1).getAsInt() / conditional.denominator))); |
| 102 | + data.add("coveredPercentage", coveredPercentage); |
| 103 | + |
| 104 | + JsonArray missedPercentage = new JsonArray(); |
| 105 | + missedPercentage.add(100 - coveredPercentage.get(0).getAsDouble()); |
| 106 | + missedPercentage.add(100 - coveredPercentage.get(1).getAsDouble()); |
| 107 | + data.add("missedPercentage", missedPercentage); |
| 108 | + |
| 109 | + String deltaLineLabel = getReferenceBuildUrl().isPresent() |
| 110 | + ? String.format("%.2f%% (%s %.2f%%)", coveredPercentage.get(0).getAsDouble(), (char) 0x0394, |
| 111 | + action.getResult().getCoverageDelta(CoverageElement.LINE)) |
| 112 | + : String.format("%.2f%% (%s unknown)", coveredPercentage.get(0).getAsDouble(), (char) 0x0394); |
| 113 | + |
| 114 | + String deltaConditionalLabel = getReferenceBuildUrl().isPresent() |
| 115 | + ? String.format("%.2f%% (%s %.2f%%)", coveredPercentage.get(1).getAsDouble(), (char) 0x0394, |
| 116 | + action.getResult().getCoverageDelta(CoverageElement.CONDITIONAL)) |
| 117 | + : String.format("%.2f%% (%s unknown)", coveredPercentage.get(1).getAsDouble(), (char) 0x0394); |
| 118 | + |
| 119 | + JsonArray coveredPercentageLabels = new JsonArray(); |
| 120 | + coveredPercentageLabels.add(deltaLineLabel); |
| 121 | + coveredPercentageLabels.add(deltaConditionalLabel); |
| 122 | + data.add("coveredPercentageLabels", coveredPercentageLabels); |
| 123 | + |
| 124 | + return data.toString(); |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * Get the link to the build, that was used to compare the result with. |
| 129 | + * |
| 130 | + * @return |
| 131 | + * optional of the link to the build or empty optional. |
| 132 | + */ |
| 133 | + public Optional<String> getReferenceBuildUrl() { |
| 134 | + return Optional.ofNullable(action.getResult().getReferenceBuildUrl()); |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * The factory for the {@link CoveragePullRequestMonitoringPortlet}. |
| 139 | + */ |
| 140 | + @Extension(optional = true) |
| 141 | + public static class PortletFactory extends MonitorPortletFactory { |
| 142 | + |
| 143 | + @Override |
| 144 | + public Collection<MonitorPortlet> getPortlets(Run<?, ?> build) { |
| 145 | + CoverageAction action = build.getAction(CoverageAction.class); |
| 146 | + |
| 147 | + if (action == null) { |
| 148 | + return Collections.emptyList(); |
| 149 | + } |
| 150 | + |
| 151 | + return Collections.singleton(new CoveragePullRequestMonitoringPortlet(action)); |
| 152 | + } |
| 153 | + |
| 154 | + @Override |
| 155 | + public String getDisplayName() { |
| 156 | + return "Code Coverage API"; |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | +} |
0 commit comments