3
3
/* eslint-disable no-console */
4
4
5
5
const https = require ( 'https' )
6
+ const { setTimeout } = require ( 'timers/promises' )
6
7
7
8
const API_REPOSITORY_URL = 'https://api.github.com/repos/DataDog/test-environment'
8
9
const DISPATCH_WORKFLOW_URL = `${ API_REPOSITORY_URL } /actions/workflows/dd-trace-js-tests.yml/dispatches`
9
10
const GET_WORKFLOWS_URL = `${ API_REPOSITORY_URL } /actions/runs`
10
11
12
+ const MAX_ATTEMPTS = 30 * 60 / 5 // 30 minutes, polling every 5 seconds = 360 attempts
13
+
11
14
function getBranchUnderTest ( ) {
12
15
/**
13
16
* GITHUB_HEAD_REF is only set for `pull_request` events
@@ -106,17 +109,9 @@ const getCurrentWorkflowJobs = (runId) => {
106
109
} )
107
110
}
108
111
109
- const wait = ( timeToWaitMs ) => {
110
- return new Promise ( resolve => {
111
- setTimeout ( ( ) => {
112
- resolve ( )
113
- } , timeToWaitMs )
114
- } )
115
- }
116
-
117
112
async function main ( ) {
118
113
// Trigger JS GHA
119
- console . log ( 'Triggering CI Visibility test environment workflow.' )
114
+ console . log ( 'Triggering Test Optimization test environment workflow.' )
120
115
const httpResponseCode = await triggerWorkflow ( )
121
116
console . log ( 'GitHub API response code:' , httpResponseCode )
122
117
@@ -125,7 +120,7 @@ async function main () {
125
120
}
126
121
127
122
// Give some time for GH to process the request
128
- await wait ( 15000 )
123
+ await setTimeout ( 15000 )
129
124
130
125
// Get the run ID from the workflow we just triggered
131
126
const workflowsInProgress = await getWorkflowRunsInProgress ( )
@@ -143,30 +138,35 @@ async function main () {
143
138
console . log ( `Workflow URL: https://github.com/DataDog/test-environment/actions/runs/${ runId } ` )
144
139
145
140
// Wait an initial 1 minute, because we're sure it won't finish earlier
146
- await wait ( 60000 )
147
-
148
- // Poll every 5 seconds until we have a finished status
149
- await new Promise ( ( resolve , reject ) => {
150
- const intervalId = setInterval ( async ( ) => {
151
- const currentWorkflow = await getCurrentWorkflowJobs ( runId )
152
- const { jobs } = currentWorkflow
153
- const hasAnyJobFailed = jobs . some ( ( { status, conclusion } ) => status === 'completed' && conclusion !== 'success' )
154
- const hasEveryJobPassed = jobs . every (
155
- ( { status, conclusion } ) => status === 'completed' && conclusion === 'success'
156
- )
157
- if ( hasAnyJobFailed ) {
158
- reject ( new Error ( `Performance overhead test failed.
159
- Check https://github.com/DataDog/test-environment/actions/runs/${ runId } for more details.` ) )
160
- clearInterval ( intervalId )
161
- } else if ( hasEveryJobPassed ) {
162
- console . log ( 'Performance overhead test successful.' )
163
- resolve ( )
164
- clearInterval ( intervalId )
165
- } else {
166
- console . log ( `Workflow https://github.com/DataDog/test-environment/actions/runs/${ runId } is not finished yet.` )
167
- }
168
- } , 5000 )
169
- } )
141
+ await setTimeout ( 60000 )
142
+
143
+ // Poll every 5 seconds until we have a finished status, up to 30 minutes
144
+ for ( let attempt = 0 ; attempt < MAX_ATTEMPTS ; attempt ++ ) {
145
+ const currentWorkflow = await getCurrentWorkflowJobs ( runId )
146
+ const { jobs } = currentWorkflow
147
+ if ( ! jobs ) {
148
+ console . error ( 'Workflow check returned unknown object %o. Retry in 5 seconds.' , currentWorkflow )
149
+ await setTimeout ( 5000 )
150
+ continue
151
+ }
152
+ const hasAnyJobFailed = jobs
153
+ . some ( ( { status, conclusion } ) => status === 'completed' && conclusion !== 'success' )
154
+ const hasEveryJobPassed = jobs . every (
155
+ ( { status, conclusion } ) => status === 'completed' && conclusion === 'success'
156
+ )
157
+ if ( hasAnyJobFailed ) {
158
+ throw new Error ( `Performance overhead test failed.\n Check https://github.com/DataDog/test-environment/actions/runs/${ runId } for more details.` )
159
+ } else if ( hasEveryJobPassed ) {
160
+ console . log ( 'Performance overhead test successful.' )
161
+ break
162
+ } else {
163
+ console . log ( `Workflow https://github.com/DataDog/test-environment/actions/runs/${ runId } is not finished yet. [Attempt ${ attempt + 1 } /${ MAX_ATTEMPTS } ]` )
164
+ }
165
+ if ( attempt === MAX_ATTEMPTS - 1 ) {
166
+ throw new Error ( `Timeout: Workflow did not finish within 30 minutes. Check https://github.com/DataDog/test-environment/actions/runs/${ runId } for more details.` )
167
+ }
168
+ await setTimeout ( 5000 )
169
+ }
170
170
}
171
171
172
172
main ( ) . catch ( e => {
0 commit comments