Skip to content

Commit 80b2cb4

Browse files
authored
Reset Dashboard Checks to In Progress when re-run all failed jobs (#5130)
fix of the github error: ``` Error: GitHub Error: Message: Invalid request. For 'properties/name', nil is not a string. is not a member of ["success", "failure", "neutral", "cancelled", "timed_out", "action_required", "skipped"]. For 'properties/conclusion', nil is not a string. For 'properties/completed_at', nil is not a string. ``` when reset check run status back to in progress in scope of: flutter/flutter#191005
1 parent f330c27 commit 80b2cb4

9 files changed

Lines changed: 400 additions & 494 deletions

File tree

‎app_dart/lib/src/foundation/github_checks_util.dart‎

Lines changed: 0 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5-
import 'dart:convert';
65
import 'dart:core';
76

87
import 'package:cocoon_server/logging.dart';
@@ -97,59 +96,6 @@ class GithubChecksUtil {
9796
);
9897
}
9998

100-
/// Sends a request to github checks api to reset a [CheckRun] to in_progress.
101-
Future<void> resetCheckRun(
102-
Config config,
103-
github.RepositorySlug slug,
104-
github.CheckRun checkRun, {
105-
github.CheckRunStatus status = github.CheckRunStatus.inProgress,
106-
String? detailsUrl,
107-
github.CheckRunOutput? output,
108-
List<github.CheckRunAction>? actions,
109-
}) async {
110-
const r = RetryOptions(maxAttempts: 3, delayFactor: Duration(seconds: 2));
111-
return r.retry(
112-
() async {
113-
final gitHubClient = await config.createGitHubClient(slug: slug);
114-
await _resetCheckRun(
115-
gitHubClient,
116-
slug,
117-
checkRun,
118-
status: status,
119-
detailsUrl: detailsUrl,
120-
output: output,
121-
);
122-
},
123-
retryIf: (Exception e) => e is github.GitHubError || e is SocketException,
124-
);
125-
}
126-
127-
Future<github.CheckRun> _resetCheckRun(
128-
github.GitHub gitHubClient,
129-
github.RepositorySlug slug,
130-
github.CheckRun checkRunToUpdate, {
131-
String? name,
132-
String? detailsUrl,
133-
github.CheckRunStatus status = github.CheckRunStatus.inProgress,
134-
github.CheckRunOutput? output,
135-
}) async {
136-
return gitHubClient.requestJson<Map<String, dynamic>, github.CheckRun>(
137-
'PATCH',
138-
'/repos/${slug.fullName}/check-runs/${checkRunToUpdate.id}',
139-
statusCode: github.StatusCodes.OK,
140-
preview: 'application/vnd.github.antiope-preview+json',
141-
body: jsonEncode(<String, dynamic>{
142-
'name': name,
143-
'details_url': detailsUrl,
144-
'status': status,
145-
'conclusion': null,
146-
'completed_at': null,
147-
'output': output,
148-
}),
149-
convert: github.CheckRun.fromJson,
150-
);
151-
}
152-
15399
Future<github.CheckRun> getCheckRun(
154100
Config config,
155101
github.RepositorySlug slug,

‎app_dart/lib/src/model/github/checks.dart‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import 'dart:convert';
66

77
import 'package:github/github.dart'
88
show CheckSuite, PullRequest, Repository, User;
9+
import 'package:github/github.dart' as github;
910
import 'package:github/hooks.dart' show HookEvent;
1011
import 'package:json_annotation/json_annotation.dart';
1112

@@ -48,6 +49,17 @@ class CheckRun {
4849

4950
factory CheckRun.fromJson(Map<String, dynamic> input) =>
5051
_$CheckRunFromJson(input);
52+
53+
github.CheckRun toGithubCheckRun({DateTime? startedAt}) =>
54+
github.CheckRun.fromJson(<String, dynamic>{
55+
'id': id,
56+
'name': name,
57+
'head_sha': headSha,
58+
'conclusion': conclusion,
59+
'started_at': (startedAt ?? DateTime.now().toUtc()).toIso8601String(),
60+
'check_suite': <String, dynamic>{'id': checkSuite?.id},
61+
});
62+
5163
final int? id;
5264
final String? headSha;
5365
final String? conclusion;

‎app_dart/lib/src/service/luci_build_service.dart‎

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,6 @@ class LuciBuildService {
450450
// need to re-request the check run before updating it to in progress.
451451
final isRerun = targets.values.first > 1;
452452
if (isUnifiedCheckRunFlow && dashboardChecks != null) {
453-
var shouldUpdateCheckRun = !isRerun;
454453
if (isRerun && stage != null) {
455454
try {
456455
final presubmitGuardDoc = await _firestore.getDocument(
@@ -471,7 +470,6 @@ class LuciBuildService {
471470
slug,
472471
checkRunId: dashboardChecks.id!,
473472
);
474-
shouldUpdateCheckRun = true;
475473
}
476474
} catch (e, s) {
477475
// We are not going to block on this error.
@@ -482,32 +480,6 @@ class LuciBuildService {
482480
);
483481
}
484482
}
485-
486-
if (shouldUpdateCheckRun) {
487-
try {
488-
log.info(
489-
'Resetting dashboard checks ${dashboardChecks.id} to in progress',
490-
);
491-
await _githubChecksUtil.resetCheckRun(
492-
_config,
493-
slug,
494-
dashboardChecks,
495-
output: const CheckRunOutput(
496-
title: Config.kDashboardCheckName,
497-
summary: Scheduler.kDashboardChecksDescription,
498-
),
499-
detailsUrl:
500-
'https://flutter-dashboard.appspot.com/#/presubmit?repo=${slug.name}&sha=$commitSha',
501-
);
502-
} catch (e, s) {
503-
// We are not going to block on this error.
504-
log.warn(
505-
'Failed to reset dashboard checks for PR# ${pullRequest.number} to in progress',
506-
e,
507-
s,
508-
);
509-
}
510-
}
511483
}
512484

513485
return targets.keys.toList();

‎app_dart/lib/src/service/scheduler.dart‎

Lines changed: 110 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1702,101 +1702,125 @@ $stacktrace
17021702
);
17031703
final name = checkRunEvent.checkRun!.name;
17041704
var success = false;
1705-
if (name == Config.kMergeQueueLockName ||
1706-
name == Config.kDashboardCheckName) {
1707-
final slug = checkRunEvent.repository!.slug();
1708-
final checkSuiteId = checkRunEvent.checkRun!.checkSuite!.id!;
1709-
log.debug(
1710-
'$logCrumb: Requested re-run of "$name" for '
1711-
'$slug / $checkSuiteId - ignoring',
1712-
);
1713-
success = true;
1714-
} else if (name == Config.kCiYamlCheckName) {
1715-
// The CheckRunEvent.checkRun.pullRequests array is empty for this
1716-
// event, so we need to find the matching pull request.
1717-
final slug = checkRunEvent.repository!.slug();
1718-
final headSha = checkRunEvent.checkRun!.headSha!;
1719-
final checkSuiteId = checkRunEvent.checkRun!.checkSuite!.id!;
1720-
final pullRequest = await _githubChecksService.findMatchingPullRequest(
1721-
slug,
1722-
headSha,
1723-
checkSuiteId,
1724-
);
1725-
if (pullRequest != null) {
1726-
log.debug('Matched PR: ${pullRequest.number} Repo: ${slug.fullName}');
1727-
await triggerPresubmitTargets(pullRequest: pullRequest);
1705+
1706+
final slug = checkRunEvent.repository!.slug();
1707+
1708+
switch (name) {
1709+
case Config.kMergeQueueLockName:
1710+
final checkSuiteId = checkRunEvent.checkRun!.checkSuite!.id!;
1711+
log.debug(
1712+
'$logCrumb: Requested re-run of "$name" for '
1713+
'$slug / $checkSuiteId - ignoring',
1714+
);
17281715
success = true;
1729-
} else {
1730-
log.warn('No matching PR found for head_sha in check run event.');
1731-
}
1732-
} else {
1733-
try {
1734-
final slug = checkRunEvent.repository!.slug();
1735-
final sha = checkRunEvent.checkRun!.headSha!;
1736-
1737-
// Only merged commits are added to the Database.
1738-
// If a commit is found, this must be a postsubmit checkrun.
1739-
final fsCommit = await fs.Commit.tryFromFirestoreBySha(
1740-
_firestore,
1741-
sha: sha,
1716+
case Config.kDashboardCheckName:
1717+
try {
1718+
log.info(
1719+
'Resetting dashboard checks ${checkRunEvent.checkRun!.id} to in progress',
1720+
);
1721+
await _githubChecksService.githubChecksUtil.updateCheckRun(
1722+
_config,
1723+
slug,
1724+
checkRunEvent.checkRun!.toGithubCheckRun(),
1725+
status: CheckRunStatus.inProgress,
1726+
output: const CheckRunOutput(
1727+
title: Config.kDashboardCheckName,
1728+
summary: Scheduler.kDashboardChecksDescription,
1729+
),
1730+
);
1731+
success = true;
1732+
} catch (e, s) {
1733+
// We are not going to block on this error.
1734+
log.warn(
1735+
'Failed to reset dashboard checks ${checkRunEvent.checkRun!.id} to in progress',
1736+
e,
1737+
s,
1738+
);
1739+
}
1740+
case Config.kCiYamlCheckName:
1741+
// The CheckRunEvent.checkRun.pullRequests array is empty for this
1742+
// event, so we need to find the matching pull request.
1743+
final headSha = checkRunEvent.checkRun!.headSha!;
1744+
final checkSuiteId = checkRunEvent.checkRun!.checkSuite!.id!;
1745+
final pullRequest = await _githubChecksService.findMatchingPullRequest(
1746+
slug,
1747+
headSha,
1748+
checkSuiteId,
17421749
);
1750+
if (pullRequest != null) {
1751+
log.debug('Matched PR: ${pullRequest.number} Repo: ${slug.fullName}');
1752+
await triggerPresubmitTargets(pullRequest: pullRequest);
1753+
success = true;
1754+
} else {
1755+
log.warn('No matching PR found for head_sha in check run event.');
1756+
}
1757+
case _:
1758+
try {
1759+
final sha = checkRunEvent.checkRun!.headSha!;
17431760

1744-
// TODO(matanlurey): Refactor into its own branch.
1745-
// https://github.com/flutter/flutter/issues/167211.
1746-
final isPresubmit = fsCommit == null;
1747-
if (isPresubmit) {
1748-
log.debug('Rescheduling presubmit build for $name');
1749-
final pullRequest = await PrCheckRuns.findPullRequestForSha(
1761+
// Only merged commits are added to the Database.
1762+
// If a commit is found, this must be a postsubmit checkrun.
1763+
final fsCommit = await fs.Commit.tryFromFirestoreBySha(
17501764
_firestore,
1751-
sha,
1765+
sha: sha,
17521766
);
1753-
if (pullRequest == null) {
1754-
return ProcessCheckRunResult.userError(
1755-
'Asked to reschedule presubmits for unknown sha/PR: $sha',
1756-
);
1757-
}
1758-
await reRunTargets(slug, pullRequest, [name!]);
1759-
} else {
1760-
log.debug('Rescheduling postsubmit build.');
1761-
1762-
final checkName = name;
1763-
final fs.Task fsTask;
17641767

1765-
// Query the lastest run of the `checkName` againt commit `sha`.
1766-
final fsTasks = await _firestore.queryRecentTasks(
1767-
limit: 1,
1768-
commitSha: fsCommit.sha,
1769-
name: checkName,
1770-
);
1771-
if (fsTasks.isEmpty) {
1772-
throw StateError('Expected 1+ tasks for $checkName');
1768+
// TODO(matanlurey): Refactor into its own branch.
1769+
// https://github.com/flutter/flutter/issues/167211.
1770+
final isPresubmit = fsCommit == null;
1771+
if (isPresubmit) {
1772+
log.debug('Rescheduling presubmit build for $name');
1773+
final pullRequest = await PrCheckRuns.findPullRequestForSha(
1774+
_firestore,
1775+
sha,
1776+
);
1777+
if (pullRequest == null) {
1778+
return ProcessCheckRunResult.userError(
1779+
'Asked to reschedule presubmits for unknown sha/PR: $sha',
1780+
);
1781+
}
1782+
await reRunTargets(slug, pullRequest, [name!]);
1783+
} else {
1784+
log.debug('Rescheduling postsubmit build.');
1785+
1786+
final checkName = name;
1787+
final fs.Task fsTask;
1788+
1789+
// Query the lastest run of the `checkName` againt commit `sha`.
1790+
final fsTasks = await _firestore.queryRecentTasks(
1791+
limit: 1,
1792+
commitSha: fsCommit.sha,
1793+
name: checkName,
1794+
);
1795+
if (fsTasks.isEmpty) {
1796+
throw StateError('Expected 1+ tasks for $checkName');
1797+
}
1798+
fsTask = fsTasks.first;
1799+
1800+
log.debug('Latest firestore task is $fsTask');
1801+
final ciYaml = await _ciYamlFetcher.getCiYamlByCommit(
1802+
fsCommit.toRef(),
1803+
postsubmit: true,
1804+
);
1805+
final target = ciYaml.postsubmitTargets().singleWhere(
1806+
(target) => target.name == fsTask.taskName,
1807+
);
1808+
await _luciBuildService.reschedulePostsubmitBuildUsingCheckRunEvent(
1809+
checkRunEvent,
1810+
commit: fsCommit.toRef(),
1811+
task: fsTask,
1812+
target: target,
1813+
);
17731814
}
1774-
fsTask = fsTasks.first;
17751815

1776-
log.debug('Latest firestore task is $fsTask');
1777-
final ciYaml = await _ciYamlFetcher.getCiYamlByCommit(
1778-
fsCommit.toRef(),
1779-
postsubmit: true,
1780-
);
1781-
final target = ciYaml.postsubmitTargets().singleWhere(
1782-
(target) => target.name == fsTask.taskName,
1783-
);
1784-
await _luciBuildService.reschedulePostsubmitBuildUsingCheckRunEvent(
1785-
checkRunEvent,
1786-
commit: fsCommit.toRef(),
1787-
task: fsTask,
1788-
target: target,
1789-
);
1816+
success = true;
1817+
} on NoBuildFoundException {
1818+
log.warn('No build found to reschedule.');
1819+
} on FormatException catch (e) {
1820+
// See https://github.com/flutter/flutter/issues/165018.
1821+
log.info('CheckName: $name failed due to user error: $e');
1822+
return ProcessCheckRunResult.userError('$e');
17901823
}
1791-
1792-
success = true;
1793-
} on NoBuildFoundException {
1794-
log.warn('No build found to reschedule.');
1795-
} on FormatException catch (e) {
1796-
// See https://github.com/flutter/flutter/issues/165018.
1797-
log.info('CheckName: $name failed due to user error: $e');
1798-
return ProcessCheckRunResult.userError('$e');
1799-
}
18001824
}
18011825

18021826
log.debug('CheckName: $name State: $success');

0 commit comments

Comments
 (0)