/
CoverageProcessor.java
672 lines (568 loc) · 26.1 KB
/
CoverageProcessor.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
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
package io.jenkins.plugins.coverage;
import com.google.common.collect.Sets;
import hudson.FilePath;
import hudson.model.*;
import hudson.remoting.VirtualChannel;
import hudson.util.RunList;
import io.jenkins.plugins.coverage.adapter.CoverageReportAdapter;
import io.jenkins.plugins.coverage.adapter.CoverageReportAdapterDescriptor;
import io.jenkins.plugins.coverage.detector.Detectable;
import io.jenkins.plugins.coverage.detector.ReportDetector;
import io.jenkins.plugins.coverage.exception.CoverageException;
import io.jenkins.plugins.coverage.source.SourceFileResolver;
import io.jenkins.plugins.coverage.targets.*;
import io.jenkins.plugins.coverage.threshold.Threshold;
import jenkins.branch.MultiBranchProject;
import jenkins.MasterToSlaveFileCallable;
import jenkins.scm.api.SCMHead;
import jenkins.scm.api.SCMRevision;
import jenkins.scm.api.SCMRevisionAction;
import jenkins.scm.api.mixin.ChangeRequestSCMHead;
import jenkins.scm.api.mixin.ChangeRequestSCMRevision;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.jvnet.localizer.Localizable;
import javax.annotation.Nonnull;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class CoverageProcessor {
private static final String DEFAULT_REPORT_SAVE_NAME = "coverage-report";
private Run<?, ?> run;
private FilePath workspace;
private TaskListener listener;
private boolean failUnhealthy;
private boolean failUnstable;
private boolean failNoReports;
private String globalTag;
private boolean calculateDiffForChangeRequests;
private SourceFileResolver sourceFileResolver;
/**
* @param run a build this is running as a part of
* @param workspace a workspace to use for any file operations
* @param listener a place to send output
*/
public CoverageProcessor(@Nonnull Run<?, ?> run, @Nonnull FilePath workspace, @Nonnull TaskListener listener) {
this.run = run;
this.workspace = workspace;
this.listener = listener;
}
/**
* Convert all reports that are specified by {@link CoverageReportAdapter}s and detected by {@link ReportDetector}s to {@link CoverageResult},
* and generate health report from CoverageResult. Add them to {@link CoverageAction} and add Action to {@link Run}.
*
* @param reportAdapters reportAdapters specified by user
* @param reportDetectors reportDetectors specified by user
* @param globalThresholds global threshold specified by user
*/
public void performCoverageReport(List<CoverageReportAdapter> reportAdapters, List<ReportDetector> reportDetectors, List<Threshold> globalThresholds)
throws IOException, InterruptedException, CoverageException {
Map<CoverageReportAdapter, List<CoverageResult>> results = convertToResults(reportAdapters, reportDetectors);
CoverageResult coverageReport = aggregateReports(results);
if (coverageReport == null) {
return;
}
coverageReport.setOwner(run);
if (sourceFileResolver != null) {
Set<String> possiblePaths = new HashSet<>();
coverageReport.getChildrenReal().forEach((s, coverageResult) -> {
Set<String> paths = coverageResult.getAdditionalProperty(CoverageFeatureConstants.FEATURE_SOURCE_FILE_PATH);
if (paths != null) {
possiblePaths.addAll(paths);
}
});
if (possiblePaths.size() > 0) {
sourceFileResolver.setPossiblePaths(possiblePaths);
}
sourceFileResolver.resolveSourceFiles(run, workspace, listener, coverageReport.getPaintedSources());
}
if (calculateDiffForChangeRequests) {
setDiffInCoverageForChangeRequest(coverageReport);
}
CoverageAction action = convertResultToAction(coverageReport);
HealthReport healthReport = processThresholds(results, globalThresholds, action);
action.setHealthReport(healthReport);
}
private CoverageAction convertResultToAction(CoverageResult coverageReport) throws IOException {
synchronized (CoverageProcessor.class) {
CoverageAction previousAction = run.getAction(CoverageAction.class);
if (previousAction == null) {
saveCoverageResult(run, coverageReport);
CoverageAction action = new CoverageAction(coverageReport);
run.addAction(action);
return action;
} else {
CoverageResult previousResult = previousAction.getResult();
Collection<CoverageResult> previousReports = previousResult.getChildrenReal().values();
for (CoverageResult report : coverageReport.getChildrenReal().values()) {
if (StringUtils.isEmpty(report.getTag())) {
report.resetParent(previousResult);
continue;
}
Optional<CoverageResult> matchedTagReport;
if ((matchedTagReport = previousReports.stream()
.filter(r -> !StringUtils.isEmpty(r.getTag()) && r.getTag().equals(report.getTag()))
.findAny()).isPresent()) {
try {
matchedTagReport.get().merge(report);
} catch (CoverageException e) {
e.printStackTrace();
report.resetParent(previousResult);
}
} else {
report.resetParent(previousResult);
}
}
previousResult.setOwner(run);
saveCoverageResult(run, previousResult);
return previousAction;
}
}
}
private void setDiffInCoverageForChangeRequest(CoverageResult coverageReport) {
// Use limited number of builds to don't search too long
final int numberOfBuildsFromTargetBranch = 50;
// Calculate diff only for line coverage
Ratio changeRequestLinesCoverage = coverageReport.getCoverage(CoverageElement.LINE);
if (changeRequestLinesCoverage == null) {
return;
}
Job job = run.getParent();
ItemGroup parent = job.getParent();
if (!(parent instanceof MultiBranchProject)) {
return;
}
SCMHead currentBuildSCMHead = SCMHead.HeadByItem.findHead(job);
if (!(currentBuildSCMHead instanceof ChangeRequestSCMHead)) {
listener.getLogger().println(
"Current build is not change request build, won't calculate coverage diff");
return;
}
SCMRevisionAction scmRevisionAction = run.getAction(SCMRevisionAction.class);
if (scmRevisionAction == null) {
return;
}
SCMRevision currentBuildSCMRevision = scmRevisionAction.getRevision();
if (!(currentBuildSCMRevision instanceof ChangeRequestSCMRevision)) {
return;
}
// Get target branch revision to compare coverage with
int targetBranchSCMHash = ((ChangeRequestSCMRevision) currentBuildSCMRevision).getTarget().hashCode();
SCMHead targetBranchSCMHead = ((ChangeRequestSCMHead) currentBuildSCMHead).getTarget();
String targetBranchName = targetBranchSCMHead.getName();
listener.getLogger().println(
String.format("Calculating coverage change for change request build is enabled, target branch %s", targetBranchName));
Job targetBranchJob = ((MultiBranchProject) parent).getItemByBranchName(targetBranchName);
if (targetBranchJob == null) {
return;
}
Run buildToTakeCoverageFrom = null;
// Search for a build from the target branch job to compare coverage with
RunList<Run> buildsFromTargetBranchJob = ((RunList<Run>) targetBranchJob.getBuilds()).limit(numberOfBuildsFromTargetBranch);
for (Run targetBranchBuild : buildsFromTargetBranchJob) {
SCMRevisionAction targetBranchBuildRevision = targetBranchBuild.getAction(SCMRevisionAction.class);
if (targetBranchBuildRevision != null) {
if (targetBranchBuildRevision.getRevision().hashCode() == targetBranchSCMHash) {
buildToTakeCoverageFrom = targetBranchBuild;
break;
}
}
}
if (buildToTakeCoverageFrom == null) {
return;
}
CoverageAction targetBranchCoverageAction = buildToTakeCoverageFrom.getAction(CoverageAction.class);
if (targetBranchCoverageAction == null) {
return;
}
CoverageResult targetBranchCoverageResult = targetBranchCoverageAction.getResult();
if (targetBranchCoverageResult == null) {
return;
}
Ratio targetBranchLinesCoverage = targetBranchCoverageResult.getCoverage(CoverageElement.LINE);
if (targetBranchLinesCoverage == null) {
return;
}
float percentageDiff =
changeRequestLinesCoverage.getPercentageFloat() - targetBranchLinesCoverage.getPercentageFloat();
coverageReport.setChangeRequestCoverageDiffWithTargetBranch(percentageDiff);
coverageReport.setLinkToBuildThatWasUsedForComparison(buildToTakeCoverageFrom.getUrl());
}
/**
* Convert reports that are specified by {@link CoverageReportAdapter} and detected by {@link ReportDetector}s to {@link CoverageResult}.
*
* @param adapters {@link CoverageReportAdapter} for each report
* @return {@link CoverageResult} for each report
*/
private Map<CoverageReportAdapter, List<CoverageResult>> convertToResults(List<CoverageReportAdapter> adapters, List<ReportDetector> reportDetectors)
throws IOException, InterruptedException, CoverageException {
PrintStream logger = listener.getLogger();
Map<CoverageReportAdapter, Set<FilePath>> reports = new HashMap<>();
Map<CoverageReportAdapter, List<File>> copiedReport = new HashMap<>();
if (adapters != null) {
// find report according to the path of each adapter
for (CoverageReportAdapter adapter : adapters) {
String path = adapter.getPath();
FilePath[] r = workspace.act(new FindReportCallable(path, adapter));
reports.put(adapter, Sets.newHashSet(r));
}
// copy reports to build dir
File runRootDir = run.getRootDir();
for (Map.Entry<CoverageReportAdapter, Set<FilePath>> adapterReports : reports.entrySet()) {
Set<FilePath> r = adapterReports.getValue();
List<File> copies = new LinkedList<>();
for (FilePath f : r) {
File copy = new File(runRootDir, f.getName());
//if copy exist, it means there have reports have same name.
int i = 1;
while (copy.exists()) {
copy = new File(runRootDir, String.format("%s(%d)", f.getName(), i++));
}
f.copyTo(new FilePath(copy));
copies.add(copy);
}
copiedReport.put(adapterReports.getKey(), copies);
}
}
int detectCount = 0;
if (reportDetectors.size() != 0) {
for (ReportDetector reportDetector : reportDetectors) {
Map<CoverageReportAdapter, List<File>> detectedReportFiles = reportDetector.getReports(run, workspace, listener, filePath -> {
for (Map.Entry<CoverageReportAdapter, Set<FilePath>> entry : reports.entrySet()) {
if (entry.getValue().contains(filePath))
return false;
}
return true;
});
detectCount += detectedReportFiles.values().stream().mapToInt(List::size).sum();
for (Map.Entry<CoverageReportAdapter, List<File>> e : detectedReportFiles.entrySet()) {
if (copiedReport.containsKey(e.getKey())) {
copiedReport.get(e.getKey()).addAll(e.getValue());
} else {
copiedReport.put(e.getKey(), e.getValue());
}
}
}
logger.printf("Auto Detect was ended: Found %d report%n", detectCount);
}
reports.clear();
// convert report to results
Map<CoverageReportAdapter, List<CoverageResult>> results = new HashMap<>();
for (Map.Entry<CoverageReportAdapter, List<File>> adapterReports : copiedReport.entrySet()) {
CoverageReportAdapter adapter = adapterReports.getKey();
CoverageReportAdapterDescriptor descriptor = (CoverageReportAdapterDescriptor) adapter.getDescriptor();
for (File foundedFile : adapterReports.getValue()) {
try {
boolean isValidate;
// If is Detectable, then use detect to validate file, else simply use file length
if (descriptor instanceof Detectable) {
isValidate = ((Detectable) descriptor).detect(foundedFile);
} else {
// skip file if file is empty
isValidate = Files.size(Paths.get(foundedFile.toURI())) > 0;
}
if (isValidate) {
results.putIfAbsent(adapter, new LinkedList<>());
CoverageResult result = adapter.getResult(foundedFile);
if (!StringUtils.isEmpty(globalTag)) {
result.setTag(globalTag);
}
results.get(adapter).add(result);
}
} catch (CoverageException e) {
e.printStackTrace();
logger.printf("report %s for %s has met some errors: %s%n",
foundedFile.getAbsolutePath(),
adapter.getDescriptor().getDisplayName(),
e.getMessage());
} finally {
FileUtils.deleteQuietly(foundedFile);
}
}
if (adapter.isMergeToOneReport()) {
List<CoverageResult> resultOfAdapter = results.get(adapter);
if (resultOfAdapter == null || resultOfAdapter.size() <= 1) {
continue;
}
CoverageResult report = aggregateToOneReport(adapter, resultOfAdapter);
resultOfAdapter.clear();
resultOfAdapter.add(report);
}
}
if (results.size() == 0) {
logger.println("No reports were found");
if (getFailNoReports()) {
throw new CoverageException("Publish Coverage Failed : No Reports were found");
}
} else {
logger.printf("A total of %d reports were found%n",
results.values()
.stream()
.mapToLong(Collection::size)
.sum());
}
return results;
}
/**
* Process threshold and return health report.
*
* @param adapterWithResults Coverage report adapter and its correspond Coverage results.
* @param globalThresholds global threshold
* @param action coverage action
* @return Health report
*/
private HealthReport processThresholds(Map<CoverageReportAdapter, List<CoverageResult>> adapterWithResults,
List<Threshold> globalThresholds, CoverageAction action) throws CoverageException {
int healthyCount = 0;
int unhealthyCount = 0;
int unstableCount = 0;
Set<Threshold> unstableThresholds = new HashSet<>();
Set<Threshold> unhealthyThresholds = new HashSet<>();
LinkedList<CoverageResult> resultTask = new LinkedList<>();
for (Map.Entry<CoverageReportAdapter, List<CoverageResult>> results : adapterWithResults.entrySet()) {
// make local threshold over global threshold
List<Threshold> thresholds = results.getKey().getThresholds();
if (thresholds != null) {
thresholds = new ArrayList<>(thresholds);
for (Threshold t : globalThresholds) {
if (!thresholds.contains(t)) {
thresholds.add(t);
}
}
} else {
thresholds = globalThresholds;
}
for (CoverageResult coverageResult : results.getValue()) {
resultTask.push(coverageResult);
while (!resultTask.isEmpty()) {
CoverageResult r = resultTask.pollFirst();
assert r != null;
resultTask.addAll(r.getChildrenReal().values());
for (Threshold threshold : thresholds) {
Ratio ratio = r.getCoverage(threshold.getThresholdTargetElement());
if (ratio == null) {
continue;
}
float percentage = ratio.getPercentageFloat();
if (percentage < threshold.getUnstableThreshold()) {
unstableCount++;
listener.getLogger().printf("Code coverage enforcement failed: %s coverage in %s level '%s' is lower than %.2f stable threshold\n",
threshold.getThresholdTarget(),
r.getElement().getName(),
r.getName(), threshold.getUnstableThreshold());
unstableThresholds.add(threshold);
} else if (percentage < threshold.getUnhealthyThreshold()) {
unhealthyCount++;
listener.getLogger().printf("Code coverage enforcement failed: %s coverage in %s level '%s' is lower than %.2f healthy threshold\n",
threshold.getThresholdTarget(),
r.getElement().getName(),
r.getName(), threshold.getUnhealthyThreshold());
unhealthyThresholds.add(threshold);
} else {
healthyCount++;
}
}
}
}
}
if (unstableCount > 0) {
if (getFailUnstable()) {
action.setFailMessage(String.format("Build failed because following metrics did not meet stability target: %s.", unstableThresholds.toString()));
throw new CoverageException(action.getFailMessage());
} else {
run.setResult(Result.UNSTABLE);
}
}
if (unhealthyCount > 0) {
if (getFailUnhealthy()) {
action.setFailMessage(String.format("Build failed because following metrics did not meet health target: %s.", unhealthyThresholds.toString()));
throw new CoverageException(action.getFailMessage());
}
unhealthyThresholds = unhealthyThresholds.stream().filter(Threshold::isFailUnhealthy)
.collect(Collectors.toSet());
if (unhealthyThresholds.size() > 0) {
action.setFailMessage(String.format("Build failed because following metrics did not meet health target: %s.", unhealthyThresholds.toString()));
throw new CoverageException(action.getFailMessage());
}
}
int score;
if (healthyCount == 0 && unhealthyCount == 0 && unstableCount == 0) {
score = 100;
} else {
score = healthyCount * 100 / (healthyCount + unhealthyCount + unstableCount);
}
Localizable localizeDescription = Messages._CoverageProcessor_healthReportDescriptionTemplate(score);
return new HealthReport(score, localizeDescription);
}
/**
* aggregate coverage results into one report
*
* @param adapter CoverageAdapter
* @param results CoverageResults converted by adapter
* @return Coverage report that have all coverage results
*/
private CoverageResult aggregateToOneReport(CoverageReportAdapter adapter, List<CoverageResult> results) {
CoverageResult report = new CoverageResult(CoverageElement.REPORT, null, adapter.getDescriptor().getDisplayName() + ": " + adapter.getPath());
results.forEach(r -> {
if (r.getElement().equals(CoverageElement.REPORT)) {
try {
report.merge(r);
} catch (CoverageException e) {
listener.getLogger().printf("Failed to aggregate coverage report %s into one report, reason %s", r.getName(), e.getMessage());
}
} else {
r.resetParent(report);
}
});
return report;
}
/**
* Aggregate results to a aggregated report.
*
* @param results results will be aggregated
* @return aggregated report
*/
private CoverageResult aggregateReports(Map<CoverageReportAdapter, List<CoverageResult>> results) {
if (results.size() == 0) {
return null;
}
CoverageResult report = new CoverageResult(CoverageElement.AGGREGATED_REPORT, null, "All reports");
for (List<CoverageResult> resultList : results.values()) {
for (CoverageResult result : resultList) {
result.addParent(report);
}
}
return report;
}
/**
* Getter for property 'failUnhealthy'
*
* @return value for property 'failUnhealthy'
*/
public boolean getFailUnhealthy() {
return failUnhealthy;
}
/**
* Setter for property 'failUnhealthy'
*
* @param failUnhealthy value to set for property 'failUnhealthy'
*/
public void setFailUnhealthy(boolean failUnhealthy) {
this.failUnhealthy = failUnhealthy;
}
/**
* Getter for property 'failUnstable'
*
* @return value for property 'failUnstable'
*/
public boolean getFailUnstable() {
return failUnstable;
}
/**
* Setter for property 'failUnstable'
*
* @param failUnstable valzue to set for property 'failUnstable'
*/
public void setFailUnstable(boolean failUnstable) {
this.failUnstable = failUnstable;
}
/**
* Getter for property 'failNoReports'
*
* @return value for property 'failNoReports'
*/
public boolean getFailNoReports() {
return failNoReports;
}
/**
* Setter for property 'failNoReports'
*
* @param failNoReports value to set for property 'failNoReports'
*/
public void setFailNoReports(boolean failNoReports) {
this.failNoReports = failNoReports;
}
public void setSourceFileResolver(SourceFileResolver sourceFileResolver) {
this.sourceFileResolver = sourceFileResolver;
}
public String getGlobalTag() {
return globalTag;
}
public void setGlobalTag(String globalTag) {
this.globalTag = globalTag;
}
/**
* Getter for property 'calculateDiffForChangeRequests'
*
* @return value for property 'calculateDiffForChangeRequests'
*/
public boolean getCalculateDiffForChangeRequests() {
return this.calculateDiffForChangeRequests;
}
/**
* Setter for property 'calculateDiffForChangeRequests'
*
* @param calculateDiffForChangeRequests value to set for property 'calculateDiffForChangeRequests'
*/
public void setCalculateDiffForChangeRequests(boolean calculateDiffForChangeRequests) {
this.calculateDiffForChangeRequests = calculateDiffForChangeRequests;
}
private static class FindReportCallable extends MasterToSlaveFileCallable<FilePath[]> {
private final String reportFilePath;
private final CoverageReportAdapter reportAdapter;
public FindReportCallable(String reportFilePath, CoverageReportAdapter reportAdapter) {
this.reportFilePath = reportFilePath;
this.reportAdapter = reportAdapter;
}
@Override
public FilePath[] invoke(File f, VirtualChannel channel) throws IOException, InterruptedException {
FilePath[] r = new FilePath(f).list(reportFilePath);
for (FilePath filePath : r) {
//TODO check report file (it might should be implement by adapter)
}
return r;
}
}
/**
* Save {@link CoverageResult} in build directory.
*
* @param run build
* @param report report
*/
public static void saveCoverageResult(Run<?, ?> run, CoverageResult report) throws IOException {
File reportFile = new File(run.getRootDir(), DEFAULT_REPORT_SAVE_NAME);
try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(reportFile))) {
oos.writeObject(report);
}
}
/**
* Recover {@link CoverageResult} from build directory.
*
* @param run build
* @return Coverage result
*/
public static CoverageResult recoverCoverageResult(Run<?, ?> run) throws IOException, ClassNotFoundException {
File reportFile = new File(run.getRootDir(), DEFAULT_REPORT_SAVE_NAME);
try (ObjectInputStream ois = new CompatibleObjectInputStream(new FileInputStream(reportFile))) {
return (CoverageResult) ois.readObject();
}
}
}