@@ -161,6 +161,9 @@ function withAvatar(
161161 return avatarUrl === null ? actor : { ...actor , avatarUrl } ;
162162}
163163
164+ const workflowRunIdOf = ( url : string | null ) =>
165+ url ?. match ( / \/ a c t i o n s \/ r u n s \/ ( \d + ) (?: \/ | $ ) / ) ?. [ 1 ] ?? null ;
166+
164167function withWorkflowApprovals (
165168 checks : ReadonlyArray < PullRequestCheck > ,
166169 runs : ReadonlyArray < GitHubWorkflowRunApproval > ,
@@ -169,8 +172,8 @@ function withWorkflowApprovals(
169172 const representedRunIds = new Set < number > ( ) ;
170173 for ( const check of checks ) {
171174 if ( check . status !== "action-required" || check . url === null ) continue ;
172- const id = check . url . match ( / \/ a c t i o n s \/ r u n s \/ ( \d + ) (?: \/ | $ ) / ) ?. [ 1 ] ;
173- if ( id !== undefined ) representedRunIds . add ( Number ( id ) ) ;
175+ const id = workflowRunIdOf ( check . url ) ;
176+ if ( id !== null ) representedRunIds . add ( Number ( id ) ) ;
174177 }
175178 const approvalChecks = runs
176179 . filter ( ( run ) => ! representedRunIds . has ( run . id ) )
@@ -197,6 +200,30 @@ function withWorkflowApprovals(
197200 ] ;
198201}
199202
203+ // GitHub adds a run's check runs only as its jobs start, so a queued run can have none yet.
204+ function withUnfinishedWorkflowRuns (
205+ checks : ReadonlyArray < PullRequestCheck > ,
206+ runs : ReadonlyArray < GitHubWorkflowRunApproval > ,
207+ ) : ReadonlyArray < PullRequestCheck > {
208+ const runningRunIds = new Set < number > ( ) ;
209+ for ( const check of checks ) {
210+ if ( check . status !== "pending" && check . status !== "action-required" ) continue ;
211+ const id = workflowRunIdOf ( check . url ) ;
212+ if ( id !== null ) runningRunIds . add ( Number ( id ) ) ;
213+ }
214+ return [
215+ ...checks ,
216+ ...runs
217+ . filter ( ( run ) => ! runningRunIds . has ( run . id ) )
218+ . map ( ( run ) : PullRequestCheck => ( {
219+ name : run . name ,
220+ status : "pending" ,
221+ description : "This workflow run has not finished." ,
222+ url : run . url ,
223+ } ) ) ,
224+ ] ;
225+ }
226+
200227/** App logins have no profile page; guessed avatar URLs 404. */
201228export function loginAvatarUrl ( login : string , host : string ) : string | null {
202229 return / ^ [ a - z 0 - 9 ] [ a - z 0 - 9 - ] { 0 , 38 } $ / iu. test ( login ) ? `https://${ host } /${ login } .png?size=80` : null ;
@@ -338,11 +365,24 @@ export const make = Effect.gen(function* () {
338365 } ) ,
339366 )
340367 ) . pipe (
341- Effect . map ( ( workflowApprovals ) => ( {
368+ Effect . zip (
369+ pullRequest . state !== "open" || pullRequest . headSha == null
370+ ? Effect . succeed ( [ ] as ReadonlyArray < GitHubWorkflowRunApproval > )
371+ : cli . listUnfinishedWorkflowRuns ( { ...input , headSha : pullRequest . headSha } ) . pipe (
372+ Effect . catchIf (
373+ ( error ) =>
374+ error . _tag !== "GitHubApiRateLimitError" &&
375+ error . _tag !== "SourceControlRateLimitPausedError" ,
376+ ( ) => Effect . succeed ( [ ] as ReadonlyArray < GitHubWorkflowRunApproval > ) ,
377+ ) ,
378+ ) ,
379+ { concurrent : true } ,
380+ ) ,
381+ Effect . map ( ( [ workflowApprovals , unfinishedRuns ] ) => ( {
342382 ...pullRequest ,
343383 author : withAvatar ( pullRequest . author , new Map ( ) , input . host ) ,
344384 checks : withWorkflowApprovals (
345- pullRequest . checks ,
385+ withUnfinishedWorkflowRuns ( pullRequest . checks , unfinishedRuns ) ,
346386 workflowApprovals . runs ,
347387 workflowApprovals . unavailable ,
348388 ) ,
0 commit comments