From 925a1d8a55fc8c70c735c2ed49b265858fd7b8d2 Mon Sep 17 00:00:00 2001 From: Randell Dawson Date: Sun, 25 Nov 2018 03:57:57 -0800 Subject: [PATCH] feat: improved findFailures.js and closeOpen.js --- get-prs/index.js | 1 - one-off-scripts/close-open-single-pr.js | 22 ++++++++ .../close-open-specific-failures.js | 51 +++++++++++++++++++ one-off-scripts/find-failures.js | 17 ++++--- pr-tasks/add-comment.js | 3 +- pr-tasks/close-open.js | 16 +++--- utils/open-json-file.js | 1 - 7 files changed, 93 insertions(+), 18 deletions(-) create mode 100644 one-off-scripts/close-open-single-pr.js create mode 100644 one-off-scripts/close-open-specific-failures.js diff --git a/get-prs/index.js b/get-prs/index.js index ce833f0942..14ef0b347d 100644 --- a/get-prs/index.js +++ b/get-prs/index.js @@ -1,5 +1,4 @@ require('dotenv').config(); -// const formatDate = require('date-fns/format'); const { owner, repo, octokitConfig, octokitAuth } = require('../constants'); diff --git a/one-off-scripts/close-open-single-pr.js b/one-off-scripts/close-open-single-pr.js new file mode 100644 index 0000000000..52711bb774 --- /dev/null +++ b/one-off-scripts/close-open-single-pr.js @@ -0,0 +1,22 @@ +require('dotenv').config({ path: '../.env' }); +const { closeOpen } = require('../pr-tasks'); + +const getUserInput = async () => { + let [ n, f, prNum ] = process.argv; + + if (!Number(prNum)) { + throw `Please specify a PR # to close and reopen.`; + } + return { prNum }; +}; + +(async () => { + const { prNum } = await getUserInput(); + return prNum; +})() +.then((prNum) => { + closeOpen(prNum); +}) +.catch((err) => { + console.log(err) +}); diff --git a/one-off-scripts/close-open-specific-failures.js b/one-off-scripts/close-open-specific-failures.js new file mode 100644 index 0000000000..21681902c5 --- /dev/null +++ b/one-off-scripts/close-open-specific-failures.js @@ -0,0 +1,51 @@ +require('dotenv').config({ path: '../.env' }); +const { closeOpen } = require('../pr-tasks'); +const { openJSONFile, ProcessingLog, rateLimiter } = require('../utils'); + +const log = new ProcessingLog('prs-closed-reopened'); + +log.start(); +const getUserInput = async () => { + let [ n, f, filename ] = process.argv; + + if (!filename) { + throw `Please specify a file with PRs which needed to be closed and reopened.`; + } + + let fileObj = openJSONFile(filename); + let { prs } = fileObj; + if (!prs.length) { + throw `Either no PRs found in file or there or an error occurred.` + } + return { prs }; +}; + +(async () => { + const { prs } = await getUserInput(); + return prs; +})() +.then(async (prs) => { + const firstPR = prs[0].number; + const lastPR = prs[prs.length - 1].number; + log.setFirstLast({ firstPR, lastPR }); + for (let { number, data: { errorDesc } } of prs) { + if (errorDesc !== "unknown error") { + log.add(number, { closedOpened: true, errorDesc }); + if (process.env.PRODUCTION_RUN === 'true') { + await closeOpen(number); + await rateLimiter(30000); + } + } + else { + log.add(number, { closedOpened: false, errorDesc }); + } + } +}) +.then(() => { + log.finish(); + console.log('closing/reopening of PRs complete'); +}) +.catch(err => { + log.finish(); + console.log(err) +}) diff --git a/one-off-scripts/find-failures.js b/one-off-scripts/find-failures.js index 8028ab07d2..c8746ada95 100644 --- a/one-off-scripts/find-failures.js +++ b/one-off-scripts/find-failures.js @@ -17,12 +17,13 @@ const { savePrData, ProcessingLog } = require('../utils'); octokit.authenticate(octokitAuth); -const log = new ProcessingLog(); +const log = new ProcessingLog('find-failures-script'); const errorsToFind = require(path.resolve(__dirname, '../input-files/failuresToFind.json')); (async () => { const { firstPR, lastPR } = await getUserInput(); + log.setFirstLast({ firstPR, lastPR }); const prPropsToGet = ['number', 'head']; const { openPRs } = await getPRs(firstPR, lastPR, prPropsToGet); @@ -32,15 +33,15 @@ const errorsToFind = require(path.resolve(__dirname, '../input-files/failuresToF console.log('Starting error finding process...'); for (let count in openPRs) { let { number, head: { sha: ref } } = openPRs[count]; - log.add(number, 'error'); - const { data: statuses } = await octokit.repos.getStatuses({ owner, repo, ref }); + const { data: statuses } = await octokit.repos.listStatusesForRef({ owner, repo, ref }); if (statuses.length) { const { state, target_url } = statuses[0]; // first element contain most recent status const hasProblem = state === 'failure' || state === 'error'; if (hasProblem) { - let buildNum = Number(target_url.match(/\/builds\/(\d+)\?/i)[1]);' - const logNumber = 'need to use Travis api to access the full log for the buildNum above' + let buildNum = Number(target_url.match(/\/builds\/(\d+)\?/i)[1]); + //const logNumber = 'need to use Travis api to access the full log for the buildNum above' + const logNumber = ++buildNum; const travisLogUrl = `https://api.travis-ci.org/v3/job/${logNumber}/log.txt`; const response = await fetch(travisLogUrl) const logText = await response.text(); @@ -54,8 +55,8 @@ const errorsToFind = require(path.resolve(__dirname, '../input-files/failuresToF break; } } - const errorDesc = error ? error : 'unkown error'; - log.update(number, 'error', { errorDesc, buildLog: travisLogUrl }); + const errorDesc = error ? error : 'unknown error'; + log.add(number, { errorDesc, buildLog: travisLogUrl }); } } } @@ -63,7 +64,7 @@ const errorsToFind = require(path.resolve(__dirname, '../input-files/failuresToF })() .then(() => { log.finish(); - console.log('Successfully finding all specified errors.'); + console.log('Successfully finished finding all specified errors.'); }) .catch(err => { log.finish(); diff --git a/pr-tasks/add-comment.js b/pr-tasks/add-comment.js index 2738de72cf..2ec8dcad0f 100644 --- a/pr-tasks/add-comment.js +++ b/pr-tasks/add-comment.js @@ -2,14 +2,13 @@ require('dotenv').config(); const { owner, repo, octokitConfig, octokitAuth } = require('../constants'); const octokit = require('@octokit/rest')(octokitConfig); - octokit.authenticate(octokitAuth); 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) + console.log(err); }); if (result) { diff --git a/pr-tasks/close-open.js b/pr-tasks/close-open.js index 930bca8cbe..375de5ffd7 100644 --- a/pr-tasks/close-open.js +++ b/pr-tasks/close-open.js @@ -1,22 +1,26 @@ require('dotenv').config(); +const { addComment } = require('./add-comment'); +const { rateLimiter } = require('../utils'); const { owner, repo, octokitConfig, octokitAuth } = require('../constants'); -const octokit = require('@octokit/rest')(octokitConfig); +const octokit = require('@octokit/rest')(octokitConfig); octokit.authenticate(octokitAuth); /* closes and reopens an open PR with applicable comment */ -const prOpenClose = async (number) => { +const closeOpen = async (number) => { const result = await octokit.pullRequests.update({ owner, repo , number, state: 'closed', base: 'master' }) - .then(() => { + .then(async () => { + await rateLimiter(2000); return octokit.pullRequests.update({ owner, repo , number, state: 'open', base: 'master' }) }) - .then(() => { - addComment(number, 'Closed and Reopened this PR to attempt to resolve a specific Travis build failure.'); + .then(async () => { + await rateLimiter(1000); + await addComment(number, 'Closed and Reopened this PR to attempt to resolve a specific Travis build failure.'); }) .catch((err) => { console.log(err) }) }; -module.exports = prOpenClose; +module.exports = { closeOpen }; diff --git a/utils/open-json-file.js b/utils/open-json-file.js index 6de44445f0..c6f818fdb0 100644 --- a/utils/open-json-file.js +++ b/utils/open-json-file.js @@ -2,7 +2,6 @@ const fs = require('fs'); const openJSONFile = fileName => { const data = JSON.parse(fs.readFileSync(fileName, 'utf8')); - console.log('Opened JSON file') return data; };