@@ -71,6 +71,51 @@ export async function getRefFromGithubPr({
7171 return ref ;
7272}
7373
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+
74119type DispatchOptions = {
75120 githubToken : string ;
76121 ref : string ;
@@ -118,29 +163,13 @@ export async function dispatchAndWait({
118163 ) ;
119164
120165 console . log ( `Dispatched run #${ run . run_number } (${ run . html_url } )` ) ;
121- try {
122- for (
123- const start = new Date ( ) ;
124- new Date ( ) . getTime ( ) - start . getTime ( ) < WATCH_POLL_TIMEOUT_MS ;
125-
126- ) {
127- const {
128- data : { status, conclusion } ,
129- } = await octokit . rest . actions . getWorkflowRun ( {
130- owner : GITHUB_OWNER ,
131- repo : GITHUB_REPO ,
132- run_id : run . id ,
133- } ) ;
134- console . log ( `Status: ${ status || 'null' } ` ) ;
135- if ( status === 'completed' ) {
136- assert . equal ( conclusion , 'success' ) ;
137- }
138- await new Promise ( ( resolve ) => setTimeout ( resolve , watchPollDelayMs ) ) ;
139- }
140- } finally {
141- console . log ( `Run completed: ${ run . html_url } ` ) ;
142- }
143- throw new Error (
144- `Run did not complete successfully within ${ WATCH_POLL_TIMEOUT_MS } ms: See ${ run . html_url } for details.`
145- ) ;
166+ const status = await pollToCompletion ( {
167+ octokit,
168+ runId : run . id ,
169+ watchTimeoutMs : WATCH_POLL_TIMEOUT_MS ,
170+ watchPollDelayMs,
171+ } ) ;
172+
173+ console . log ( `Run completed: ${ run . html_url } ` ) ;
174+ assert . equal ( status , 'success' ) ;
146175}
0 commit comments