-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathhealth.dart
601 lines (512 loc) · 18.4 KB
/
health.dart
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
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'dart:convert';
import 'dart:io';
import 'dart:math';
import 'package:collection/collection.dart';
import 'package:glob/glob.dart';
import 'package:path/path.dart' as path;
import 'package:pub_semver/pub_semver.dart';
import '../../firehose.dart';
import '../utils.dart';
import 'changelog.dart';
import 'coverage.dart';
import 'license.dart';
const apiToolHash = '7b3c2b829b16a523c0d0cc50ce9ff076f41de243';
enum Check {
license('License Headers', 'license'),
changelog('Changelog Entry', 'changelog'),
coverage('Coverage', 'coverage'),
breaking('Breaking changes', 'breaking'),
leaking('API leaks', 'leaking'),
donotsubmit('Do Not Submit', 'do-not-submit');
final String tag;
final String displayName;
const Check(this.tag, this.displayName);
}
class Health {
final Directory directory;
final String commentPath;
Health(
this.directory,
this.check,
this.warnOn,
this.failOn,
this.coverageweb,
List<String> ignoredPackages,
Map<Check, List<String>> ignoredFor,
this.experiments,
this.github,
List<String> flutterPackages, {
Directory? base,
String? comment,
this.log = printLogger,
}) : ignoredPackages = toGlobs(ignoredPackages),
flutterPackageGlobs = toGlobs(flutterPackages),
ignoredFor =
ignoredFor.map((c, globString) => MapEntry(c, toGlobs(globString))),
baseDirectory = base ?? Directory('../base_repo'),
commentPath = comment ??
path.join(
directory.path,
'output',
'comment.md',
) {
flutterExecutable =
(Process.runSync('which', ['-a', 'flutter']).stdout as String)
.split('\n')
.where((element) => element.isNotEmpty)
.firstOrNull;
var dartExecutables =
(Process.runSync('which', ['-a', 'dart']).stdout as String)
.split('\n')
.where((element) => element.isNotEmpty);
dartExecutable = dartExecutables
.sortedBy((path) => path.contains('flutter').toString())
.first;
}
static List<Glob> toGlobs(List<String> ignoredPackages) =>
ignoredPackages.map((pattern) => Glob(pattern, recursive: true)).toList();
final GithubApi github;
final Check check;
final List<String> warnOn;
final List<String> failOn;
final bool coverageweb;
final List<Glob> ignoredPackages;
final Map<Check, List<Glob>> ignoredFor;
final List<Glob> flutterPackageGlobs;
final Directory baseDirectory;
final List<String> experiments;
final Logger log;
late final String dartExecutable;
late final String? flutterExecutable;
List<Glob> get ignored => [...ignoredPackages, ...ignoredFor[check] ?? []];
String executable(bool isFlutter) =>
isFlutter ? flutterExecutable ?? dartExecutable : dartExecutable;
Future<void> healthCheck() async {
// Do basic validation of our expected env var.
if (!expectEnv(github.repoSlug?.fullName, 'GITHUB_REPOSITORY')) return;
if (!expectEnv(github.issueNumber?.toString(), 'ISSUE_NUMBER')) return;
if (!expectEnv(github.sha, 'GITHUB_SHA')) return;
var checkName = check.displayName;
log('Start health check for the check $checkName with');
log(' warnOn: $warnOn');
log(' failOn: $failOn');
log(' coverageweb: $coverageweb');
log(' flutterPackages: $flutterPackageGlobs');
log(' ignoredPackages: $ignoredPackages');
log(' ignoredFor: $ignoredFor');
log(' baseDirectory: $baseDirectory');
log(' experiments: $experiments');
log('Checking for $checkName');
if (!github.prLabels.contains('skip-$checkName-check')) {
final firstResult = await checkFor(check)();
final HealthCheckResult finalResult;
if (warnOn.contains(check.displayName) &&
firstResult.severity == Severity.error) {
finalResult = firstResult.withSeverity(Severity.warning);
} else if (failOn.contains(check.displayName) &&
firstResult.severity == Severity.warning) {
finalResult = firstResult.withSeverity(Severity.error);
} else {
finalResult = firstResult;
}
await writeInComment(github, finalResult);
var severity = finalResult.severity.name.toUpperCase();
log('\n\n$severity: $checkName done.\n\n');
} else {
log('Skipping $checkName, as the skip tag is present.');
}
}
Future<HealthCheckResult> Function() checkFor(Check check) => switch (check) {
Check.license => licenseCheck,
Check.changelog => changelogCheck,
Check.coverage => coverageCheck,
Check.breaking => breakingCheck,
Check.donotsubmit => doNotSubmitCheck,
Check.leaking => leakingCheck,
};
Future<HealthCheckResult> breakingCheck() async {
final filesInPR = await listFilesInPRorAll();
final changeForPackage = <Package, BreakingChange>{};
final flutterPackages =
packagesContaining(filesInPR, only: flutterPackageGlobs);
log('This list of Flutter packages is $flutterPackages');
for (var package in packagesContaining(filesInPR, ignore: ignored)) {
log('Look for changes in $package');
var relativePath =
path.relative(package.directory.path, from: directory.path);
var tempDirectory = Directory.systemTemp.createTempSync();
var reportPath = path.join(tempDirectory.path, 'report.json');
runDashProcess(
flutterPackages,
package,
[
'pub',
'global',
'activate',
...['-sgit', 'https://github.com/bmw-tech/dart_apitool.git'],
...['--git-ref', apiToolHash],
],
logStdout: false,
);
runDashProcess(
flutterPackages,
package,
[
...['pub', 'global', 'run'],
'dart_apitool:main',
'diff',
'--no-check-sdk-version',
...['--old', getCurrentVersionOfPackage(package)],
...['--new', relativePath],
...['--report-format', 'json'],
...['--report-file-path', reportPath],
],
);
var fullReportString = File(reportPath).readAsStringSync();
var decoded = jsonDecode(fullReportString) as Map<String, dynamic>;
var report = decoded['report'] as Map<String, dynamic>;
var formattedChanges = const JsonEncoder.withIndent(' ').convert(report);
log('Breaking change report:\n$formattedChanges');
final versionMap = decoded['version'] as Map<String, dynamic>;
changeForPackage[package] = BreakingChange(
level: _breakingLevel(report),
oldVersion: Version.parse(versionMap['old'].toString()),
newVersion: Version.parse(versionMap['new'].toString()),
neededVersion: Version.parse(versionMap['needed'].toString()),
versionIsFine: versionMap['success'] as bool,
explanation: versionMap['explanation'].toString(),
);
}
return HealthCheckResult(
Check.breaking,
changeForPackage.values.any((element) => !element.versionIsFine)
? Severity.warning
: Severity.info,
'''
| Package | Change | Current Version | New Version | Needed Version | Looking good? |
| :--- | :--- | ---: | ---: | ---: | ---: |
${changeForPackage.entries.map((e) => '|${e.key.name}|${e.value.toMarkdownRow()}|').join('\n')}
''',
);
}
String getCurrentVersionOfPackage(Package package) => 'pub://${package.name}';
ProcessResult runDashProcess(
List<Package> flutterPackages,
Package package,
List<String> arguments, {
bool logStdout = true,
}) {
var exec = executable(flutterPackages.any((p) => p.name == package.name));
log('Running `$exec ${arguments.join(' ')}` in ${directory.path}');
var runApiTool = Process.runSync(
exec,
arguments,
workingDirectory: directory.path,
);
final out = (runApiTool.stdout as String).trimRight();
if (logStdout && out.isNotEmpty) {
print(out);
}
final err = (runApiTool.stderr as String).trimRight();
if (err.isNotEmpty) {
print(err);
}
return runApiTool;
}
BreakingLevel _breakingLevel(Map<String, dynamic> report) {
BreakingLevel breakingLevel;
if ((report['noChangesDetected'] as bool?) ?? false) {
breakingLevel = BreakingLevel.none;
} else if ((report['breakingChanges'] as Map? ?? {}).isNotEmpty) {
breakingLevel = BreakingLevel.breaking;
} else if ((report['nonBreakingChanges'] as Map? ?? {}).isNotEmpty) {
breakingLevel = BreakingLevel.nonBreaking;
} else {
breakingLevel = BreakingLevel.none;
}
return breakingLevel;
}
Future<HealthCheckResult> leakingCheck() async {
var filesInPR = await listFilesInPRorAll();
final leaksForPackage = <Package, List<String>>{};
final flutterPackages =
packagesContaining(filesInPR, only: flutterPackageGlobs);
log('This list of Flutter packages is $flutterPackages');
for (var package in packagesContaining(filesInPR)) {
log('');
log('--- ${package.name} ---');
log('Look for leaks in ${package.name}');
var relativePath =
path.relative(package.directory.path, from: directory.path);
var tempDirectory = Directory.systemTemp.createTempSync();
var reportPath = path.join(tempDirectory.path, 'leaks.json');
runDashProcess(
flutterPackages,
package,
[
'pub',
'global',
'activate',
...['-sgit', 'https://github.com/bmw-tech/dart_apitool.git'],
...['--git-ref', apiToolHash],
],
logStdout: false,
);
var arguments = [
...['pub', 'global', 'run'],
'dart_apitool:main',
'extract',
...['--input', relativePath],
...['--output', reportPath],
];
var runApiTool = runDashProcess(
flutterPackages,
package,
arguments,
);
log('');
if (runApiTool.exitCode == 0) {
var fullReportString = await File(reportPath).readAsString();
var decoded = jsonDecode(fullReportString) as Map<String, dynamic>;
var leaks = decoded['missingEntryPoints'] as List<dynamic>;
if (leaks.isNotEmpty) {
leaksForPackage[package] = leaks.cast();
final desc = leaks.map((item) => '$item').join(', ');
log('Leaked symbols found: $desc.');
log('');
final report = const JsonEncoder.withIndent(' ').convert(decoded);
log(report);
} else {
log('No leaks found.');
}
log('');
} else {
throw ProcessException(
executable(flutterPackages.contains(package)),
arguments,
'Api tool finished with exit code ${runApiTool.exitCode}',
);
}
}
return HealthCheckResult(
Check.leaking,
leaksForPackage.values.any((leaks) => leaks.isNotEmpty)
? Severity.warning
: Severity.success,
'''
The following packages contain symbols visible in the public API, but not exported by the library. Export these symbols or remove them from your publicly visible API.
| Package | Leaked API symbols |
| :--- | :--- |
${leaksForPackage.entries.map((e) => '|${e.key.name}|${e.value.join('<br>')}|').join('\n')}
''',
);
}
Future<HealthCheckResult> licenseCheck() async {
var files = await listFilesInPRorAll();
var allFilePaths = await getFilesWithoutLicenses(directory, ignored);
var groupedPaths = allFilePaths.groupListsBy((filePath) {
return files.any((f) => f.filename == filePath);
});
var unchangedFilesPaths = groupedPaths[false] ?? [];
var unchangedMarkdown = '''
<details>
<summary>
Unrelated files missing license headers
</summary>
| Files |
| :--- |
${unchangedFilesPaths.map((e) => '|$e|').join('\n')}
</details>
''';
var changedFilesPaths = groupedPaths[true] ?? [];
var markdownResult = '''
```
$license
```
| Files |
| :--- |
${changedFilesPaths.isNotEmpty ? changedFilesPaths.map((e) => '|$e|').join('\n') : '| _no missing headers_ |'}
All source files should start with a [license header](https://github.com/dart-lang/ecosystem/wiki/License-Header).
${unchangedFilesPaths.isNotEmpty ? unchangedMarkdown : ''}
''';
return HealthCheckResult(
Check.license,
changedFilesPaths.isNotEmpty ? Severity.error : Severity.success,
markdownResult,
);
}
bool healthYamlChanged(List<GitFile> files) => files
.where((file) =>
[FileStatus.added, FileStatus.modified].contains(file.status))
.any((file) =>
file.filename.endsWith('health.yaml') ||
file.filename.endsWith('health.yml'));
Future<HealthCheckResult> changelogCheck() async {
var filePaths = await packagesWithoutChangelog(
github,
ignored,
directory,
);
final markdownResult = '''
| Package | Changed Files |
| :--- | :--- |
${filePaths.entries.map((e) => '| package:${e.key.name} | ${e.value.map((e) => e.filename).join('<br />')} |').join('\n')}
Changes to files need to be [accounted for](https://github.com/dart-lang/ecosystem/wiki/Changelog) in their respective changelogs.
''';
return HealthCheckResult(
Check.changelog,
filePaths.isNotEmpty ? Severity.error : Severity.success,
markdownResult,
);
}
Future<HealthCheckResult> doNotSubmitCheck() async {
final dns = 'DO_NOT${'_'}SUBMIT';
// To avoid trying to read non-text files.
const supportedExtensions = ['.dart', '.json', '.md', '.txt'];
final body = await github.pullrequestBody();
var files = await listFilesInPRorAll();
log('Checking for DO_NOT${'_'}SUBMIT strings: $files');
final filesWithDNS = files
.where((file) =>
![FileStatus.removed, FileStatus.unchanged].contains(file.status))
.where((file) =>
supportedExtensions.contains(path.extension(file.filename)))
.where((file) => File(file.pathInRepository)
.readAsStringSync()
.contains('DO_NOT${'_'}SUBMIT'))
.toList();
log('Found files with $dns: $filesWithDNS');
final bodyContainsDNS = body.contains(dns);
log('The body contains a $dns string: $bodyContainsDNS');
final markdownResult = '''
Body contains `$dns`: $bodyContainsDNS
| Files with `$dns` |
| :--- |
${filesWithDNS.map((e) => e.filename).map((e) => '|$e|').join('\n')}
''';
final hasDNS = filesWithDNS.isNotEmpty || bodyContainsDNS;
return HealthCheckResult(
Check.donotsubmit,
hasDNS ? Severity.error : Severity.success,
hasDNS ? markdownResult : null,
);
}
Future<List<GitFile>> listFilesInPRorAll() async {
final files = await github.listFilesForPR(directory, ignored);
return healthYamlChanged(files) ? await _getAllFiles() : files;
}
Future<List<GitFile>> _getAllFiles() async => await directory
.list(recursive: true)
.where((entity) => entity is File)
.map((file) => path.relative(file.path, from: directory.path))
.where((file) => ignored.none((glob) => glob.matches(file)))
.map((file) => GitFile(file, FileStatus.added, directory))
.toList();
Future<HealthCheckResult> coverageCheck() async {
var coverage = Coverage(
coverageweb,
ignored,
directory,
experiments,
dartExecutable,
);
var files = await listFilesInPRorAll();
var coverageResult = coverage.compareCoveragesFor(files, baseDirectory);
var markdownResult = '''
| File | Coverage |
| :--- | :--- |
${coverageResult.coveragePerFile.entries.map((e) => '|${e.key}| ${e.value.toMarkdown()} |').join('\n')}
This check for [test coverage](https://github.com/dart-lang/ecosystem/wiki/Test-Coverage) is informational (issues shown here will not fail the PR).
''';
return HealthCheckResult(
Check.coverage,
Severity.values[coverageResult.coveragePerFile.values
.map((change) => change.severity.index)
.fold(0, max)],
markdownResult,
);
}
Future<void> writeInComment(
GithubApi github, HealthCheckResult result) async {
final String markdownSummary;
if (result.markdown != null) {
var markdown = result.markdown;
var isWorseThanInfo = result.severity.index >= Severity.warning.index;
markdownSummary = '''
<details${isWorseThanInfo ? ' open' : ''}>
<summary>
<strong>${check.tag}</strong> ${result.severity.emoji}
</summary>
$markdown
${isWorseThanInfo ? 'This check can be disabled by tagging the PR with `skip-${result.check.displayName}-check`.' : ''}
</details>
''';
} else {
markdownSummary = '';
}
github.appendStepSummary(markdownSummary);
var commentFile = File(commentPath);
log('Saving comment markdown to file ${commentFile.path}');
await commentFile.create(recursive: true);
await commentFile.writeAsString(markdownSummary);
if (result.severity == Severity.error && exitCode == 0) {
exitCode = 1;
}
}
List<Package> packagesContaining(
List<GitFile> filesInPR, {
List<Glob>? ignore,
List<Glob>? only,
}) {
var files = filesInPR.where((element) => element.status.isRelevant);
return Repository(directory)
.locatePackages(ignore: ignore, only: only)
.where((package) => files.any((file) =>
path.isWithin(package.directory.path, file.pathInRepository)))
.toList();
}
}
enum BreakingLevel {
none('None'),
nonBreaking('Non-Breaking'),
breaking('Breaking');
final String name;
const BreakingLevel(this.name);
}
class HealthCheckResult {
final Check check;
final Severity severity;
final String? markdown;
HealthCheckResult(this.check, this.severity, this.markdown);
HealthCheckResult withSeverity(Severity severity) => HealthCheckResult(
check,
severity,
markdown,
);
}
class BreakingChange {
final BreakingLevel level;
final Version oldVersion;
final Version newVersion;
final Version neededVersion;
final bool versionIsFine;
final String explanation;
BreakingChange({
required this.level,
required this.oldVersion,
required this.newVersion,
required this.neededVersion,
required this.versionIsFine,
required this.explanation,
});
String toMarkdownRow() => [
level.name,
oldVersion,
newVersion,
versionIsFine ? neededVersion : '**$neededVersion** <br> $explanation',
versionIsFine ? ':heavy_check_mark:' : ':warning:'
].map((e) => e.toString()).join('|');
}