Skip to content

Commit

Permalink
feat: add --since option to ncu-ci (#649)
Browse files Browse the repository at this point in the history
* feat: add --since option to ncu-ci

Co-authored-by: Michaël Zasso <targos@protonmail.com>
  • Loading branch information
joyeecheung and targos committed Oct 7, 2022
1 parent 36e9f0f commit e01ca12
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
23 changes: 22 additions & 1 deletion bin/ncu-ci.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 <date>', {
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 <date> should be string that can ' +
'be parsed by new Date()');
}
return true;
});
},
handler
Expand Down Expand Up @@ -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);
Expand All @@ -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('');
Expand Down
9 changes: 6 additions & 3 deletions lib/ci/ci_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit e01ca12

Please sign in to comment.