diff --git a/labeler.js b/labeler.js new file mode 100644 index 0000000000..8eb20393d4 --- /dev/null +++ b/labeler.js @@ -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 }; diff --git a/sweeper.js b/sweeper.js new file mode 100644 index 0000000000..f705fbd18d --- /dev/null +++ b/sweeper.js @@ -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) +})