refactor: removed setInterval logic
This commit is contained in:
committed by
mrugesh mohapatra
parent
aa47b16156
commit
5070a87dd5
@ -13,14 +13,8 @@ const { addComment } = require('./addComment');
|
|||||||
|
|
||||||
octokit.authenticate(octokitAuth);
|
octokit.authenticate(octokitAuth);
|
||||||
|
|
||||||
const labelsAdder = (number, existingLabels, labelsToAdd, log) => {
|
const rateLimiter = (delay) => {
|
||||||
const newLabels = Object.keys(labelsToAdd).filter(label => !existingLabels.includes(label));
|
return new Promise(resolve => setTimeout(() => resolve(true), delay));
|
||||||
if (newLabels.length) {
|
|
||||||
addLabels(number, newLabels, log);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
log.update(number, false);
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const { PrProcessingLog } = require('./prProcessingLog');
|
const { PrProcessingLog } = require('./prProcessingLog');
|
||||||
@ -42,42 +36,55 @@ const prPropsToGet = ['number', 'labels', 'user'];
|
|||||||
|
|
||||||
log.start();
|
log.start();
|
||||||
console.log('Starting labeling process...');
|
console.log('Starting labeling process...');
|
||||||
let count = 0;
|
for (let count = 0; count < openPRs.length; count++) {
|
||||||
const maxCount = openPRs.length;
|
let { number, labels, user: { login: username } } = openPRs[count];
|
||||||
|
|
||||||
let interval = setInterval(async () => {
|
|
||||||
if (count < maxCount ) {
|
|
||||||
let { number, labels, user: {login: username} } = openPRs[count];
|
|
||||||
const { data: prFiles } = await octokit.pullRequests.getFiles({ owner, repo, number });
|
const { data: prFiles } = await octokit.pullRequests.getFiles({ owner, repo, number });
|
||||||
log.add(number)
|
log.add(number)
|
||||||
const existingLabels = labels.map(({ name }) => name);
|
|
||||||
const labelsToAdd = {}; // holds potential labels to add based on file path
|
const labelsToAdd = {}; // holds potential labels to add based on file path
|
||||||
|
|
||||||
|
const guideFolderErrorsComment = guideFolderChecks(prFiles, username);
|
||||||
|
if (guideFolderErrorsComment) {
|
||||||
|
const result = await octokit.issues.createComment({ owner, repo, number, body: guideFolderErrorsComment })
|
||||||
|
.catch((err) => {
|
||||||
|
console.log(`PR #${number} had an error when trying to add a comment\n`);
|
||||||
|
console.log(err)
|
||||||
|
});
|
||||||
|
if (result) {
|
||||||
|
console.log(`PR #${number} successfully added a comment\n`);
|
||||||
|
await rateLimiter(3000);
|
||||||
|
}
|
||||||
|
labelsToAdd['status: needs update'] = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
const existingLabels = labels.map(({ name }) => name);
|
||||||
|
|
||||||
prFiles.forEach(({ filename }) => {
|
prFiles.forEach(({ filename }) => {
|
||||||
/* remove '/challenges' from filename so variable second (below) will be the language */
|
/* remove '/challenges' from filename so language variable hold the language */
|
||||||
const filenameReplacement = filename.replace(/^curriculum\/challenges\//, 'curriculum\/');
|
const filenameReplacement = filename.replace(/^curriculum\/challenges\//, 'curriculum\/');
|
||||||
const regex = /^(docs|curriculum|guide)(?:\/)(arabic|chinese|portuguese|russian|spanish)?\/?/
|
const regex = /^(docs|curriculum|guide)(?:\/)(arabic|chinese|portuguese|russian|spanish)?\/?/
|
||||||
const [ _, first, second ] = filenameReplacement.match(regex) || []; // need an array to pass to labelsAdder
|
const [ _, articleType, language ] = filenameReplacement.match(regex) || []; // need an array to pass to labelsAdder
|
||||||
if (first && validLabels[first]) { labelsToAdd[validLabels[first]] = 1 }
|
|
||||||
if (second && validLabels[second]) { labelsToAdd[validLabels[second]] = 1 }
|
|
||||||
|
|
||||||
const guideFolderErrorsComment = guideFolderChecks(filename, username);
|
if (articleType && validLabels[articleType]) {
|
||||||
|
labelsToAdd[validLabels[articleType]] = 1
|
||||||
if (guideFolderErrorsComment) {
|
}
|
||||||
addComment(number, guideFolderErrorsComment);
|
if (language && validLabels[language]) {
|
||||||
labelsToAdd['status: needs update'] = 1;
|
labelsToAdd[validLabels[language]] = 1
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
labelsAdder(number, existingLabels, labelsToAdd, log);
|
/* 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) {
|
||||||
|
addLabels(number, newLabels, log);
|
||||||
|
await rateLimiter(3000);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
clearInterval(interval);
|
log.update(number, false);
|
||||||
interval = null;
|
}
|
||||||
log.export();
|
if (count % 25 === 0) {
|
||||||
|
log.export()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (count % 25 === 0) { log.export() }
|
|
||||||
count++;
|
|
||||||
}, 1000);
|
|
||||||
}
|
}
|
||||||
})()
|
})()
|
||||||
.then(() => {
|
.then(() => {
|
||||||
|
Reference in New Issue
Block a user