ci: use danger diff to get master data

This commit is contained in:
Rohit Gohri 2020-05-04 00:35:12 +05:30
parent ef972cf38f
commit 61e1aeeb36
4 changed files with 18 additions and 57 deletions

View file

@ -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) {

View file

@ -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));

View file

@ -1,5 +0,0 @@
/**
* this is a stub file, do not edit it
* see `scripts/utils.js` -> `getMasterData`
*/
module.exports = [];

View file

@ -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(),