refac: moved labeler.js to pr-tasks

This commit is contained in:
Randell Dawson
2018-11-29 12:10:57 -08:00
committed by mrugesh mohapatra
parent 37aceca8b0
commit f443f2bbd6
3 changed files with 42 additions and 2 deletions

View File

@ -1,5 +1,6 @@
const { addComment } = require('./add-comment'); const { addComment } = require('./add-comment');
const { addLabels } = require('./add-labels'); const { addLabels } = require('./add-labels');
const { closeOpen } = require('./close-open'); const { closeOpen } = require('./close-open');
const { labeler } = require('./labeler');
module.exports = { addComment, addLabels, closeOpen }; module.exports = { addComment, addLabels, closeOpen, labeler };

39
pr-tasks/labeler.js Normal file
View File

@ -0,0 +1,39 @@
const { validLabels } = require('../validation');
const { addLabels } = require('./add-labels');
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 };

View File

@ -16,7 +16,7 @@ const octokit = require('@octokit/rest')(octokitConfig);
const { getPRs, getUserInput } = require('./get-prs'); const { getPRs, getUserInput } = require('./get-prs');
const { guideFolderChecks } = require('./validation'); const { guideFolderChecks } = require('./validation');
const { savePrData, ProcessingLog, rateLimiter } = require('./utils'); const { savePrData, ProcessingLog, rateLimiter } = require('./utils');
const { labeler } = require('./labeler'); const { labeler } = require('./pr-tasks');
octokit.authenticate(octokitAuth); octokit.authenticate(octokitAuth);