From e01ca12368677e1f8f72f97c4170b386cb250fb8 Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Fri, 7 Oct 2022 18:12:24 +0800 Subject: [PATCH] feat: add --since option to ncu-ci (#649) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat: add --since option to ncu-ci Co-authored-by: Michaƫl Zasso --- bin/ncu-ci.js | 23 ++++++++++++++++++++++- lib/ci/ci_utils.js | 9 ++++++--- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/bin/ncu-ci.js b/bin/ncu-ci.js index 5e865927..d0fd8bf1 100755 --- a/bin/ncu-ci.js +++ b/bin/ncu-ci.js @@ -86,6 +86,19 @@ const args = yargs(hideBin(process.argv)) .option('limit', { default: 99, describe: 'Maximum number of CIs to get data from' + }) + .option('since ', { + type: 'string', + describe: 'Time since when the CI results should be queried' + }).check(argv => { + try { + // eslint-disable-next-line no-new + new Date(argv.since); + } catch { + throw new Error('--since should be string that can ' + + 'be parsed by new Date()'); + } + return true; }); }, handler @@ -421,7 +434,12 @@ class WalkCommand extends CICommand { async initialize() { const ciType = commandToType[this.argv.type]; - const builds = await listBuilds(this.cli, this.request, ciType); + const since = this.argv.since ? new Date(this.argv.since) : undefined; + const builds = await listBuilds(this.cli, this.request, ciType, since); + if (builds.count === 0) { + this.cli.log('No applicable builds found.'); + return; + } this.queue.push({ type: 'health', ciType, builds }); for (const build of builds.failed.slice(0, this.argv.limit)) { this.queue.push(build); @@ -430,6 +448,9 @@ class WalkCommand extends CICommand { async aggregate() { const { argv, cli } = this; + if (this.queue.length === 0) { + return; + } const aggregator = new FailureAggregator(cli, this.json); this.json = aggregator.aggregate(); cli.log(''); diff --git a/lib/ci/ci_utils.js b/lib/ci/ci_utils.js index 79fa8a87..a442ff2e 100644 --- a/lib/ci/ci_utils.js +++ b/lib/ci/ci_utils.js @@ -50,16 +50,19 @@ function filterBuild(builds, type) { .map(build => parseJobFromURL(build.url)); } -export async function listBuilds(cli, request, type) { +export async function listBuilds(cli, request, type, since) { // assert(type === COMMIT || type === PR) const { jobName } = CI_TYPES.get(type); - const tree = 'builds[url,result]'; + const tree = 'builds[url,result,timestamp]'; const url = `https://${CI_DOMAIN}/job/${jobName}/api/json?tree=${qs.escape(tree)}`; cli.startSpinner(`Querying ${url}`); const result = await request.json(url); - const builds = result.builds; + let builds = result.builds; + if (since) { + builds = builds.filter(build => build.timestamp > since); + } const failed = filterBuild(builds, statusType.FAILURE); const aborted = filterBuild(builds, statusType.ABORTED); const pending = filterBuild(builds, null);