From 2ed2ea5ca14507b0596fe1d7eee654226093146f Mon Sep 17 00:00:00 2001 From: Hugo Di Francesco Date: Mon, 13 Jan 2020 15:38:23 +0000 Subject: [PATCH] make sure uses URLs 2xx --- scripts/data-validate.js | 51 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/scripts/data-validate.js b/scripts/data-validate.js index b578dbe9..e3b74ad1 100644 --- a/scripts/data-validate.js +++ b/scripts/data-validate.js @@ -1,8 +1,15 @@ import Joi from '@hapi/joi'; import core from '@actions/core'; +import * as http from 'http'; +import * as https from 'https'; import data from '../src/data.js'; import flags from './flags.js'; +if (process.env.CI !== 'true') { + core.error = console.error; + core.setFailed = console.error; +} + const schema = Joi.object({ name: Joi.string().required(), description: Joi.string().required(), @@ -32,3 +39,47 @@ errors.forEach(e => { if (errors.length) { core.setFailed('Action failed with validation errors, see logs'); } +const REQUEST_TIMEOUT = 5000; + +function getStatusCode(url) { + const client = url.startsWith('https') ? https : http; + return new Promise((resolve, reject) => { + setTimeout(() => reject(new Error('Request timed out')), REQUEST_TIMEOUT); + client + .get(url, res => { + resolve(res.statusCode); + }) + .on('error', err => { + reject(err); + }); + }); +} + +async function isWorkingUrl(url) { + try { + const statusCode = await getStatusCode(url); + if (statusCode < 200 || statusCode >= 300) { + core.error(`URL: "${url}" failed with status: ${statusCode}`); + return false; + } + return true; + } catch (e) { + core.error(`URL: "${url}" failed with error: ${e}`); + return false; + } +} + +(async () => { + // TODO: we might need to batch these in sets instead of requesting 100+ URLs + // at the same time + const areWorkingUrls = await Promise.all( + data.map(p => p.url).map(url => isWorkingUrl(url)) + ); + const failingUrls = areWorkingUrls.filter(a => !a); + if (failingUrls.length > 0) { + core.setFailed( + `Action failed with ${failingUrls.length} URL fetch failures, see logs` + ); + } + process.exit(0); +})();