diff --git a/.github/actions/redirection-verification/action.yaml b/.github/actions/redirection-verification/action.yaml index d1c73293db..a27e6cdf78 100644 --- a/.github/actions/redirection-verification/action.yaml +++ b/.github/actions/redirection-verification/action.yaml @@ -4,6 +4,10 @@ inputs: environment-url: description: 'Pull Request Environment URL' required: true + number_retries: + description: 'The number of attempts we should make to contact the target environment URL. 1 second delay between attempt.' + required: false + default: '100' #### #outputs: diff --git a/.github/actions/redirection-verification/index.js b/.github/actions/redirection-verification/index.js index 7a740801e7..e4d58c5b89 100644 --- a/.github/actions/redirection-verification/index.js +++ b/.github/actions/redirection-verification/index.js @@ -35,55 +35,89 @@ function linkify(path,url) { * @type {string} */ axios.defaults.baseURL = core.getInput('environment-url') +//axios.defaults.baseURL = 'https://httpstat.us/random/200,500-504,500-504,500-504' +const retries = Number(core.getInput('number_retries')) +//const retries = Number('100') +function sleep(ms) { + return new Promise((resolve) => { + setTimeout(resolve, ms); + }); +} -try { - /** - * @todo Can we get the full workspace path to this file? - * @type {*} - */ - const yamlData = yaml.load(fs.readFileSync('./.platform/routes.yaml', 'utf8')); - /** - * @todo the key (docs.upsun.com) here should be a variable that is set somewhere else - * @type {Record | _.LodashAt | ((request: string) => (string[] | null)) | string[]} - */ - const anchors = yamlData['https://docs.upsun.com/'].redirects.paths - - const RedirectKeys = Object.keys(anchors).filter((path)=>{ +const verifyTargetResponse = async(count = 0) => { + try { + const axiosResponse = await axios.get('/'); + core.notice('Target URL finally responded with a 200. Proceeding.') + return axiosResponse; + } catch (error) { + if (error || error.status != 200) { + core.info(`At attempt ${count}, target url responded with status ${error.status}, retrying...`) + if (count++ < retries) { + await sleep(1000); + return verifyTargetResponse(count); + } else { + core.setFailed(`Max number of retries (${retries}) reached. Aborting.`) + }; + } else { + core.setFailed(`Action failed with error ${error}`) + }; + }; +}; + +const verify = async () => { + let targetReady = await verifyTargetResponse(); + core.info('Target URL ready. Beginning verification.') + try { /** - * @todo the piece we're using to identify our contracts (/anchors/) should be a variable + * @todo Can we get the full workspace path to this file? + * @type {*} */ - return path.startsWith('/anchors/') - }) - - const validateRedirects = RedirectKeys.map(async (path, index, array) => { - //console.log(`I'm going to test ${path} to see if it goes to ${anchors[path].to}`) - - try { - const response = await axios.head(path); - //core.info(`Response for our check of ${path} is ${response.status}`) - return response - } catch (reqerr) { - //core.warning(`issue encountered with path ${path}!!! Returned status is ${reqerr.status}`) - let row = [{data: linkify(path, axios.defaults.baseURL)},{data: linkify( anchors[path].to, axios.defaults.baseURL) }] - tableData.push(row) - } - }); - - - Promise.all(validateRedirects).then(() => { - if(tableData.length > 1) { - - core.error('There was an error with one or more redirects.') - - core.summary.addTable(tableData) - - core.summary.write() - core.setFailed('There was an error with one or more contracted redirects.') - } else { - core.notice('All contracted redirections are valid.') - } - }); - -} catch (error) { - core.setFailed(`Action failed with error ${error}`) + const yamlData = yaml.load(fs.readFileSync('./.platform/routes.yaml', 'utf8')); + /** + * @todo the key (docs.upsun.com) here should be a variable that is set somewhere else + * @type {Record | _.LodashAt | ((request: string) => (string[] | null)) | string[]} + */ + const anchors = yamlData['https://docs.upsun.com/'].redirects.paths + + const RedirectKeys = Object.keys(anchors).filter((path)=>{ + /** + * @todo the piece we're using to identify our contracts (/anchors/) should be a variable + */ + return path.startsWith('/anchors/') + }) + + const validateRedirects = RedirectKeys.map(async (path, index, array) => { + //console.log(`I'm going to test ${path} to see if it goes to ${anchors[path].to}`) + + try { + const response = await axios.head(path); + core.debug(`Response for our check of ${path} is ${response.status}`) + return response + } catch (reqerr) { + //core.warning(`issue encountered with path ${path}!!! Returned status is ${reqerr.status}`) + let row = [{data: linkify(path, axios.defaults.baseURL)},{data: linkify( anchors[path].to, axios.defaults.baseURL) }] + tableData.push(row) + } + }); + + + Promise.all(validateRedirects).then(() => { + if(tableData.length > 1) { + + core.error('There was an error with one or more redirects.') + + core.summary.addTable(tableData) + + core.summary.write() + core.setFailed('There was an error with one or more contracted redirects.') + } else { + core.notice('All contracted redirections are valid.') + } + }); + + } catch (error) { + core.setFailed(`Action failed with error ${error}`) + } } + +verify();