@@ -37,17 +37,21 @@ final class UnifiedCheckRun {
3737 config.flags.isUnifiedCheckRunFlowEnabledForUser (
3838 pullRequest.user! .login! ,
3939 )) {
40+ // Create the UnifiedCheckRun and UnifiedCheckRunBuilds.
4041 log.info (
4142 'Storing UnifiedCheckRun data for ${slug .fullName }#${pullRequest .number } as it enabled for user ${pullRequest .user !.login }.' ,
4243 );
43- // Create the UnifiedCheckRun and UnifiedCheckRunBuilds.
44+ // We store the creation time of the guard since there might be several
45+ // guards for the same PR created and each new one created after previous
46+ // was succeeded so we are interested in a state of the last one.
47+ final creationTime = DateTime .now ().toUtc ().microsecondsSinceEpoch;
4448 final guard = PresubmitGuard (
4549 checkRun: checkRun,
4650 commitSha: sha,
4751 slug: slug,
4852 pullRequestId: pullRequest.number! ,
4953 stage: stage,
50- creationTime: pullRequest.createdAt ! .microsecondsSinceEpoch ,
54+ creationTime: creationTime ,
5155 author: pullRequest.user! .login! ,
5256 remainingBuilds: tasks.length,
5357 failedBuilds: 0 ,
@@ -58,7 +62,7 @@ final class UnifiedCheckRun {
5862 PresubmitCheck .init (
5963 buildName: task,
6064 checkRunId: checkRun.id! ,
61- creationTime: pullRequest.createdAt ! .microsecondsSinceEpoch ,
65+ creationTime: creationTime ,
6266 ),
6367 ];
6468 await firestoreService.writeViaTransaction (
@@ -89,63 +93,67 @@ final class UnifiedCheckRun {
8993 log.info ('$logCrumb Re-Running failed checks.' );
9094 final transaction = await firestoreService.beginTransaction ();
9195
92- final guards = await getPresubmitGuardsForCheckRun (
96+ // New guard created only if previous is succeeded so failed checks might be
97+ // only in last guard.
98+ final guard = await getLastPresubmitGuardForCheckRun (
9399 firestoreService: firestoreService,
94100 slug: slug,
95101 pullRequestId: pullRequestId,
96102 checkRunId: checkRunId,
97103 transaction: transaction,
98104 );
99105
100- for (final guard in guards) {
101- // Copy the failed build names to a local variable to avoid losing the
102- // failed build names after resetting the failed guard.builds.
103- final failedBuildNames = guard.failedBuildNames;
104- if (failedBuildNames.isNotEmpty) {
105- guard.failedBuilds = 0 ;
106- guard.remainingBuilds = failedBuildNames.length;
107- final builds = guard.builds;
108- for (final buildName in failedBuildNames) {
109- builds[buildName] = TaskStatus .waitingForBackfill;
110- }
111- guard.builds = builds;
112- final checks = [
113- for (final buildName in failedBuildNames)
114- PresubmitCheck .init (
115- buildName: buildName,
116- checkRunId: checkRunId,
117- creationTime: DateTime .now ().toUtc ().microsecondsSinceEpoch,
118- attemptNumber:
119- ((await getLatestPresubmitCheck (
120- firestoreService: firestoreService,
121- checkRunId: checkRunId,
122- buildName: buildName,
123- transaction: transaction,
124- ))? .attemptNumber ??
125- 0 ) +
126- 1 , // Increment the latest attempt number.
127- ),
128- ];
129- try {
130- final response = await firestoreService.commit (
131- transaction,
132- documentsToWrites ([...checks, guard]),
133- );
134- log.info (
135- '$logCrumb : results = ${response .writeResults ?.map ((e ) => e .toJson ())}' ,
136- );
137- return FailedChecksForRerun (
138- checkRunGuard: guard.checkRun,
139- checkNames: failedBuildNames,
140- stage: guard.stage,
141- );
142- } catch (e) {
143- log.info ('$logCrumb : failed to update presubmit check' , e);
144- rethrow ;
145- }
106+ if (guard == null ) {
107+ return null ;
108+ }
109+
110+ // Copy the failed build names to a local variable to avoid losing the
111+ // failed build names after resetting the failed guard.builds.
112+ final creationTime = DateTime .now ().toUtc ().microsecondsSinceEpoch;
113+ final failedBuildNames = guard.failedBuildNames;
114+ if (failedBuildNames.isNotEmpty) {
115+ guard.failedBuilds = 0 ;
116+ guard.remainingBuilds = failedBuildNames.length;
117+ final builds = guard.builds;
118+ for (final buildName in failedBuildNames) {
119+ builds[buildName] = TaskStatus .waitingForBackfill;
120+ }
121+ guard.builds = builds;
122+ final checks = [
123+ for (final buildName in failedBuildNames)
124+ PresubmitCheck .init (
125+ buildName: buildName,
126+ checkRunId: checkRunId,
127+ creationTime: creationTime,
128+ attemptNumber:
129+ ((await getLatestPresubmitCheck (
130+ firestoreService: firestoreService,
131+ checkRunId: checkRunId,
132+ buildName: buildName,
133+ transaction: transaction,
134+ ))? .attemptNumber ??
135+ 0 ) +
136+ 1 , // Increment the latest attempt number.
137+ ),
138+ ];
139+ try {
140+ final response = await firestoreService.commit (
141+ transaction,
142+ documentsToWrites ([...checks, guard]),
143+ );
144+ log.info (
145+ '$logCrumb : results = ${response .writeResults ?.map ((e ) => e .toJson ())}' ,
146+ );
147+ return FailedChecksForRerun (
148+ checkRunGuard: guard.checkRun,
149+ checkNames: failedBuildNames,
150+ stage: guard.stage,
151+ );
152+ } catch (e) {
153+ log.info ('$logCrumb : failed to update presubmit check' , e);
154+ rethrow ;
146155 }
147156 }
148- return null ;
149157 }
150158
151159 /// Returns _all_ checks running against the specified github [checkRunId] .
@@ -203,19 +211,19 @@ final class UnifiedCheckRun {
203211 }
204212
205213 /// Returns [PresubmitGuard] s for the specified github [checkRunId] .
206- static Future <List < PresubmitGuard >> getPresubmitGuardsForCheckRun ({
214+ static Future <PresubmitGuard ?> getLastPresubmitGuardForCheckRun ({
207215 required FirestoreService firestoreService,
208216 required RepositorySlug slug,
209217 required int pullRequestId,
210218 required int checkRunId,
211219 Transaction ? transaction,
212220 }) async {
213- return await _queryPresubmitGuards (
221+ return ( await _queryPresubmitGuards (
214222 firestoreService: firestoreService,
215223 checkRunId: checkRunId,
216224 transaction: transaction,
217- orderMap : const { PresubmitGuard .fieldStage : kQueryOrderAscending} ,
218- );
225+ limit : 1 ,
226+ )).firstOrNull ;
219227 }
220228
221229 static Future <List <PresubmitGuard >> _queryPresubmitGuards ({
0 commit comments