From 76c23be416a8e54da05c467dc16523ffc7746a07 Mon Sep 17 00:00:00 2001 From: Randell Dawson Date: Fri, 9 Nov 2018 21:58:12 -0800 Subject: [PATCH] fix: addComment and labler issues --- addComment.js | 25 +++++++++++++------------ labelOpenPrs.js | 18 ++++-------------- 2 files changed, 17 insertions(+), 26 deletions(-) diff --git a/addComment.js b/addComment.js index a3deecaf22..46d2b09e8a 100644 --- a/addComment.js +++ b/addComment.js @@ -5,17 +5,18 @@ const octokit = require('@octokit/rest')(octokitConfig); octokit.authenticate(octokitAuth); -const addComment = (number, comment) => { - setTimeout(async () => { - octokit.issues.createComment({ owner, repo, number, body: comment }) - .then(() => { - console.log(`PR #${number} successfully added a comment\n`); - }) - .catch((err) => { - console.log(`PR #${number} had an error when trying to add a comment\n`); - console.log(err) - }) - }, 1000); // wait one second to comment for API rate limiting compliance +const addComment = async (number, comment) => { + const result = await octokit.issues.createComment({ owner, repo, number, body: comment }) + .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`); + } + + return result; }; -exports.addComment = addComment; +module.exports = { addComment }; diff --git a/labelOpenPrs.js b/labelOpenPrs.js index 21dd14644b..9444532a3a 100644 --- a/labelOpenPrs.js +++ b/labelOpenPrs.js @@ -10,13 +10,10 @@ const { validLabels } = require('./validLabels'); const { addLabels } = require('./addLabels'); const { guideFolderChecks } = require('./guideFolderChecks'); const { addComment } = require('./addComment'); +const { rateLimiter } = require('./utils'); octokit.authenticate(octokitAuth); -const rateLimiter = (delay) => { - return new Promise(resolve => setTimeout(() => resolve(true), delay)); -}; - const { PrProcessingLog } = require('./prProcessingLog'); const log = new PrProcessingLog(); @@ -44,15 +41,8 @@ const prPropsToGet = ['number', 'labels', 'user']; 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); - } + const result = await addComment(number, guideFolderErrorsComment); + await rateLimiter(1000); labelsToAdd['status: needs update'] = 1; } @@ -76,7 +66,7 @@ const prPropsToGet = ['number', 'labels', 'user']; const newLabels = Object.keys(labelsToAdd).filter(label => !existingLabels.includes(label)); if (newLabels.length) { addLabels(number, newLabels, log); - await rateLimiter(3000); + await rateLimiter(1000); } else { log.update(number, false);