feat: updated label script

This commit is contained in:
Randell Dawson
2018-11-14 23:40:26 -08:00
committed by mrugesh mohapatra
parent 743d719f8d
commit cb39d645f2
5 changed files with 104 additions and 9 deletions

65
addTestLocallyLabel.js Normal file
View File

@ -0,0 +1,65 @@
require('dotenv').config();
const path = require('path');
const fs = require('fs');
const formatDate = require('date-fns/format');
const { owner, repo, fccBaseUrl, prBaseUrl } = require('./constants');
const { saveToFile, openJSONFile } = require('./fileFunctions');
const { octokitConfig, octokitAuth } = require('./octokitConfig');
const octokit = require('@octokit/rest')(octokitConfig);
const { getOpenPrs, getPrRange } = require('./getOpenPrs');
const { addLabels } = require('./addLabels');
const { rateLimiter, savePrData } = require('./utils');
octokit.authenticate(octokitAuth);
const { PrProcessingLog } = require('./prProcessingLog');
const log = new PrProcessingLog();
const prPropsToGet = ['number', 'labels'];
(async () => {
const { firstPR, lastPR } = await getPrRange();
const { openPRs } = await getOpenPrs(firstPR, lastPR, prPropsToGet);
if (openPRs.length) {
savePrData(openPRs, firstPR, lastPR);
log.start();
console.log('Starting labeling process...');
for (let count = 0; count < openPRs.length; count++) {
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;
}
log.add(number, 'labels');
/* 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');
}
if (count % 25 === 0) {
log.export()
}
}
}
})()
.then(() => {
log.finish();
console.log('Successfully completed labeling');
})
.catch(err => {
log.finish();
console.log(err)
})

13
frontmatterChecks.js Normal file
View File

@ -0,0 +1,13 @@
const matter = require('gray-matter');
const checkFrontmatter = (fullPath, isTranslation, content) => {
const { data: frontmatter } = matter(content);
let errors = [];
if (!frontmatter || _.isEmpty(frontmatter) || !frontmatter.title) {
errors.push(`${fullPath} is missing \`title key\` frontmatter.`);
}
if (isTranslation && !frontmatter.localeTitle) {
errors.push(`${fullPath} is missing \`localeTitle\`frontmatter.`);
}
return errors;
}

View File

@ -13,7 +13,7 @@ const { validLabels } = require('./validLabels');
const { addLabels } = require('./addLabels');
const { guideFolderChecks } = require('./guideFolderChecks');
const { addComment } = require('./addComment');
const { rateLimiter } = require('./utils');
const { rateLimiter, savePrData } = require('./utils');
octokit.authenticate(octokitAuth);
@ -27,12 +27,7 @@ const prPropsToGet = ['number', 'labels', 'user'];
const { openPRs } = await getOpenPrs(firstPR, lastPR, prPropsToGet);
if (openPRs.length) {
console.log(`# of PRs Retrieved: ${openPRs.length}`);
console.log(`PR Range: ${firstPR} - ${lastPR}`);
const now = formatDate(new Date(), 'YYYY-MM-DDTHHmmss');
const fileName = path.resolve(__dirname, `./work-logs/openprs_${firstPR}-${lastPR}_${now}.json`);
saveToFile(fileName, JSON.stringify(openPRs));
console.log(`Data saved in file: ${fileName}`);
savePrData(openPRs, firstPR, lastPR);
log.start();
console.log('Starting labeling process...');
@ -69,6 +64,9 @@ const prPropsToGet = ['number', 'labels', 'user'];
if (language && validLabels[language]) {
labelsToAdd[validLabels[language]] = 1
}
if (articleType === 'curriculum') {
labelsToAdd['status: need to test locally'] = 1;
}
})
/* this next section only adds needed labels which are NOT currently on the PR. */

View File

@ -10,7 +10,11 @@ class PrProcessingLog {
this._lastPRlogged = null;
this._finish = null;
this._prs = {};
this._logfile = path.resolve(__dirname, './work-logs/open-prs-processed.json');
this._logfile = path.resolve(__dirname, `./work-logs/${this.getRunType()}_open-prs-processed.json`);
}
getRunType() {
return process.env.PRODUCTION_RUN === 'true' ? 'production' : 'test';
}
import() {

View File

@ -1,5 +1,20 @@
const path = require('path');
const fs = require('fs');
const formatDate = require('date-fns/format');
const { saveToFile } = require('./fileFunctions');
const rateLimiter = (delay) => {
return new Promise(resolve => setTimeout(() => resolve(true), delay));
};
module.exports = { rateLimiter };
const savePrData = (openPRs, firstPR, lastPR) => {
const now = formatDate(new Date(), 'YYYY-MM-DDTHHmmss');
const filename = path.resolve(__dirname, `./work-logs/openprs_${firstPR}-${lastPR}_${now}.json`);
console.log(`# of PRs Retrieved: ${openPRs.length}`);
console.log(`PR Range: ${firstPR} - ${lastPR}`);
saveToFile(filename, JSON.stringify(openPRs));
console.log(`Data saved in file: ${filename}`);
};
module.exports = { rateLimiter, savePrData };