Skip to content

Commit d731396

Browse files
Add an explicit checkrun which indicates that the CICD label needs to be added. (#5018)
It's very easy to forget to add the CICD label, and it isn't always clear that is what our checkruns are waiting on. This adds an explicit checkrun that says that we are waiting on the CICD label.
1 parent 63a9908 commit d731396

4 files changed

Lines changed: 169 additions & 0 deletions

File tree

app_dart/lib/src/request_handlers/github/webhook_subscription.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,10 +203,12 @@ final class GithubWebhookSubscription extends SubscriptionHandler {
203203
pullRequestEvent.changes!.base != null) {
204204
await _addCICDForRollersAndMembers(pullRequestEvent);
205205
}
206+
await scheduler.createAwaitingCicdLabelCheckRun(slug, pr.head!.sha!);
206207
await _checkForTests(pullRequestEvent);
207208
break;
208209
case 'opened':
209210
await _addCICDForRollersAndMembers(pullRequestEvent);
211+
await scheduler.createAwaitingCicdLabelCheckRun(slug, pr.head!.sha!);
210212
await _checkForTests(pullRequestEvent);
211213
await _tryReleaseApproval(pullRequestEvent);
212214
break;
@@ -224,6 +226,10 @@ final class GithubWebhookSubscription extends SubscriptionHandler {
224226
if (Config.kCicdLabelIds.contains(label.id) &&
225227
label.name == Config.kCicdLabel) {
226228
log.info('new CICD label added - scheduling tests');
229+
await scheduler.resolveAwaitingCicdLabelCheckRun(
230+
slug,
231+
pr.head!.sha!,
232+
);
227233
await _scheduleIfMergeable(pullRequestEvent);
228234
}
229235
}

app_dart/lib/src/service/scheduler.dart

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -841,6 +841,57 @@ $s
841841
);
842842
}
843843

844+
/// Creates a pending check run for "Awaiting CICD label" if it doesn't exist.
845+
Future<CheckRun?> createAwaitingCicdLabelCheckRun(
846+
RepositorySlug slug,
847+
String headSha,
848+
) async {
849+
final githubService = await _config.createGithubService(slug);
850+
final existing = await githubService.getCheckRunsFiltered(
851+
slug: slug,
852+
ref: headSha,
853+
checkName: 'Awaiting CICD label',
854+
);
855+
if (existing.isEmpty) {
856+
return _githubChecksService.githubChecksUtil.createCheckRun(
857+
_config,
858+
slug,
859+
headSha,
860+
'Awaiting CICD label',
861+
output: const CheckRunOutput(
862+
title: 'Awaiting CICD label',
863+
summary: 'This PR is awaiting the CICD label to start tests.',
864+
),
865+
);
866+
}
867+
return null;
868+
}
869+
870+
/// Resolves the pending check run for "Awaiting CICD label".
871+
Future<void> resolveAwaitingCicdLabelCheckRun(
872+
RepositorySlug slug,
873+
String headSha,
874+
) async {
875+
final githubService = await _config.createGithubService(slug);
876+
final guard = (await githubService.getCheckRunsFiltered(
877+
slug: slug,
878+
ref: headSha,
879+
checkName: 'Awaiting CICD label',
880+
)).singleOrNull;
881+
if (guard != null) {
882+
await githubService.updateCheckRun(
883+
slug: slug,
884+
checkRun: guard,
885+
status: CheckRunStatus.completed,
886+
conclusion: CheckRunConclusion.success,
887+
output: const CheckRunOutput(
888+
title: 'Awaiting CICD label',
889+
summary: 'CICD label added.',
890+
),
891+
);
892+
}
893+
}
894+
844895
/// Completes the "Merge Queue Guard" check run.
845896
///
846897
/// If the guard is guarding a merge group, this immediately makes the merge

app_dart/test/request_handlers/github/webhook_subscription_test.dart

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3859,5 +3859,114 @@ void foo() {
38593859
);
38603860
},
38613861
);
3862+
3863+
test(
3864+
'opened PR without CICD label creates "Awaiting CICD label" checkrun',
3865+
() async {
3866+
githubService.checkRunsMock = '{"total_count": 0, "check_runs": [{}]}';
3867+
tester.message = generateGithubWebhookMessage(
3868+
action: 'opened',
3869+
withCicdLabel: false,
3870+
);
3871+
3872+
await tester.post(webhook);
3873+
3874+
verify(
3875+
mockGithubChecksUtil.createCheckRun(
3876+
any,
3877+
any,
3878+
any,
3879+
'Awaiting CICD label',
3880+
output: anyNamed('output'),
3881+
),
3882+
).called(1);
3883+
},
3884+
);
3885+
3886+
test(
3887+
'opened PR with CICD label STILL creates "Awaiting CICD label" checkrun if not exists',
3888+
() async {
3889+
githubService.checkRunsMock = '{"total_count": 0, "check_runs": [{}]}';
3890+
tester.message = generateGithubWebhookMessage(
3891+
action: 'opened',
3892+
withCicdLabel: true,
3893+
);
3894+
3895+
await tester.post(webhook);
3896+
3897+
verify(
3898+
mockGithubChecksUtil.createCheckRun(
3899+
any,
3900+
any,
3901+
any,
3902+
'Awaiting CICD label',
3903+
output: anyNamed('output'),
3904+
),
3905+
).called(1);
3906+
},
3907+
);
3908+
3909+
test(
3910+
'edited PR without CICD label creates "Awaiting CICD label" checkrun if not exists',
3911+
() async {
3912+
githubService.checkRunsMock = '{"total_count": 0, "check_runs": [{}]}';
3913+
tester.message = generateGithubWebhookMessage(
3914+
action: 'edited',
3915+
withCicdLabel: false,
3916+
);
3917+
3918+
await tester.post(webhook);
3919+
3920+
verify(
3921+
mockGithubChecksUtil.createCheckRun(
3922+
any,
3923+
any,
3924+
any,
3925+
'Awaiting CICD label',
3926+
output: anyNamed('output'),
3927+
),
3928+
).called(1);
3929+
},
3930+
);
3931+
3932+
test(
3933+
'labeled event with CICD label resolves "Awaiting CICD label" checkrun',
3934+
() async {
3935+
githubService.checkRunsMock = '''{
3936+
"total_count": 1,
3937+
"check_runs": [
3938+
{
3939+
"id": 1,
3940+
"name": "Awaiting CICD label",
3941+
"head_sha": "be6ff099a4ee56e152a5fa2f37edd10f79d1269a",
3942+
"status": "in_progress",
3943+
"started_at": "2018-05-04T01:14:52Z",
3944+
"conclusion": null,
3945+
"check_suite": {
3946+
"id": 5
3947+
}
3948+
}
3949+
]
3950+
}''';
3951+
3952+
tester.message = generateGithubWebhookMessage(
3953+
action: 'labeled',
3954+
labeledLabel: labelEventMap,
3955+
withCicdLabel: true,
3956+
);
3957+
3958+
await tester.post(webhook);
3959+
3960+
expect(githubService.checkRunUpdates.length, 1);
3961+
expect(
3962+
githubService.checkRunUpdates.first.status,
3963+
CheckRunStatus.completed,
3964+
);
3965+
expect(
3966+
githubService.checkRunUpdates.first.conclusion,
3967+
CheckRunConclusion.success,
3968+
);
3969+
},
3970+
);
38623971
});
38633972
}

packages/cocoon_integration_test/lib/src/fakes/fake_github_service.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,9 @@ class FakeGithubService implements GithubService {
216216

217217
@override
218218
Future<List<CheckRun>> getCheckRuns(RepositorySlug slug, String ref) async {
219+
if (checkRunsMock == null) {
220+
return <CheckRun>[];
221+
}
219222
final rawBody = json.decode(checkRunsMock!) as Map<String, dynamic>;
220223
final checkRunsBody = rawBody['check_runs']! as List<dynamic>;
221224
final checkRuns = <CheckRun>[];

0 commit comments

Comments
 (0)