Files
freeCodeCamp/one-off-scripts/add-test-locally-label.js
2018-12-03 00:09:23 +05:30

61 lines
1.9 KiB
JavaScript

/*
This is a one-off script to run on all open PRs to add the
"status: need to test locally" label to any PR with an existing "scope: curriculum"
label on it.
*/
require('dotenv').config({ path: '../.env' });
const { owner, repo, octokitConfig, octokitAuth } = require('../constants');
const octokit = require('@octokit/rest')(octokitConfig);
const { getPRs, getUserInput } = require('../get-prs');
const { addLabels } = require('../pr-tasks');
const { rateLimiter, savePrData, ProcessingLog } = require('../utils');
octokit.authenticate(octokitAuth);
const log = new ProcessingLog();
(async () => {
const { firstPR, lastPR } = await getUserInput();
const prPropsToGet = ['number', 'labels'];
const { openPRs } = await getPRs(firstPR, lastPR, prPropsToGet);
if (openPRs.length) {
savePrData(openPRs, firstPR, lastPR);
log.start();
console.log('Starting labeling process...');
for (let count in openPRs) {
let { number, labels } = openPRs[count];
log.add(number, 'labels');
const labelsToAdd = {}; // holds potential labels to add based on file path
const existingLabels = labels.map(({ name }) => name);
if (existingLabels.includes('scope: curriculum')) {
labelsToAdd['status: need to test locally'] = 1;
}
/* this next section only adds needed labels which are NOT currently on the PR. */
const newLabels = Object.keys(labelsToAdd).filter(label => !existingLabels.includes(label));
if (newLabels.length) {
log.update(number, 'labels', newLabels);
if (process.env.PRODUCTION_RUN === 'true') {
addLabels(number, newLabels, log);
await rateLimiter(+process.env.RATELIMIT_INTERVAL | 1500);
}
}
else {
log.update(number, 'labels', 'none added');
}
}
}
})()
.then(() => {
log.finish();
console.log('Successfully completed labeling');
})
.catch(err => {
log.finish();
console.log(err)
})