-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathCoverageUploadMojo.java
More file actions
295 lines (265 loc) · 12 KB
/
CoverageUploadMojo.java
File metadata and controls
295 lines (265 loc) · 12 KB
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
package com.teamscale.maven.upload;
import com.teamscale.client.CommitDescriptor;
import com.teamscale.client.EReportFormat;
import com.teamscale.client.TeamscaleClient;
import com.teamscale.client.TeamscaleServiceGenerator;
import com.teamscale.maven.BuildVersion;
import com.teamscale.maven.DependencyUtils;
import com.teamscale.maven.TeamscaleMojoBase;
import com.teamscale.maven.tia.TestwiseCoverageReportMojo;
import com.teamscale.maven.tia.TiaIntegrationTestMojo;
import com.teamscale.maven.tia.TiaUnitTestMojo;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.annotations.ResolutionScope;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.util.xml.Xpp3Dom;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
/**
* Run this goal after the Jacoco and Testwise Coverage report generation to upload them to a configured Teamscale
* instance. The configuration can be specified in the root Maven project. The goal should only be run in the aggregator
* module.
* <p>
* Offers the following functionality:
* <ol>
* <li>Validate Jacoco Maven plugin configuration</li>
* <li>Locate and upload all Jacoco and Testwise Coverage reports in one session per partition</li>
* </ol>
*
* @see <a href="https://www.jacoco.org/jacoco/trunk/doc/maven.html">Jacoco
* Plugin</a>
*/
@Mojo(name = "upload-coverage", defaultPhase = LifecyclePhase.VERIFY, requiresDependencyResolution = ResolutionScope.RUNTIME,
threadSafe = true)
public class CoverageUploadMojo extends TeamscaleMojoBase {
private static final String JACOCO_PLUGIN_NAME = "org.jacoco:jacoco-maven-plugin";
private static final String COVERAGE_UPLOAD_MESSAGE = "Coverage upload via Teamscale Maven plugin";
/**
* The Teamscale partition name to which unit test reports will be uploaded.
*/
@Parameter(property = "teamscale.unitTestPartition", defaultValue = "Unit Tests")
public String unitTestPartition;
/**
* The Teamscale partition name to which integration test reports will be uploaded.
*/
@Parameter(property = "teamscale.integrationTestPartition", defaultValue = "Integration Tests")
public String integrationTestPartition;
/**
* The Teamscale partition name to which aggregated test reports will be uploaded.
*/
@Parameter(property = "teamscale.aggregatedTestPartition", defaultValue = "Aggregated Tests")
public String aggregatedTestPartition;
/**
* Maven project. Provided automatically by Maven.
*/
@Parameter(property = "project", readonly = true)
MavenProject project;
/**
* The projects in the reactor. Provided automatically by Maven.
*/
@Parameter(property = "reactorProjects", readonly = true)
private List<MavenProject> reactorProjects;
/**
* Paths to all reports generated by subprojects
*
* @see <a href= "https://www.jacoco.org/jacoco/trunk/doc/report-mojo.html">report</a>
*/
private final List<Path> reportGoalOutputFiles = new ArrayList<>();
/**
* Paths to all integration reports generated by subprojects
*
* @see <a href= "https://www.jacoco.org/jacoco/trunk/doc/report-integration-mojo.html">report-integration</a>
*/
private final List<Path> reportIntegrationGoalOutputFiles = new ArrayList<>();
/**
* Paths to all aggregated reports generated by subprojects
*
* @see <a href= "https://www.jacoco.org/jacoco/trunk/doc/report-aggregate-mojo.html">report-aggregate</a>
*/
private final List<Path> reportAggregateGoalOutputFiles = new ArrayList<>();
/**
* Paths to all aggregated Testwise coverage reports generated by this project and subproject's unit tests
*
* @see TestwiseCoverageReportMojo
*/
private final List<Path> tiaUnitGoalOutputFiles = new ArrayList<>();
/**
* Paths to all aggregated Testwise coverage reports generated by this project and subproject's integration tests
*
* @see TestwiseCoverageReportMojo
*/
private final List<Path> tiaIntegrationGoalOutputFiles = new ArrayList<>();
/**
* The Teamscale client that is used to upload reports to a Teamscale instance.
*/
private TeamscaleClient teamscaleClient;
@Override
public void execute() throws MojoFailureException, MojoExecutionException {
super.execute();
if (skip) {
getLog().debug("Skipping since skip is set to true");
return;
}
teamscaleClient = new TeamscaleClient(teamscaleUrl, username, accessToken, projectId,
TeamscaleServiceGenerator.buildUserAgent("Teamscale Maven Plugin", BuildVersion.VERSION));
getLog().debug("Resolving end commit");
resolveCommitOrRevision();
getLog().debug("Parsing Jacoco plugin configurations");
parseJacocoConfiguration();
try {
getLog().debug("Uploading coverage reports");
uploadCoverageReports();
} catch (IOException e) {
throw new MojoFailureException(
"Uploading coverage reports failed. No upload to Teamscale was performed. You can try again or upload the XML coverage reports manually, see https://docs.teamscale.com/reference/ui/project/project/#manual-report-upload",
e);
}
}
/**
* Check that Jacoco is set up correctly and read any custom settings that may have been set
*
* @throws MojoFailureException If Jacoco is not set up correctly
*/
private void parseJacocoConfiguration() throws MojoFailureException {
List<MavenProject> dependantProjects = DependencyUtils.findDependencies(reactorProjects, project,
Artifact.SCOPE_COMPILE, Artifact.SCOPE_RUNTIME, Artifact.SCOPE_PROVIDED);
getLog().debug(String.format("Found %d dependant projects", dependantProjects.size()));
for (MavenProject dependantProject : dependantProjects) {
collectReportFiles(dependantProject);
}
}
private void collectReportFiles(MavenProject subProject) throws MojoFailureException {
collectJaCoCoReportOutputDirectory(subProject, "report", "jacoco", reportGoalOutputFiles);
collectJaCoCoReportOutputDirectory(subProject, "report-integration", "jacoco-it",
reportIntegrationGoalOutputFiles);
collectJaCoCoReportOutputDirectory(subProject, "report-aggregate", "jacoco-aggregate",
reportAggregateGoalOutputFiles);
collectTestwiseCoverageReportOutputDirectory(subProject, TiaUnitTestMojo.OUTPUT_DIR_NAME,
tiaUnitGoalOutputFiles);
collectTestwiseCoverageReportOutputDirectory(subProject, TiaIntegrationTestMojo.OUTPUT_DIR_NAME,
tiaIntegrationGoalOutputFiles);
}
private void collectTestwiseCoverageReportOutputDirectory(MavenProject subProject, String folderName,
List<Path> reportOutputFiles) {
Path reportPath = Paths.get(subProject.getBuild().getDirectory(), folderName, "reports");
File[] files = reportPath.toFile().listFiles(File::isFile);
if (files != null) {
reportOutputFiles.addAll(Arrays.stream(files).map(File::toPath).collect(Collectors.toList()));
}
}
/**
* Collect the file locations in which JaCoCo is configured to save the XML coverage reports
*
* @param project The project
* @param reportGoal The JaCoCo report goal
* @param jacocoDirectory The name of the directory, matching the JaCoCo goal
* @see <a href="https://www.eclemma.org/jacoco/trunk/doc/maven.html">Goals</a>
*/
private void collectJaCoCoReportOutputDirectory(MavenProject project, String reportGoal, String jacocoDirectory,
List<Path> reportOutputFiles) throws MojoFailureException {
Path defaultOutputDirectory = Paths.get(project.getReporting().getOutputDirectory());
// If a Dom is null it means the execution goal uses default parameters which work correctly
Xpp3Dom reportConfigurationDom = getJacocoGoalExecutionConfiguration(project, reportGoal);
String errorMessage = "Skipping upload for %s as %s is not configured to produce XML reports for goal %s. See https://www.jacoco.org/jacoco/trunk/doc/report-mojo.html#formats";
if (!validateReportFormat(reportConfigurationDom)) {
throw new MojoFailureException(
String.format(errorMessage, project.getName(), JACOCO_PLUGIN_NAME, jacocoDirectory));
}
Path resolvedCoverageFile = getCustomOutputDirectory(reportConfigurationDom).orElse(defaultOutputDirectory)
.resolve(jacocoDirectory).resolve("jacoco.xml");
getLog().debug(String.format("Adding possible report location: %s", resolvedCoverageFile));
reportOutputFiles.add(resolvedCoverageFile);
}
private void uploadCoverageReports() throws IOException {
Map<String, Map<String, List<File>>> reportsByFormatByPartition = new HashMap<>();
reportsByFormatByPartition.computeIfAbsent(unitTestPartition, k -> new HashMap<>());
reportsByFormatByPartition.computeIfAbsent(integrationTestPartition, k -> new HashMap<>());
reportsByFormatByPartition.computeIfAbsent(aggregatedTestPartition, k -> new HashMap<>());
insertReports(reportsByFormatByPartition.get(unitTestPartition), EReportFormat.JACOCO,
reportGoalOutputFiles);
insertReports(reportsByFormatByPartition.get(unitTestPartition), EReportFormat.TESTWISE_COVERAGE,
tiaUnitGoalOutputFiles);
insertReports(reportsByFormatByPartition.get(integrationTestPartition), EReportFormat.JACOCO,
reportIntegrationGoalOutputFiles);
insertReports(reportsByFormatByPartition.get(integrationTestPartition), EReportFormat.TESTWISE_COVERAGE,
tiaIntegrationGoalOutputFiles);
insertReports(reportsByFormatByPartition.get(aggregatedTestPartition), EReportFormat.JACOCO,
reportAggregateGoalOutputFiles);
for (Map.Entry<String, Map<String, List<File>>> entry : reportsByFormatByPartition.entrySet()) {
uploadCoverage(entry.getKey(), entry.getValue());
}
}
private void insertReports(Map<String, List<File>> reportsByFormat, EReportFormat format,
List<Path> reportOutputFiles) {
List<File> reports = new ArrayList<>();
getLog().debug(String.format("Scanning through %d locations...", reportOutputFiles.size()));
for (Path reportPath : reportOutputFiles) {
File report = reportPath.toFile();
if (!report.exists()) {
getLog().debug(String.format("Cannot find %s, skipping...", report.getAbsolutePath()));
continue;
}
if (!report.canRead()) {
getLog().warn(String.format("Cannot read %s, skipping!", report.getAbsolutePath()));
continue;
}
reports.add(report);
}
if (!reports.isEmpty()) {
reportsByFormat.computeIfAbsent(format.name(), k -> new ArrayList<>()).addAll(reports);
}
}
private void uploadCoverage(String partition, Map<String, List<File>> reportsByFormat)
throws IOException {
if (reportsByFormat.isEmpty()) {
getLog().info(String.format("Found no valid reports for %s", partition));
return;
}
getLog().info(
String.format("Uploading %d report for project %s to %s", reportsByFormat.values().stream().mapToLong(
Collection::size).sum(), projectId, partition));
teamscaleClient.uploadReports(reportsByFormat, CommitDescriptor.parse(resolvedCommit), resolvedRevision,
repository,
partition, COVERAGE_UPLOAD_MESSAGE);
}
/**
* Validates that a configuration Dom is set up to generate XML reports
*
* @param configurationDom The configuration Dom of a goal execution
*/
private boolean validateReportFormat(Xpp3Dom configurationDom) {
if (configurationDom == null || configurationDom.getChild("formats") == null) {
return true;
}
for (Xpp3Dom format : configurationDom.getChild("formats").getChildren()) {
if (format.getValue().equals("XML")) {
return true;
}
}
return false;
}
private Optional<Path> getCustomOutputDirectory(Xpp3Dom configurationDom) {
if (configurationDom != null && configurationDom.getChild("outputDirectory") != null) {
return Optional.of(Paths.get(configurationDom.getChild("outputDirectory").getValue()));
}
return Optional.empty();
}
private Xpp3Dom getJacocoGoalExecutionConfiguration(MavenProject project, String pluginGoal) {
return super.getExecutionConfigurationDom(project, JACOCO_PLUGIN_NAME, pluginGoal);
}
}