1+ import assert from 'node:assert/strict' ;
12import crypto from 'node:crypto' ;
23
34import * as github from '@actions/github' ;
@@ -70,12 +71,58 @@ export async function getRefFromGithubPr({
7071 return ref ;
7172}
7273
74+ type PollToCompletionOptions = {
75+ octokit : ReturnType < typeof github . getOctokit > ;
76+ runId : number ;
77+ watchTimeoutMs : number ;
78+ watchPollDelayMs : number ;
79+ } ;
80+
81+ async function pollToCompletion ( {
82+ octokit,
83+ runId,
84+ watchTimeoutMs,
85+ watchPollDelayMs,
86+ } : PollToCompletionOptions ) : Promise < string > {
87+ for (
88+ const start = new Date ( ) ;
89+ new Date ( ) . getTime ( ) - start . getTime ( ) < watchTimeoutMs ;
90+
91+ ) {
92+ const {
93+ data : { status, conclusion } ,
94+ } = await octokit . rest . actions . getWorkflowRun ( {
95+ owner : GITHUB_OWNER ,
96+ repo : GITHUB_REPO ,
97+ run_id : runId ,
98+ } ) ;
99+ console . log ( `Status: ${ status || 'null' } ` ) ;
100+ if ( status === 'completed' ) {
101+ assert (
102+ typeof conclusion === 'string' ,
103+ 'Expected conclusion when completed'
104+ ) ;
105+ return conclusion ;
106+ }
107+ await new Promise ( ( resolve ) => setTimeout ( resolve , watchPollDelayMs ) ) ;
108+ }
109+
110+ // Cancel the run before timing out
111+ await octokit . rest . actions . cancelWorkflowRun ( {
112+ owner : GITHUB_OWNER ,
113+ repo : GITHUB_REPO ,
114+ run_id : runId ,
115+ } ) ;
116+ return 'timeout' ;
117+ }
118+
73119type DispatchOptions = {
74120 githubToken : string ;
75121 ref : string ;
76122 bucketName : string ;
77123 bucketKeyPrefix : string ;
78124 devVersion ?: string ;
125+ evergreenTaskUrl ?: string ;
79126
80127 /**
81128 * Delay in milliseconds to wait between requests when polling while watching the run.
@@ -89,6 +136,7 @@ export async function dispatchAndWait({
89136 devVersion,
90137 bucketName,
91138 bucketKeyPrefix,
139+ evergreenTaskUrl,
92140 watchPollDelayMs = 5000 ,
93141} : DispatchOptions ) {
94142 const octokit = github . getOctokit ( githubToken ) ;
@@ -103,6 +151,7 @@ export async function dispatchAndWait({
103151 dev_version : devVersion ,
104152 bucket_name : bucketName ,
105153 bucket_key_prefix : bucketKeyPrefix ,
154+ evergreen_task_url : evergreenTaskUrl ,
106155 nonce,
107156 } ,
108157 } ) ;
@@ -114,27 +163,13 @@ export async function dispatchAndWait({
114163 ) ;
115164
116165 console . log ( `Dispatched run #${ run . run_number } (${ run . html_url } )` ) ;
117- for (
118- const start = new Date ( ) ;
119- new Date ( ) . getTime ( ) - start . getTime ( ) < WATCH_POLL_TIMEOUT_MS ;
166+ const status = await pollToCompletion ( {
167+ octokit,
168+ runId : run . id ,
169+ watchTimeoutMs : WATCH_POLL_TIMEOUT_MS ,
170+ watchPollDelayMs,
171+ } ) ;
120172
121- ) {
122- const {
123- data : { status, conclusion } ,
124- } = await octokit . rest . actions . getWorkflowRun ( {
125- owner : GITHUB_OWNER ,
126- repo : GITHUB_REPO ,
127- run_id : run . id ,
128- } ) ;
129- console . log (
130- `Status = ${ status || 'null' } , conclusion = ${ conclusion || 'null' } `
131- ) ;
132- if ( status === 'completed' && conclusion === 'success' ) {
133- return ;
134- }
135- await new Promise ( ( resolve ) => setTimeout ( resolve , watchPollDelayMs ) ) ;
136- }
137- throw new Error (
138- `Run did not complete successfully within ${ WATCH_POLL_TIMEOUT_MS } ms: See ${ run . html_url } for details.`
139- ) ;
173+ console . log ( `Run completed: ${ run . html_url } ` ) ;
174+ assert . equal ( status , 'success' , "Expected a 'success' conclusion" ) ;
140175}
0 commit comments