1+ import assert from 'node:assert/strict' ;
12import crypto from 'node:crypto' ;
23
34import * as github from '@actions/github' ;
@@ -70,12 +71,59 @@ 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+ githubPrNumber ?: string ;
126+ evergreenTaskUrl ?: string ;
79127
80128 /**
81129 * Delay in milliseconds to wait between requests when polling while watching the run.
@@ -89,6 +137,8 @@ export async function dispatchAndWait({
89137 devVersion,
90138 bucketName,
91139 bucketKeyPrefix,
140+ githubPrNumber,
141+ evergreenTaskUrl,
92142 watchPollDelayMs = 5000 ,
93143} : DispatchOptions ) {
94144 const octokit = github . getOctokit ( githubToken ) ;
@@ -103,6 +153,8 @@ export async function dispatchAndWait({
103153 dev_version : devVersion ,
104154 bucket_name : bucketName ,
105155 bucket_key_prefix : bucketKeyPrefix ,
156+ github_pr_number : githubPrNumber ,
157+ evergreen_task_url : evergreenTaskUrl ,
106158 nonce,
107159 } ,
108160 } ) ;
@@ -114,27 +166,13 @@ export async function dispatchAndWait({
114166 ) ;
115167
116168 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 ;
169+ const status = await pollToCompletion ( {
170+ octokit,
171+ runId : run . id ,
172+ watchTimeoutMs : WATCH_POLL_TIMEOUT_MS ,
173+ watchPollDelayMs,
174+ } ) ;
120175
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- ) ;
176+ console . log ( `Run completed: ${ run . html_url } ` ) ;
177+ assert . equal ( status , 'success' , "Expected a 'success' conclusion" ) ;
140178}
0 commit comments