feat: renamed labelOpenPrs.js to sweeper.js
This commit is contained in:
committed by
mrugesh mohapatra
parent
f21489fd22
commit
51d7b02dae
39
labeler.js
Normal file
39
labeler.js
Normal file
@ -0,0 +1,39 @@
|
||||
const { validLabels } = require('./validation');
|
||||
const { addLabels } = require('./prTasks');
|
||||
const { rateLimiter } = require('./utils');
|
||||
|
||||
const labeler = async (number, prFiles, currentLabels, guideFolderErrorsComment) => {
|
||||
const labelsToAdd = {}; // holds potential labels to add based on file path
|
||||
if (guideFolderErrorsComment) {
|
||||
labelsToAdd['status: needs update'] = 1;
|
||||
}
|
||||
const existingLabels = currentLabels.map(({ name }) => name);
|
||||
prFiles.forEach(({ filename }) => {
|
||||
/* remove '/challenges' from filename so language variable hold the language */
|
||||
const filenameReplacement = filename.replace(/^curriculum\/challenges\//, 'curriculum\/');
|
||||
const regex = /^(docs|curriculum|guide)(?:\/)(arabic|chinese|portuguese|russian|spanish)?\/?/
|
||||
const [ _, articleType, language ] = filenameReplacement.match(regex) || []; // need an array to pass to labelsAdder
|
||||
|
||||
if (articleType && validLabels[articleType]) {
|
||||
labelsToAdd[validLabels[articleType]] = 1
|
||||
}
|
||||
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. */
|
||||
const newLabels = Object.keys(labelsToAdd).filter(label => !existingLabels.includes(label));
|
||||
if (newLabels.length) {
|
||||
if (process.env.PRODUCTION_RUN === 'true') {
|
||||
addLabels(number, newLabels);
|
||||
}
|
||||
await rateLimiter(+process.env.RATELIMIT_INTERVAL | 1500);
|
||||
}
|
||||
return newLabels;
|
||||
};
|
||||
|
||||
module.exports = { labeler };
|
48
sweeper.js
Normal file
48
sweeper.js
Normal file
@ -0,0 +1,48 @@
|
||||
const { owner, repo, octokitConfig, octokitAuth } = require('./constants');
|
||||
|
||||
const octokit = require('@octokit/rest')(octokitConfig);
|
||||
|
||||
const { getPRs, getUserInput } = require('./getPRs');
|
||||
const { guideFolderChecks } = require('./validation');
|
||||
const { savePrData, ProcessingLog } = require('./utils');
|
||||
const { labeler } = require('./labeler');
|
||||
|
||||
octokit.authenticate(octokitAuth);
|
||||
|
||||
const log = new ProcessingLog();
|
||||
|
||||
console.log('Sweeper started...');
|
||||
(async () => {
|
||||
const { firstPR, lastPR } = await getUserInput();
|
||||
const prPropsToGet = ['number', 'labels', 'user'];
|
||||
const { openPRs } = await getPRs(firstPR, lastPR, prPropsToGet);
|
||||
|
||||
if (openPRs.length) {
|
||||
savePrData(openPRs, firstPR, lastPR);
|
||||
log.start();
|
||||
console.log('Processing PRs...');
|
||||
for (let count in openPRs) {
|
||||
let { number, labels: currentLabels, user: { login: username } } = openPRs[count];
|
||||
log.add(number, 'labels');
|
||||
log.add(number, 'comment');
|
||||
|
||||
const { data: prFiles } = await octokit.pullRequests.listFiles({ owner, repo, number });
|
||||
|
||||
const guideFolderErrorsComment = await guideFolderChecks(number, prFiles, username);
|
||||
const commentLogVal = guideFolderErrorsComment ? guideFolderErrorsComment : 'none';
|
||||
log.update(number, 'comment', commentLogVal)
|
||||
|
||||
const labelsAdded = await labeler(number, prFiles, currentLabels, guideFolderErrorsComment);
|
||||
const labelLogVal = labelsAdded.length ? labelsAdded : 'none added';
|
||||
log.update(number, 'labels', labelLogVal);
|
||||
}
|
||||
}
|
||||
})()
|
||||
.then(() => {
|
||||
log.finish();
|
||||
console.log('Sweeper complete');
|
||||
})
|
||||
.catch(err => {
|
||||
log.finish();
|
||||
console.log(err)
|
||||
})
|
Reference in New Issue
Block a user