-
Notifications
You must be signed in to change notification settings - Fork 77
/
CoverageChecksPublisher.java
217 lines (182 loc) · 8.79 KB
/
CoverageChecksPublisher.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package io.jenkins.plugins.coverage;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import edu.hm.hafner.util.VisibleForTesting;
import hudson.model.Run;
import hudson.model.TaskListener;
import io.jenkins.plugins.checks.api.ChecksConclusion;
import io.jenkins.plugins.checks.api.ChecksDetails;
import io.jenkins.plugins.checks.api.ChecksDetails.ChecksDetailsBuilder;
import io.jenkins.plugins.checks.api.ChecksOutput;
import io.jenkins.plugins.checks.api.ChecksOutput.ChecksOutputBuilder;
import io.jenkins.plugins.checks.api.ChecksPublisher;
import io.jenkins.plugins.checks.api.ChecksPublisherFactory;
import io.jenkins.plugins.checks.api.ChecksStatus;
import io.jenkins.plugins.coverage.targets.CoverageElement;
import io.jenkins.plugins.coverage.targets.CoverageResult;
import io.jenkins.plugins.coverage.targets.Ratio;
import io.jenkins.plugins.util.JenkinsFacade;
/**
* Publishes coverage as checks to scm platforms.
*
* @author Kezhi Xiong
*/
class CoverageChecksPublisher {
private final CoverageAction action;
private final JenkinsFacade jenkinsFacade;
private static final List<String> COVERAGE_TYPES =
Arrays.asList("Report", "Group", "Package", "File", "Class", "Method", "Conditional", "Line", "Instruction");
CoverageChecksPublisher(final CoverageAction action) {
this(action, new JenkinsFacade());
}
@VisibleForTesting
CoverageChecksPublisher(final CoverageAction action, final JenkinsFacade jenkinsFacade) {
this.jenkinsFacade = jenkinsFacade;
this.action = action;
}
void publishChecks(final TaskListener listener) {
ChecksPublisher publisher = ChecksPublisherFactory.fromRun(action.getOwner(), listener);
publisher.publish(extractChecksDetails());
}
@VisibleForTesting
ChecksDetails extractChecksDetails() {
CoverageResult result = action.getResult();
ChecksOutput output = new ChecksOutputBuilder()
.withTitle(extractChecksTitle(result))
.withSummary(extractComparedBuildsSummary(result) + extractHealthSummary(action))
.withText(extractChecksText(result))
.build();
return new ChecksDetailsBuilder()
.withName("Code Coverage")
.withStatus(ChecksStatus.COMPLETED)
.withConclusion(StringUtils.isBlank(action.getFailMessage()) ? ChecksConclusion.SUCCESS : ChecksConclusion.FAILURE)
.withDetailsURL(jenkinsFacade.getAbsoluteUrl(result.getOwner().getUrl(), action.getUrlName()))
.withOutput(output)
.build();
}
private String extractChecksText(final CoverageResult result) {
Map<String, Float> ratios = convertRatios(result.getResults());
Map<String, Float> lastRatios = convertRatios(getLastRatios(result));
List<String> containedTypes = new ArrayList<>(COVERAGE_TYPES.size());
StringBuilder coverages = new StringBuilder("|:white_check_mark: **Coverage**|");
StringBuilder trends = new StringBuilder("|:chart_with_upwards_trend: **Trend**|");
for (String singleType : COVERAGE_TYPES) {
if (ratios.containsKey(singleType)) {
containedTypes.add(singleType);
float percentage = ratios.get(singleType);
coverages.append(String.format("%.2f%%|", percentage));
if (lastRatios.containsKey(singleType)) {
float diff = percentage - lastRatios.get(singleType);
trends.append(String.format("%+.2f%%", diff));
if (Float.compare(diff, 0) > 0) {
trends.append(" :arrow_up:|");
} else if (Float.compare(diff, 0) < 0) {
trends.append(" :arrow_down:|");
} else {
trends.append(" :arrow_right:|");
}
} else {
trends.append("-|");
}
}
}
return "||" + String.join("|", containedTypes) + "|\n"
+ "|" + String.join("", Collections.nCopies(containedTypes.size() + 1, ":-:|")) + "\n"
+ coverages + "\n"
+ trends;
}
private String extractChecksTitle(final CoverageResult result) {
StringBuilder title = new StringBuilder();
Map<CoverageElement, Ratio> lastRatios = getLastRatios(result);
if (result.getCoverage(CoverageElement.LINE) != null) {
float lineCoverage = result.getCoverage(CoverageElement.LINE).getPercentageFloat();
if (result.getReferenceBuildUrl() != null) {
title.append(extractChecksTitle("Line", "target branch", lineCoverage,
result.getCoverageDelta(CoverageElement.LINE)));
} else if (lastRatios.containsKey(CoverageElement.LINE)) {
title.append(extractChecksTitle("Line", "last successful build", lineCoverage,
lineCoverage - lastRatios.get(CoverageElement.LINE).getPercentageFloat()));
} else {
title.append(extractChecksTitle("Line", "", lineCoverage, 0));
}
title.append(" ");
}
if (result.getCoverage(CoverageElement.CONDITIONAL) != null) {
float branchCoverage = result.getCoverage(CoverageElement.CONDITIONAL).getPercentageFloat();
if (result.getReferenceBuildUrl() != null) {
title.append(extractChecksTitle("Branch", "target branch", branchCoverage,
result.getCoverageDelta(CoverageElement.CONDITIONAL)));
} else if (lastRatios.containsKey(CoverageElement.CONDITIONAL)) {
title.append(extractChecksTitle("Branch", "last successful build", branchCoverage,
branchCoverage - lastRatios.get(CoverageElement.CONDITIONAL).getPercentageFloat()));
} else {
title.append(extractChecksTitle("Branch", "", branchCoverage, 0));
}
}
if (title.length() == 0) {
title.append("No line or branch coverage has been computed.");
}
return title.toString();
}
private String extractChecksTitle(final String elementName, final String targetBuildName,
final float coverage, final float coverageDiff) {
StringBuilder title = new StringBuilder()
.append(elementName)
.append(String.format(": %.2f", coverage))
.append("%");
if (StringUtils.isBlank(targetBuildName)) {
title.append(".");
} else {
title.append(" (")
.append(String.format("%+.2f%%", coverageDiff))
.append(" against ")
.append(targetBuildName)
.append(").");
}
return title.toString();
}
private String extractComparedBuildsSummary(final CoverageResult result) {
StringBuilder summary = new StringBuilder();
if (result.getReferenceBuildUrl() != null) {
summary.append("* ### [Target branch build](")
.append(jenkinsFacade.getAbsoluteUrl(result.getReferenceBuildUrl()))
.append(")\n");
}
Run<?, ?> lastSuccessfulBuild = result.getOwner().getPreviousSuccessfulBuild();
if (lastSuccessfulBuild != null) {
summary.append("* ### [Last successful build](")
.append(jenkinsFacade.getAbsoluteUrl(lastSuccessfulBuild.getUrl()))
.append(")\n");
}
return summary.toString();
}
private String extractHealthSummary(final CoverageAction action) {
StringBuilder summary = new StringBuilder("## ")
.append(action.getHealthReport().getLocalizableDescription().toString())
.append(".");
if (!StringUtils.isBlank(action.getFailMessage())) {
summary.append("\n## ")
.append(action.getFailMessage());
}
return summary.toString();
}
private Map<CoverageElement, Ratio> getLastRatios(final CoverageResult result) {
CoverageResult previousResult = result.getPreviousResult();
if (previousResult == null) {
return Collections.emptyMap();
}
return previousResult.getResults();
}
private Map<String, Float> convertRatios(final Map<CoverageElement, Ratio> ratios) {
Map<String, Float> converted = new HashMap<String, Float>(ratios.size());
for (Map.Entry<CoverageElement, Ratio> singleRatio : ratios.entrySet()) {
converted.put(singleRatio.getKey().getName(), singleRatio.getValue().getPercentageFloat());
}
return converted;
}
}