mirror of
https://github.com/BradNut/awesome-uses
synced 2025-09-08 17:40:31 +00:00
make sure uses URLs 2xx
This commit is contained in:
parent
34a883061e
commit
2ed2ea5ca1
1 changed files with 51 additions and 0 deletions
|
|
@ -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);
|
||||
})();
|
||||
|
|
|
|||
Loading…
Reference in a new issue