-
Notifications
You must be signed in to change notification settings - Fork 102
/
Copy pathwebhook_subscription.dart
1055 lines (946 loc) · 37.4 KB
/
webhook_subscription.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
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
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2019 The Flutter Authors. 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 'package:cocoon_server/logging.dart';
import 'package:github/github.dart';
import 'package:github/hooks.dart';
import 'package:meta/meta.dart';
import '../../../cocoon_service.dart';
import '../../../protos.dart' as pb;
import '../../model/github/checks.dart' as cocoon_checks;
import '../../model/github/checks.dart';
import '../../request_handling/exceptions.dart';
import '../../request_handling/subscription_handler.dart';
import '../../service/commit_service.dart';
import '../../service/datastore.dart';
import '../../service/github_service.dart';
// Filenames which are not actually tests.
const List<String> kNotActuallyATest = <String>[
'packages/flutter/lib/src/gestures/hit_test.dart',
];
/// List of repos that require check for tests.
Set<RepositorySlug> kNeedsTests = <RepositorySlug>{
Config.flutterSlug,
Config.packagesSlug,
};
// Extentions for files that use // for single line comments.
// See [_allChangesAreCodeComments] for more.
@visibleForTesting
const Set<String> knownCommentCodeExtensions = <String>{
'cc',
'cpp',
'dart',
'gradle',
'groovy',
'java',
'kt',
'm',
'mm',
'swift',
};
/// Subscription for processing GitHub webhooks.
///
/// The PubSub subscription is set up here:
/// https://console.cloud.google.com/cloudpubsub/subscription/detail/github-webhooks-sub?project=flutter-dashboard&tab=overview
///
/// This endpoint enables Cocoon to recover from outages.
///
/// This endpoint takes in a POST request with the GitHub event JSON.
// TODO(chillers): There's potential now to split this into seprate subscriptions
// for various activities (such as infra vs releases). This would mitigate
// breakages across Cocoon.
@immutable
class GithubWebhookSubscription extends SubscriptionHandler {
/// Creates a subscription for processing GitHub webhooks.
const GithubWebhookSubscription({
required super.cache,
required super.config,
required this.scheduler,
required this.gerritService,
required this.commitService,
this.datastoreProvider = DatastoreService.defaultProvider,
required this.fusionTester,
super.authProvider,
this.pullRequestLabelProcessorProvider = PullRequestLabelProcessor.new,
// Gets the initial github events from this sub after the webhook uploads them.
}) : super(subscriptionName: 'github-webhooks-sub');
/// Cocoon scheduler to trigger tasks against changes from GitHub.
final Scheduler scheduler;
/// To verify whether a commit was mirrored to GoB.
final GerritService gerritService;
/// Used to handle push events and create commits based on those events.
final CommitService commitService;
final DatastoreServiceProvider datastoreProvider;
final FusionTester fusionTester;
final PullRequestLabelProcessorProvider pullRequestLabelProcessorProvider;
@override
Future<Body> post() async {
if (message.data == null || message.data!.isEmpty) {
log.warning('GitHub webhook message was empty. No-oping');
return Body.empty;
}
final webhook = pb.GithubWebhookMessage.fromJson(message.data!);
log.info('Processing ${webhook.event}');
log.finest(webhook.payload);
switch (webhook.event) {
case 'pull_request':
await _handlePullRequest(webhook.payload);
break;
case 'merge_group':
await _handleMergeGroup(webhook.payload);
break;
case 'check_run':
final event = jsonDecode(webhook.payload) as Map<String, dynamic>;
final checkRunEvent = cocoon_checks.CheckRunEvent.fromJson(event);
if (await scheduler.processCheckRun(checkRunEvent) == false) {
throw InternalServerError(
'Failed to process check_run event. checkRunEvent: $checkRunEvent',
);
}
break;
case 'push':
final event = jsonDecode(webhook.payload) as Map<String, dynamic>;
final branch =
(event['ref'] as String).split(
'/',
)[2]; // Eg: refs/heads/beta would return beta.
final repository = event['repository']['name'] as String;
// If the branch is beta/stable, then a commit wasn't created through a PR,
// meaning the commit needs to be added to the datastore here instead.
if (repository == 'flutter' &&
(branch == 'stable' || branch == 'beta')) {
await commitService.handlePushGithubRequest(event);
}
break;
case 'create':
final createEvent = CreateEvent.fromJson(
json.decode(webhook.payload) as Map<String, dynamic>,
);
final candidateBranchRegex = RegExp(r'flutter-\d+\.\d+-candidate\.\d+');
// Create a commit object for candidate branches in the datastore so
// dart-internal builds that are triggered by the initial branch
// creation have an associated commit.
if (candidateBranchRegex.hasMatch(createEvent.ref!)) {
log.fine(
'Branch ${createEvent.ref} is a candidate branch, creating new commit in the datastore',
);
await commitService.handleCreateGithubRequest(createEvent);
}
}
return Body.empty;
}
/// Handles a GitHub webhook with the event type "pull_request".
///
/// Regarding merged pull request events: the commit must be mirrored to GoB
/// before we can trigger postsubmit tasks. If the commit is not found, the
/// event will be failed so it can be retried. As of Jan 26, 2023, the
/// retention policy for Pub/Sub messages is 7 days. This event will be
/// retried with exponential backoff within that time period. The GoB mirror
/// should be caught up within that time frame via either the internal
/// mirroring service or [VacuumGithubCommits].
Future<void> _handlePullRequest(String rawRequest) async {
final pullRequestEvent = _getPullRequestEvent(rawRequest);
if (pullRequestEvent == null) {
throw const BadRequestException('Expected pull request event.');
}
final eventAction = pullRequestEvent.action;
final pr = pullRequestEvent.pullRequest!;
final crumb = '$GithubWebhookSubscription._handlePullRequest(${pr.number})';
final slug = pr.base!.repo!.slug();
if (!config.supportedRepos.contains(slug)) {
log.warning(
'$crumb: asked to handle unsupported repo $slug for ${pr.htmlUrl}',
);
throw const InternalServerError('Unsupported repository');
}
// See the API reference:
// https://developer.github.com/v3/activity/events/types/#pullrequestevent
// which unfortunately is a bit light on explanations.
log.info('$crumb: processing $eventAction for ${pr.htmlUrl}');
switch (eventAction) {
case 'closed':
await _processPullRequestClosed(pullRequestEvent);
break;
case 'edited':
await _checkForTests(pullRequestEvent);
// In the event of the base ref changing we want to start new checks.
if (pullRequestEvent.changes != null &&
pullRequestEvent.changes!.base != null) {
await _scheduleIfMergeable(pullRequestEvent);
}
break;
case 'opened':
case 'reopened':
// These cases should trigger LUCI jobs. The closed event should happen
// before these which should cancel all in progress checks.
await _checkForTests(pullRequestEvent);
await _scheduleIfMergeable(pullRequestEvent);
await _tryReleaseApproval(pullRequestEvent);
break;
case 'labeled':
log.info(
'$crumb: PR labels = [${pr.labels?.map((label) => '"${label.name}"').join(', ')}]',
);
await _processLabels(pr);
break;
case 'synchronize':
// This indicates the PR has new commits. We need to cancel old jobs
// and schedule new ones.
await _scheduleIfMergeable(pullRequestEvent);
break;
case 'dequeued':
await _respondToPullRequestDequeued(pullRequestEvent);
break;
// Ignore the rest of the events.
case 'ready_for_review':
case 'unlabeled':
case 'assigned':
case 'locked':
case 'review_request_removed':
case 'review_requested':
case 'unassigned':
case 'unlocked':
break;
}
}
Future<void> _processLabels(PullRequest pullRequest) async {
final slug = pullRequest.base!.repo!.slug();
final githubService = await config.createGithubService(slug);
final labelProcessor = pullRequestLabelProcessorProvider(
config: config,
githubService: githubService,
pullRequest: pullRequest,
);
return labelProcessor.processLabels();
}
/// Handles a GitHub webhook with the event type "merge_group".
///
/// A merge group contains commits from multiple pull requests. Each pull
/// request is squashed into one commit, then that commit is stacked on top of
/// other commits in the queue. A merge group is therefore not associated with
/// any one pull request. Instead, its `head_sha` (the SHA of the top-most
/// commit) is the one the CI runs all the checks against. If the checks pass,
/// the group of commits is pushed onto the main/master branch.
///
/// The commit SHAs in the merge group are not the same as the commit SHAs in
/// the pull request. Merge group SHAs are rewritten while they are stacked on
/// top of each other.
Future<void> _handleMergeGroup(String rawRequest) async {
final request = json.decode(rawRequest);
if (request is! Map<String, Object?>) {
throw BadRequestException('Malformed merge_group request:\n$rawRequest');
}
final mergeGroupEvent = MergeGroupEvent.fromJson(request);
final MergeGroupEvent(:mergeGroup, :action, :reason) = mergeGroupEvent;
final headSha = mergeGroup.headSha;
final slug = mergeGroupEvent.repository!.slug();
// See the API reference:
// https://docs.github.com/en/webhooks/webhook-events-and-payloads#merge_group
log.info('Processing $action for merge queue @ $headSha');
switch (action) {
// A merge group (a group of PRs to be tested a merged together) was
// created and Github is requesting checks to be performed before merging
// into the main branch. Cocoon should kick off CI jobs needed to verify
// the PR group.
case 'checks_requested':
log.info('Checks requests for merge queue @ $headSha');
if (!await _shaExistsInGob(slug, headSha)) {
throw InternalServerError(
'$slug/$headSha was not found on GoB. Failing so this event can be retried',
);
}
log.info(
'$slug/$headSha was found on GoB mirror. Scheduling merge group tasks',
);
await scheduler.triggerMergeGroupTargets(
mergeGroupEvent: mergeGroupEvent,
);
// A merge group was deleted. This can happen when a PR is pulled from the
// merge queue. All CI jobs pertaining to this merge group should be
// stopped to save CI resources, as Github will no longer merge this group
// into the main branch.
case 'destroyed':
log.info(
'Merge group destroyed for $slug/$headSha because it was $reason.',
);
if (reason == 'invalidated' || reason == 'dequeued') {
await scheduler.cancelDestroyedMergeGroupTargets(headSha: headSha);
} else if (reason == 'merged') {
log.info('Merge group for $slug/$headSha was merged successfully.');
} else {
log.warning(
'Unrecognized reason for merge group destroyed event: $reason',
);
}
}
}
Future<bool> _commitExistsInGob(PullRequest pr) async {
final slug = pr.base!.repo!.slug();
final sha = pr.mergeCommitSha!;
return _shaExistsInGob(slug, sha);
}
Future<bool> _shaExistsInGob(RepositorySlug slug, String sha) async {
final gobCommit = await gerritService.findMirroredCommit(slug, sha);
return gobCommit != null;
}
/// Responds to the "dequeued" pull request event.
///
/// See also: https://docs.github.com/en/webhooks/webhook-events-and-payloads?actionType=dequeued#pull_request
Future<void> _respondToPullRequestDequeued(
PullRequestEvent pullRequestEvent,
) async {
final pr = pullRequestEvent.pullRequest!;
final slug = pullRequestEvent.repository!.slug();
final githubService = await config.createGithubService(slug);
// Remove the autosubmit label when a pull request is kicked out of the
// merge queue to avoid infinite loops.
//
// An example of an infinite loop:
//
// 1. Autosubmit bot is notified that a PR is ready (reviewed, all green, has `autosubmit` label).
// 2. Autosubmit bot puts the PR onto the merge queue.
// 3. The PR fails some tests in the merge queue.
// 4. Github kicks the PR back, removing it fom the merge queue.
// 5. GOTO step 1.
//
// Removing the `autosubmit` label will prevent the autosubmit bot from
// repeating the process, until a human looks at the PR, decides that it's
// ready again, and manually adds the `autosubmit` label on it.
final hasAutosubmitLabel =
pr.labels?.any((label) => label.name == Config.kAutosubmitLabel) ??
false;
if (hasAutosubmitLabel) {
await githubService.removeLabel(
slug,
pr.number!,
Config.kAutosubmitLabel,
);
}
}
/// This method assumes that jobs should be cancelled if they are already
/// runnning.
Future<void> _scheduleIfMergeable(PullRequestEvent pullRequestEvent) async {
final pr = pullRequestEvent.pullRequest!;
final slug = pullRequestEvent.repository!.slug();
log.info(
'Scheduling tasks if mergeable(${pr.mergeable}): owner=${slug.owner} repo=${slug.name} and pr=${pr.number}',
);
// The mergeable flag may be null. False indicates there's a merge conflict,
// null indicates unknown. Err on the side of allowing the job to run.
if (pr.mergeable == false) {
final slug = pullRequestEvent.repository!.slug();
final gitHubClient = await config.createGitHubClient(pullRequest: pr);
final body = config.mergeConflictPullRequestMessage;
if (!await _alreadyCommented(gitHubClient, pr, body)) {
await gitHubClient.issues.createComment(slug, pr.number!, body);
}
return;
}
await scheduler.triggerPresubmitTargets(pullRequest: pr);
// When presubmit targets are scheduled the PR acquires a new Merge Queue
// Guard. This can happen when the PR is just created, a new commit is
// pushed, reopened, etc. In all cases the guard may need to be unlocked if,
// for example, the "emergency" label is present.
await _processLabels(pr);
}
/// Release tooling generates cherrypick pull requests that should be granted an approval.
Future<void> _tryReleaseApproval(PullRequestEvent pullRequestEvent) async {
final pr = pullRequestEvent.pullRequest!;
final slug = pullRequestEvent.repository!.slug();
final defaultBranch = Config.defaultBranch(slug);
final branch = pr.base?.ref;
if (branch == null || branch.contains(defaultBranch)) {
// This isn't a release branch PR
return;
}
final releaseAccounts = await config.releaseAccounts;
if (releaseAccounts.contains(pr.user?.login) == false) {
// The author isn't in the list of release accounts, do nothing
return;
}
final gitHubClient = config.createGitHubClientWithToken(
await config.githubOAuthToken,
);
final review = CreatePullRequestReview(
slug.owner,
slug.name,
pr.number!,
'APPROVE',
);
await gitHubClient.pullRequests.createReview(slug, review);
}
Future<void> _processPullRequestClosed(
PullRequestEvent pullRequestEvent,
) async {
final pr = pullRequestEvent.pullRequest!;
// Cancel any outstanding presubmit jobs. They are useless after the PR is
// closed. If the PR was merged, then the post-submit results will determine
// what needs to be done about this PR (maybe it lands, or maybe it will be
// reverted). If the PR was just closed and abandoned, well, that means we
// don't care about it any more.
await scheduler.cancelPreSubmitTargets(
pullRequest: pr,
reason: (!pr.merged!) ? 'Pull request closed' : 'Pull request merged',
);
if (pr.merged!) {
log.fine('Pull request ${pr.number} was closed and merged.');
// To avoid polluting the repo with temporary revert branches, delete the
// branch after the reverted PR is merged.
//
// This can be done no ealier than the event declaring the PR both
// 'closed' and merged, because:
//
// * If the branch is deleted before the PR reaches 'closed', then GitHub
// will force-close the PR because the branch is the source of all the
// code changes in the PR. In a previous iteration, Cocoon used to
// delete the branch immediately after merging it. However, with merge
// queues a PR is not merged by Cocoon anymore. It stays open while in
// the merge queue. Deleting the branch while in the queue would close
// the PR and not merge it.
// * If a PR is closed but not merged, the author may still want to reopen
// the PR. That would not be possible if the source branch was deleted.
final isRevertPullRequest =
pr.labels?.any((label) => label.name == Config.revertOfLabel) == true;
if (isRevertPullRequest) {
log.info(
'Revert merged successfully, deleting branch ${pr.head!.ref!}',
);
final slug = pullRequestEvent.repository!.slug();
final githubService = await config.createGithubService(slug);
await githubService.deleteBranch(slug, pr.head!.ref!);
}
if (await _commitExistsInGob(pr)) {
log.fine(
'Merged commit was found on GoB mirror. Scheduling postsubmit tasks...',
);
return scheduler.addPullRequest(pr);
}
throw InternalServerError(
'${pr.mergeCommitSha!} was not found on GoB. Failing so this event can be retried...',
);
}
}
Future<void> _checkForTests(PullRequestEvent pullRequestEvent) async {
final pr = pullRequestEvent.pullRequest!;
final eventAction = pullRequestEvent.action;
final slug = pr.base!.repo!.slug();
final isTipOfTree = pr.base!.ref == Config.defaultBranch(slug);
final gitHubClient = await config.createGitHubClient(pullRequest: pr);
await _validateRefs(gitHubClient, pr);
if (kNeedsTests.contains(slug) && isTipOfTree) {
switch (slug.name) {
case 'flutter':
final isFusion = await fusionTester.isFusionBasedRef(
slug,
pr.head!.sha!,
);
final files =
await gitHubClient.pullRequests
.listFiles(slug, pr.number!)
.toList();
await _applyFrameworkRepoLabels(
gitHubClient,
eventAction,
pr,
isFusion: isFusion,
files: files,
);
if (isFusion) {
await _applyEngineRepoLabels(
gitHubClient,
eventAction,
pr,
files: files,
);
}
case 'packages':
return _applyPackageTestChecks(gitHubClient, eventAction, pr);
}
}
}
Future<void> _applyFrameworkRepoLabels(
GitHub gitHubClient,
String? eventAction,
PullRequest pr, {
bool isFusion = false,
List<PullRequestFile>? files,
}) async {
if (pr.user!.login == 'engine-flutter-autoroll') {
return;
}
final slug = pr.base!.repo!.slug();
log.info(
'Applying framework repo labels for: owner=${slug.owner} repo=${slug.name} isFusion=$isFusion and pr=${pr.number}',
);
files ??=
await gitHubClient.pullRequests.listFiles(slug, pr.number!).toList();
final labels = <String>{};
var hasTests = false;
var needsTests = false;
var frameworkFiles = 0;
for (var file in files) {
final filename = file.filename!;
if (!_isFusionEnginePath(filename)) {
frameworkFiles++;
}
if (_fileContainsAddedCode(file) &&
!_isTestExempt(filename) &&
!filename.startsWith('dev/bots/') &&
!filename.endsWith('.gitignore')) {
needsTests = !_allChangesAreCodeComments(file);
}
// Check to see if tests were submitted with this PR.
if (_isAFrameworkTest(filename)) {
hasTests = true;
}
}
if (frameworkFiles == 0) {
// a fusion / engine only change.
return;
}
if (pr.user!.login == 'fluttergithubbot') {
needsTests = false;
labels.addAll(<String>['c: tech-debt', 'c: flake']);
}
if (labels.isNotEmpty) {
await gitHubClient.issues.addLabelsToIssue(
slug,
pr.number!,
labels.toList(),
);
}
// We do not need to add test labels if this is an auto roller author.
if (config.rollerAccounts.contains(pr.user!.login)) {
return;
}
if (!hasTests && needsTests && !pr.draft! && !_isReleaseBranch(pr)) {
final body = config.missingTestsPullRequestMessage;
if (!await _alreadyCommented(gitHubClient, pr, body)) {
await gitHubClient.issues.createComment(slug, pr.number!, body);
}
}
}
bool _isAFrameworkTest(String filename) {
if (kNotActuallyATest.any(filename.endsWith)) {
return false;
}
// Check for Objective-C tests which end in either "Tests.m" or "Test.m"
// in the "dev" directory.
final objectiveCTestRegex = RegExp(r'.*dev\/.*Test[s]?\.m$');
return filename.endsWith('_test.dart') ||
filename.endsWith('.expect') ||
filename.contains('test_fixes') ||
// Include updates to test utilities or test data
filename.contains('packages/flutter_tools/test/') ||
// Kotlin source tests, used in the Flutter Gradle Plugin.
filename.startsWith('packages/flutter_tools/gradle/src/test/') ||
filename.startsWith('dev/bots/analyze.dart') ||
filename.startsWith('dev/bots/test.dart') ||
filename.startsWith('dev/devicelab/bin/tasks') ||
filename.startsWith('dev/devicelab/lib/tasks') ||
filename.startsWith('dev/benchmarks') ||
objectiveCTestRegex.hasMatch(filename);
}
/// Returns true if changes to [filename] are exempt from the testing
/// requirement, across repositories.
bool _isTestExempt(String filename, {String engineBasePath = ''}) {
final isBuildPythonScript =
filename.startsWith('${engineBasePath}sky/tools') &&
filename.endsWith('.py');
return filename.contains('.ci.yaml') ||
filename.endsWith('analysis_options.yaml') ||
filename.endsWith('AUTHORS') ||
filename.endsWith('CODEOWNERS') ||
filename.endsWith('TESTOWNERS') ||
filename.endsWith('pubspec.yaml') ||
filename.endsWith('pubspec.yaml.tmpl') ||
// Exempt categories.
filename.contains('.github/') ||
filename.endsWith('.md') ||
// Exempt paths.
filename.startsWith('dev/devicelab/lib/versions/gallery.dart') ||
filename.startsWith('dev/integration_tests/') ||
filename.startsWith('docs/') ||
filename.startsWith('${engineBasePath}docs/') ||
filename.endsWith('test/flutter_test_config.dart') ||
// ↓↓↓ Begin engine specific paths ↓↓↓
filename == 'DEPS' || // note: in monorepo; DEPS is still at the root.
isBuildPythonScript ||
filename.startsWith('${engineBasePath}impeller/fixtures/') ||
filename.startsWith('${engineBasePath}impeller/golden_tests/') ||
filename.startsWith('${engineBasePath}impeller/playground/') ||
filename.startsWith(
'${engineBasePath}shell/platform/embedder/tests/',
) ||
filename.startsWith(
'${engineBasePath}shell/platform/embedder/fixtures/',
) ||
filename.startsWith('${engineBasePath}testing/') ||
filename.startsWith('${engineBasePath}tools/clangd_check/');
}
bool _isFusionEnginePath(String path) =>
path.startsWith('engine/') || path == 'DEPS';
Future<void> _applyEngineRepoLabels(
GitHub gitHubClient,
String? eventAction,
PullRequest pr, {
List<PullRequestFile>? files,
}) async {
// Do not apply the test labels for the autoroller accounts.
if (pr.user!.login == 'skia-flutter-autoroll') {
return;
}
final slug = pr.base!.repo!.slug();
var hasTests = false;
var needsTests = false;
// If engine labels are being applied to the flutterSlug - we're in a fusion repo.
final isFusion = slug == Config.flutterSlug;
final engineBasePath = isFusion ? 'engine/src/flutter/' : '';
var engineFiles = 0;
files ??=
await gitHubClient.pullRequests.listFiles(slug, pr.number!).toList();
for (var file in files) {
final path = file.filename!;
if (isFusion && _isFusionEnginePath(path)) {
engineFiles++;
}
if (_fileContainsAddedCode(file) &&
!_isTestExempt(path, engineBasePath: engineBasePath) &&
// License goldens are auto-generated.
!path.startsWith('${engineBasePath}ci/licenses_golden/') &&
// Build configuration files tell CI what to run.
!path.startsWith('${engineBasePath}ci/builders/') &&
// Build files don't need unit tests.
!path.endsWith('$engineBasePath.gn') &&
!path.endsWith('$engineBasePath.gni')) {
needsTests = !_allChangesAreCodeComments(file);
}
if (_isAnEngineTest(path)) {
hasTests = true;
}
}
if (isFusion && engineFiles == 0) {
// framework only change
return;
}
// We do not need to add test labels if this is an auto roller author.
if (config.rollerAccounts.contains(pr.user!.login)) {
return;
}
if (!hasTests && needsTests && !pr.draft! && !_isReleaseBranch(pr)) {
final body = config.missingTestsPullRequestMessage;
if (!await _alreadyCommented(gitHubClient, pr, body)) {
await gitHubClient.issues.createComment(slug, pr.number!, body);
}
}
}
bool _isAnEngineTest(String filename) {
final engineTestRegExp = RegExp(
r'(tests?|benchmarks?)\.(dart|java|mm|m|cc|sh|py)$',
);
return filename.contains('IosBenchmarks') ||
filename.contains('IosUnitTests') ||
filename.contains('scenario_app') ||
engineTestRegExp.hasMatch(filename.toLowerCase());
}
bool _fileContainsAddedCode(PullRequestFile file) {
// When null, do not assume 0 lines have been added.
final linesAdded = file.additionsCount ?? 1;
final linesDeleted = file.deletionsCount ?? 0;
final linesTotal = file.changesCount ?? linesDeleted + linesAdded;
return linesAdded > 0 || linesDeleted != linesTotal;
}
// Runs automated test checks for both flutter/packages.
Future<void> _applyPackageTestChecks(
GitHub gitHubClient,
String? eventAction,
PullRequest pr,
) async {
final slug = pr.base!.repo!.slug();
final files = gitHubClient.pullRequests.listFiles(slug, pr.number!);
var hasTests = false;
var needsTests = false;
await for (PullRequestFile file in files) {
final filename = file.filename!;
if (_fileContainsAddedCode(file) &&
!_isTestExempt(filename) &&
!filename.contains('.ci/') &&
// Custom package-specific test runners. These do not count as tests
// for the purposes of testing a change that otherwise needs tests,
// but since they are the driver for tests they don't need test
// coverage.
!filename.endsWith('tool/run_tests.dart') &&
!filename.endsWith('run_tests.sh')) {
needsTests = !_allChangesAreCodeComments(file);
}
// See https://github.com/flutter/flutter/blob/master/docs/ecosystem/testing/Plugin-Tests.md for discussion
// of various plugin test types and locations.
if (filename.endsWith('_test.dart') ||
// Native iOS/macOS tests.
filename.contains('RunnerTests/') ||
filename.contains('RunnerUITests/') ||
filename.contains('darwin/Tests/') ||
// Native Android tests.
filename.contains('android/src/test/') ||
filename.contains('androidTest/') ||
// Native Linux tests.
filename.endsWith('_test.cc') ||
// Native Windows tests.
filename.endsWith('_test.cpp') ||
// Pigeon native tests.
filename.contains('/platform_tests/') ||
// Test files in package-specific test folders.
filename.contains('go_router/test_fixes/') ||
filename.contains('go_router_builder/test_inputs/')) {
hasTests = true;
}
}
// We do not need to add test labels if this is an auto roller author.
if (config.rollerAccounts.contains(pr.user!.login)) {
return;
}
if (!hasTests && needsTests && !pr.draft! && !_isReleaseBranch(pr)) {
final body = config.missingTestsPullRequestMessage;
if (!await _alreadyCommented(gitHubClient, pr, body)) {
await gitHubClient.issues.createComment(slug, pr.number!, body);
}
}
}
/// Validate the base and head refs of the PR.
Future<void> _validateRefs(GitHub gitHubClient, PullRequest pr) async {
final slug = pr.base!.repo!.slug();
String body;
const releaseChannels = <String>['stable', 'beta', 'dev'];
// Close PRs that use a release branch as a source.
if (releaseChannels.contains(pr.head!.ref)) {
body = config.wrongHeadBranchPullRequestMessage(pr.head!.ref!);
if (!await _alreadyCommented(gitHubClient, pr, body)) {
await gitHubClient.pullRequests.edit(slug, pr.number!, state: 'closed');
await gitHubClient.issues.createComment(slug, pr.number!, body);
}
return;
}
final defaultBranchName = Config.defaultBranch(pr.base!.repo!.slug());
final baseName = pr.base!.ref!;
if (baseName == defaultBranchName) {
return;
}
if (_isReleaseBranch(pr)) {
body = config.releaseBranchPullRequestMessage;
if (!await _alreadyCommented(gitHubClient, pr, body)) {
await gitHubClient.issues.createComment(slug, pr.number!, body);
}
return;
}
// For repos migrated to main, close PRs opened against master.
final isMaster = pr.base?.ref == 'master';
final isMigrated = defaultBranchName == 'main';
// PRs should never be open to "beta" or "stable."
final isReleaseChannelBranch = releaseChannels.contains(pr.base?.ref);
if ((isMaster && isMigrated) || isReleaseChannelBranch) {
body = _getWrongBaseComment(
base: baseName,
defaultBranch: defaultBranchName,
);
if (!await _alreadyCommented(gitHubClient, pr, body)) {
await gitHubClient.pullRequests.edit(
slug,
pr.number!,
base: Config.defaultBranch(slug),
);
await gitHubClient.issues.createComment(slug, pr.number!, body);
}
}
}
bool _isReleaseBranch(PullRequest pr) {
final defaultBranchName = Config.defaultBranch(pr.base!.repo!.slug());
final baseName = pr.base!.ref!;
if (baseName == defaultBranchName) {
return false;
}
// Check if branch name confroms to the format flutter-x.x-candidate.x,
// A pr with conforming branch name is likely to be intended
// for a release branch, whereas a pr with non conforming name is likely
// caused by user misoperations, in which case bot
// will suggest open pull request against default branch instead.
final candidateTest = RegExp(r'flutter-\d+\.\d+-candidate\.\d+');
if (candidateTest.hasMatch(baseName) &&
candidateTest.hasMatch(pr.head!.ref!)) {
return true;
}
return false;
}
Future<bool> _alreadyCommented(
GitHub gitHubClient,
PullRequest pr,
String message,
) async {
final comments = gitHubClient.issues.listCommentsByIssue(
pr.base!.repo!.slug(),
pr.number!,
);
await for (IssueComment comment in comments) {
if (comment.body != null && comment.body!.contains(message)) {
return true;
}
}
return false;
}
String _getWrongBaseComment({
required String base,
required String defaultBranch,
}) {
final messageTemplate = config.wrongBaseBranchPullRequestMessage;
return messageTemplate
.replaceAll('{{target_branch}}', base)
.replaceAll('{{default_branch}}', defaultBranch);
}
PullRequestEvent? _getPullRequestEvent(String request) {
try {
return PullRequestEvent.fromJson(
json.decode(request) as Map<String, dynamic>,
);
} on FormatException {
return null;
}
}
/// Returns true if the changes to [file] are all code comments.
///
/// If that cannot be determined with confidence, returns false. False
/// negatives (e.g., for /* */-style multi-line comments) should be expected.
bool _allChangesAreCodeComments(PullRequestFile file) {
final linesAdded = file.additionsCount;
final linesDeleted = file.deletionsCount;
final patch = file.patch;
// If information is missing, err or the side of assuming it's a non-comment
// change.
if (linesAdded == null || linesDeleted == null || patch == null) {
return false;
}
final filename = file.filename!;
final extension =
filename.contains('.') ? filename.split('.').last.toLowerCase() : null;
if (extension == null || !knownCommentCodeExtensions.contains(extension)) {
return false;
}
// Only handles single-line comments; identifying multi-line comments
// would require the full file and non-trivial parsing. Also doesn't handle
// end-of-line comments (e.g., "int x = 0; // Info about x").
final commentRegex = RegExp(r'^[+-]\s*//');
final onlyWhitespaceRegex = RegExp(r'^[+-]\s*$');
for (var line in patch.split('\n')) {
if (!line.startsWith('+') && !line.startsWith('-')) {
continue;
}
if (onlyWhitespaceRegex.hasMatch(line)) {
// whitespace only changes don't require tests
continue;
}
if (!commentRegex.hasMatch(line)) {
return false;
}
}
return true;
}
}
typedef PullRequestLabelProcessorProvider =
PullRequestLabelProcessor Function({
required Config config,
required GithubService githubService,
required PullRequest pullRequest,
});
class PullRequestLabelProcessor {
PullRequestLabelProcessor({
required this.config,
required this.githubService,
required this.pullRequest,
}) : slug = pullRequest.base!.repo!.slug(),
prNumber = pullRequest.number!;
static const kEmergencyLabelEducation = '''
Detected the `emergency` label.
If you add the `autosubmit` label, the bot will wait until all presubmits pass but ignore the tree status, allowing fixes for tree breakages while still validating that they don't break any existing presubmits.
The "Merge" button is also unlocked. To bypass presubmits as well as the tree status, press the GitHub "Add to Merge Queue".
''';
final Config config;
final GithubService githubService;
final PullRequest pullRequest;
final RepositorySlug slug;
final int prNumber;
late final String logCrumb =
'$PullRequestLabelProcessor($slug/pull/$prNumber)';
void logInfo(Object? message) {
log.info('$logCrumb: $message');
}
void logSevere(Object? message, {Object? error, StackTrace? stackTrace}) {
log.severe('$logCrumb: $message', error, stackTrace);
}
Future<void> processLabels() async {
final hasEmergencyLabel =
pullRequest.labels?.any(
(label) => label.name == Config.kEmergencyLabel,
) ??
false;
if (hasEmergencyLabel) {
// The merge guard can be unlocked without approval checks because:
//
// * For manual merges the GitHub repo settings already require minimum
// approvals before the PR can be submitted.
// * For `autosubmit` label Cocoon has the [Approval] validation that
// checks approvasl before attempting to merge the PR.
await _unlockMergeQueueGuardForEmergency();
} else {
logInfo('no emergency label; moving on.');
}
}
Future<void> _unlockMergeQueueGuardForEmergency() async {