From 61e1aeeb3674d29396285fec6b813deed91d35b9 Mon Sep 17 00:00:00 2001 From: Rohit Gohri Date: Mon, 4 May 2020 00:35:12 +0530 Subject: [PATCH] ci: use danger diff to get master data --- dangerfile.js | 17 +++++++++++++-- scripts/data-validate.js | 7 +++--- scripts/masterData.js | 5 ----- scripts/utils.js | 46 ---------------------------------------- 4 files changed, 18 insertions(+), 57 deletions(-) delete mode 100644 scripts/masterData.js diff --git a/dangerfile.js b/dangerfile.js index 24bcde3a..b7dd33d2 100644 --- a/dangerfile.js +++ b/dangerfile.js @@ -1,9 +1,22 @@ /* eslint-disable import/no-extraneous-dependencies */ -const { fail, markdown, message, schedule } = require('danger'); +const { danger, fail, markdown, message, schedule } = require('danger'); const validate = require('./scripts/data-validate'); +const DATA_FILE = 'src/data.js'; + async function main() { - const { data: changedData, errorMsgs, failedUrls } = await validate(); + if (!danger.git.modified_files.includes(DATA_FILE)) { + message(`No changes in \`${DATA_FILE}\``); + return; + } + + const diff = await danger.git.diffForFile(DATA_FILE); + // eslint-disable-next-line no-eval + const masterData = eval(diff.before); + + const { data: changedData, errorMsgs, failedUrls } = await validate( + masterData + ); // If there are errors, will fail the action & add a comment detailing the issues if (errorMsgs.length) { diff --git a/scripts/data-validate.js b/scripts/data-validate.js index 9f25cf7b..3b80d324 100644 --- a/scripts/data-validate.js +++ b/scripts/data-validate.js @@ -1,9 +1,8 @@ -const { getMasterData, Schema, getStatusCode } = require('./utils.js'); +const { Schema, getStatusCode } = require('./utils.js'); const srcData = require('../src/data.js'); -async function main() { - // on master branch will be empty array - const masterDataUrls = (await getMasterData()).map(d => d.url); +async function main(masterData = []) { + const masterDataUrls = masterData.map(d => d.url); // so here data will be an array with all users const data = srcData.filter(d => !masterDataUrls.includes(d.url)); diff --git a/scripts/masterData.js b/scripts/masterData.js deleted file mode 100644 index 3d4c49b7..00000000 --- a/scripts/masterData.js +++ /dev/null @@ -1,5 +0,0 @@ -/** - * this is a stub file, do not edit it - * see `scripts/utils.js` -> `getMasterData` - */ -module.exports = []; diff --git a/scripts/utils.js b/scripts/utils.js index fd5e7ea2..b6fb6b12 100644 --- a/scripts/utils.js +++ b/scripts/utils.js @@ -1,54 +1,8 @@ -const exec = require('@actions/exec'); -const core = require('@actions/core'); const Joi = require('@hapi/joi'); const http = require('http'); const https = require('https'); const flags = require('./flags.js'); -async function getCurrentBranchName() { - let myOutput = ''; - let myError = ''; - - const options = { - silent: true, - listeners: { - stdout: data => (myOutput += data.toString()), - stderr: data => (myError += data.toString()), - }, - }; - - await exec.exec('git rev-parse --abbrev-ref HEAD', [], options); - return myOutput.trim(); -} - -/** on master branch will return an empty array */ -module.exports.getMasterData = async function() { - const options = { silent: true }; - const curentBranchName = await getCurrentBranchName(); - // when on a branch/PR different from master - // will populate scripts/masterData.js with src/data.js from master - if (curentBranchName !== 'master') { - core.info('Executing action on branch different from master'); - await exec.exec('mv src/data.js src/tmpData.js', [], options); - await exec.exec('git fetch origin master', [], options); - await exec.exec('git restore --source=FETCH_HEAD src/data.js', [], options); - await exec.exec('mv src/data.js scripts/masterData.js', [], options); - await exec.exec('mv src/tmpData.js src/data.js', [], options); - } else { - core.info('Executing action on master branch'); - } - - // eslint-disable-next-line global-require - const masterData = require('./masterData.js'); - - // restore `scripts/masterData.js` after was loaded - if (curentBranchName !== 'master') { - await exec.exec('git restore scripts/masterData.js', [], options); - } - - return masterData; -}; - module.exports.Schema = Joi.object({ name: Joi.string().required(), description: Joi.string().required(),