2020-05-20 19:04:46 -04:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright Google LLC All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
*/
|
|
|
|
|
2020-05-15 11:21:01 -04:00
|
|
|
import {Arguments, Argv} from 'yargs';
|
|
|
|
|
2020-05-20 17:28:27 -04:00
|
|
|
import {error} from '../../utils/console';
|
|
|
|
|
2020-05-15 11:21:01 -04:00
|
|
|
import {discoverNewConflictsForPr} from './index';
|
|
|
|
|
2020-08-14 15:20:55 -04:00
|
|
|
/** The options available to the discover-new-conflicts command via CLI. */
|
|
|
|
export interface DiscoverNewConflictsCommandOptions {
|
|
|
|
date: number;
|
|
|
|
'pr-number': number;
|
|
|
|
}
|
|
|
|
|
2020-05-15 11:21:01 -04:00
|
|
|
/** Builds the discover-new-conflicts pull request command. */
|
2020-08-14 15:20:55 -04:00
|
|
|
export function buildDiscoverNewConflictsCommand(yargs: Argv):
|
|
|
|
Argv<DiscoverNewConflictsCommandOptions> {
|
|
|
|
return yargs
|
|
|
|
.option('date', {
|
|
|
|
description: 'Only consider PRs updated since provided date',
|
|
|
|
defaultDescription: '30 days ago',
|
|
|
|
coerce: (date) => typeof date === 'number' ? date : Date.parse(date),
|
|
|
|
default: getThirtyDaysAgoDate(),
|
|
|
|
})
|
|
|
|
.positional('pr-number', {demandOption: true, type: 'number'});
|
2020-05-15 11:21:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Handles the discover-new-conflicts pull request command. */
|
2020-08-14 15:20:55 -04:00
|
|
|
export async function handleDiscoverNewConflictsCommand(
|
|
|
|
{'pr-number': prNumber, date}: Arguments<DiscoverNewConflictsCommandOptions>) {
|
2020-05-15 11:21:01 -04:00
|
|
|
// If a provided date is not able to be parsed, yargs provides it as NaN.
|
|
|
|
if (isNaN(date)) {
|
2020-05-20 17:28:27 -04:00
|
|
|
error('Unable to parse the value provided via --date flag');
|
2020-05-15 11:21:01 -04:00
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
await discoverNewConflictsForPr(prNumber, date);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Gets a date object 30 days ago from today. */
|
2020-08-14 15:20:55 -04:00
|
|
|
function getThirtyDaysAgoDate() {
|
2020-05-15 11:21:01 -04:00
|
|
|
const date = new Date();
|
|
|
|
// Set the hours, minutes and seconds to 0 to only consider date.
|
|
|
|
date.setHours(0, 0, 0, 0);
|
|
|
|
// Set the date to 30 days in the past.
|
|
|
|
date.setDate(date.getDate() - 30);
|
2020-08-14 15:20:55 -04:00
|
|
|
return date.getTime();
|
2020-05-15 11:21:01 -04:00
|
|
|
}
|