refactor: uses getOpenPrs to get PRs
This commit is contained in:
105
findFailures.js
105
findFailures.js
@ -1,30 +1,45 @@
|
|||||||
require('dotenv').config();
|
require('dotenv').config();
|
||||||
const { owner, repo, fccBaseUrl, prBaseUrl } = require('./constants');
|
const { owner, repo, fccBaseUrl, prBaseUrl } = require('./constants');
|
||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
|
const formatDate = require('date-fns/format');
|
||||||
|
const { saveToFile, openJSONFile } = require('./fileFunctions');
|
||||||
const { octokitConfig, octokitAuth } = require('./octokitConfig');
|
const { octokitConfig, octokitAuth } = require('./octokitConfig');
|
||||||
const octokit = require('@octokit/rest')(octokitConfig);
|
const octokit = require('@octokit/rest')(octokitConfig);
|
||||||
const { paginate } = require('./paginate');
|
const { getOpenPrs, getPrRange } = require('./getOpenPrs');
|
||||||
const { saveToFile, writeToFile } = './fileFunctions';
|
|
||||||
const fetch = require('node-fetch');
|
const fetch = require('node-fetch');
|
||||||
const { getStatuses } = require('./getStatuses');
|
const { getStatuses } = require('./getStatuses');
|
||||||
|
|
||||||
octokit.authenticate(octokitAuth);
|
octokit.authenticate(octokitAuth);
|
||||||
|
|
||||||
const findFailures = async (errorsToFind, maxPrs) => {
|
//const { PrProcessingLog } = require('./prProcessingLog');
|
||||||
const methodProps = {
|
//const log = new PrProcessingLog();
|
||||||
owner, repo,
|
|
||||||
state: 'open', base: 'master', sort: 'created',
|
|
||||||
direction: 'asc', page: 1, per_page: 100
|
|
||||||
};
|
|
||||||
if (maxPRs) {
|
|
||||||
maxPages = Math.ceil(maxPrs / 100); // limits
|
|
||||||
}
|
|
||||||
|
|
||||||
const allOpenPRs = await paginate(octokit.pullRequests.getAll, methodProps, octokit, maxPages);
|
const prPropsToGet = ['number', 'head'];
|
||||||
|
|
||||||
const failedPRs = allOpenPRs.reduce(async (prevPromise, { number: pr, head: { sha: ref }) => {
|
const errorsToFind = require('./failuresToFind.json');
|
||||||
const failed = await prevPromise;
|
|
||||||
|
(async () => {
|
||||||
|
const { firstPR, lastPR } = await getPrRange();
|
||||||
|
const { openPRs } = await getOpenPrs(firstPR, lastPR, prPropsToGet);
|
||||||
|
|
||||||
|
if (openPRs.length) {
|
||||||
|
console.log(`# of PRs Retrieved: ${openPRs.length}`);
|
||||||
|
console.log(`PR Range: ${firstPR} - ${lastPR}`);
|
||||||
|
const now = formatDate(new Date(), 'YYYY-MM-DDTHHmmss');
|
||||||
|
const fileName = `data/openprs_statuses_urls_${firstPR}-${lastPR}_${now}.json`;
|
||||||
|
saveToFile(fileName, JSON.stringify(openPRs));
|
||||||
|
console.log(`Data saved in file: ${fileName}`);
|
||||||
|
|
||||||
|
//log.start();
|
||||||
|
console.log('Starting error finding process...');
|
||||||
|
let count = 0;
|
||||||
|
const maxCount = openPRs.length;
|
||||||
|
const failed = [];
|
||||||
|
let interval = setInterval(async () => {
|
||||||
|
if (count < maxCount ) {
|
||||||
|
let { number, head: { sha: ref } } = openPRs[count];
|
||||||
const statuses = await getStatuses(octokit.repos.getStatuses, {owner, repo, ref});
|
const statuses = await getStatuses(octokit.repos.getStatuses, {owner, repo, ref});
|
||||||
|
//log.add(number)
|
||||||
if (statuses.length) {
|
if (statuses.length) {
|
||||||
const { state, target_url } = statuses[0]; // first element contain most recent status
|
const { state, target_url } = statuses[0]; // first element contain most recent status
|
||||||
const hasProblem = state === 'failure' || state === 'error';
|
const hasProblem = state === 'failure' || state === 'error';
|
||||||
@ -32,44 +47,42 @@ const findFailures = async (errorsToFind, maxPrs) => {
|
|||||||
let buildNum = Number(target_url.match(/\/builds\/(\d+)\?/i)[1]);
|
let buildNum = Number(target_url.match(/\/builds\/(\d+)\?/i)[1]);
|
||||||
buildNum++; // full build log file is 1 # higher than buildNum (same as job number)
|
buildNum++; // full build log file is 1 # higher than buildNum (same as job number)
|
||||||
const travisLogUrl = `https://api.travis-ci.org/v3/job/${buildNum}/log.txt`;
|
const travisLogUrl = `https://api.travis-ci.org/v3/job/${buildNum}/log.txt`;
|
||||||
errorsToFind.forEach(async ({ errorDesc, errorRegex }) => {
|
console.log(number + '\'s errors:');
|
||||||
|
errorsToFind.forEach(async ({ error: errorDesc, regex }) => {
|
||||||
const response = await fetch(travisLogUrl)
|
const response = await fetch(travisLogUrl)
|
||||||
const logText = await response.text();
|
const logText = await response.text();
|
||||||
if (errorRegex.test(logText)) {
|
regex = RegExp(regex);
|
||||||
failed.push({
|
if (regex.test(logText)) {
|
||||||
|
const error = {
|
||||||
errorDesc,
|
errorDesc,
|
||||||
pr,
|
number,
|
||||||
buildLog: buildNum
|
buildLog: travisLogUrl
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
failed.push(error)
|
||||||
|
console.log(' ' + errorDesc);
|
||||||
|
}
|
||||||
|
console.log()
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return failed;
|
|
||||||
}, Promise.resolve([]));
|
|
||||||
|
|
||||||
return failedPRs;
|
|
||||||
};
|
|
||||||
|
|
||||||
/* Main Program */
|
|
||||||
const [ n, f, inputFile, outputFile, maxPRs ] = process.argv;
|
|
||||||
const inputFileMsg = 'Please specify an input file containing errors to find.\n';
|
|
||||||
const outputFileMsg = 'Please specify an output file to save the results of the search.\n';
|
|
||||||
const maxPRMsg = 'Please specify an integer from 1-4500 (inclusive) for the max # of PR to search.';
|
|
||||||
let errors = '';
|
|
||||||
if (!inputFile) { errors += inputFileMsg }
|
|
||||||
if (!outputFile) { errors += outputFileMsg }
|
|
||||||
if (!maxPRs || parseInt(maxPRs) < 1 || parseInt(maxPRs) > 4500) {
|
|
||||||
errors += maxPRMsg;
|
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
if (errors) { return console.log(errors) }
|
clearInterval(interval);
|
||||||
|
interval = null;
|
||||||
fs.readFile(inputFile, 'utf8', (err, errorsToFind) => {
|
//log.export();
|
||||||
if (err) { throw err }
|
}
|
||||||
findFailures(errorsToFind, maxPRs)
|
if (count % 25 === 0) {
|
||||||
.then((failedPRs) => {
|
//log.export()
|
||||||
saveToFile(outputFile, JSON.stringify(failedPRs));
|
}
|
||||||
console.log(`# PRs matching specified error: ${failedPRs.length}`);
|
count++;
|
||||||
|
}, 1000);
|
||||||
|
}
|
||||||
|
})()
|
||||||
|
.then(() => {
|
||||||
|
//log.finish();
|
||||||
|
console.log('Successfully finding all specified errors.');
|
||||||
|
})
|
||||||
|
.catch(err => {
|
||||||
|
//log.finish();
|
||||||
|
console.log(err)
|
||||||
})
|
})
|
||||||
});
|
|
||||||
|
@ -1,14 +1,23 @@
|
|||||||
const prOpenClose = async () => {
|
require('dotenv').config();
|
||||||
const result = await octokit.pullRequests.update({ owner, repo , number, state: 'closed', base })
|
const { owner, repo, fccBaseUrl, prBaseUrl } = require('./constants');
|
||||||
|
const { octokitConfig, octokitAuth } = require('./octokitConfig');
|
||||||
|
const octokit = require('@octokit/rest')(octokitConfig);
|
||||||
|
octokit.authenticate(octokitAuth);
|
||||||
|
|
||||||
|
const prOpenClose = async (number) => {
|
||||||
|
const result = await octokit.pullRequests.update({ owner, repo , number, state: 'closed', base: 'master' })
|
||||||
.then(() => {
|
.then(() => {
|
||||||
return octokit.pullRequests.update({ owner, repo , number, state: 'open', base })
|
return octokit.pullRequests.update({ owner, repo , number, state: 'open', base: 'master' })
|
||||||
})
|
})
|
||||||
.then(() => {
|
.then(() => {
|
||||||
log.update(true)
|
console.log('success')
|
||||||
|
//log.update(true)
|
||||||
})
|
})
|
||||||
.catch(() => {
|
.catch((err) => {
|
||||||
log.update(false)
|
console.log('catch')
|
||||||
|
console.log(err)
|
||||||
|
//log.update(false)
|
||||||
})
|
})
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.changePrState = changePrState;
|
exports.prOpenClose = prOpenClose;
|
||||||
|
Reference in New Issue
Block a user