Skip to content

Commit

Permalink
Redirection push fix (#4286)
Browse files Browse the repository at this point in the history
* adds input for defining the number of attempts to make on the target url before aborting

* checks that the root of the target URL responds with a 300 before we proceed with URL redirection verifications

* removes remaining calls to console.log, replaces with core.info
  • Loading branch information
gilzow authored Dec 4, 2024
1 parent 982bf8a commit 7078b88
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 48 deletions.
4 changes: 4 additions & 0 deletions .github/actions/redirection-verification/action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
130 changes: 82 additions & 48 deletions .github/actions/redirection-verification/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string[]> | _.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<string, string[]> | _.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();

0 comments on commit 7078b88

Please sign in to comment.