From 38464bf0653e3aeed5e958bef629946014d365e4 Mon Sep 17 00:00:00 2001 From: Rohit Gohri Date: Sun, 3 May 2020 23:58:55 +0530 Subject: [PATCH] ci: Move comment creation to dangerfile --- dangerfile.js | 18 +++++++++++++----- scripts/data-validate.js | 17 +++++++++-------- 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/dangerfile.js b/dangerfile.js index 5357e2f0..f35b5d0c 100644 --- a/dangerfile.js +++ b/dangerfile.js @@ -4,17 +4,25 @@ const validate = require('./scripts/data-validate'); async function main() { let comment = ''; - const { data: changedData, errors, failedUrls } = await validate(); + const { data: changedData, errorMsgs, failedUrls } = await validate(); // If there are errors, will fail the action & add a comment detailing the issues // If there are no errors, will leave an "all-clear" comment with relevant URLs (to ease a potential manual check) - if (errors.length || failedUrls.length) { - fail('Action failed with errors, see logs & comment'); + if (errorMsgs.length || failedUrls.length) { + fail( + `Action failed with ${errorMsgs.length + + failedUrls.length} errors, see logs & comment` + ); comment += [ '🚨 We have detected the following issues, let us (contributors) know if you need support or clarifications:', - ...errors.map(e => `- ${e.message}`), - ...failedUrls.map(url => `- URL is invalid: ${url}`), + + ...errorMsgs.map(msg => `- ${msg}`), + + ...failedUrls.map(({ url, error, statusCode }) => { + if (error) return `- URL is invalid: ${url}, error: ${error.message}`; + return `- URL is invalid: ${url}, status code: ${statusCode}`; + }), ].join('\n'); } else if (changedData.length) { comment += [ diff --git a/scripts/data-validate.js b/scripts/data-validate.js index c0cad8ff..e9cc0f91 100644 --- a/scripts/data-validate.js +++ b/scripts/data-validate.js @@ -1,4 +1,3 @@ -const core = require('@actions/core'); const { getMasterData, Schema, getStatusCode } = require('./utils.js'); const srcData = require('../src/data.js'); @@ -17,28 +16,30 @@ async function main() { .filter(v => v.error) .map(v => v.error); + const errorMsgs = []; + errors.forEach(e => { - core.error(e._original.name || e._original.url); - e.details.forEach(d => core.error(d.message)); + e.details.forEach(d => errorMsgs.push(d.message)); }); + /** + * @type {{url: string, statusCode?: number, error?: Error}[]} + */ const failedUrls = []; for (const { url } of data) { try { const statusCode = await getStatusCode(url); if (statusCode < 200 || statusCode >= 400) { - core.error(`Ping to "${url}" failed with status: ${statusCode}`); - failedUrls.push(url); + failedUrls.push({ url, statusCode }); } } catch (e) { - core.error(`Ping to "${url}" failed with error: ${e}`); - failedUrls.push(url); + failedUrls.push({ url, error: e }); } } return { failedUrls, - errors, + errorMsgs, data, }; }