Skip to content

Commit

Permalink
refactor(ng-dev/pr): Migrate discover-new-conflicts and rebase comman…
Browse files Browse the repository at this point in the history
…ds to CommandModules

Use CommandModules for discover-new-conflicts and rebase commands to better align with how all of the
other subcommands are defined.
  • Loading branch information
josephperrott authored and devversion committed May 24, 2022
1 parent 89bffa6 commit a53d820
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 33 deletions.
21 changes: 4 additions & 17 deletions ng-dev/pr/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,18 @@ import * as yargs from 'yargs';

import {CheckTargetBranchesModule} from './check-target-branches/cli';
import {CheckoutCommandModule} from './checkout/cli';
import {
buildDiscoverNewConflictsCommand,
handleDiscoverNewConflictsCommand,
} from './discover-new-conflicts/cli';
import {DiscoverNewConflictsCommandModule} from './discover-new-conflicts/cli';
import {MergeCommandModule} from './merge/cli';
import {buildRebaseCommand, handleRebaseCommand} from './rebase/cli';
import {RebaseCommandModule} from './rebase/cli';

/** Build the parser for pull request commands. */
export function buildPrParser(localYargs: yargs.Argv) {
return localYargs
.help()
.strict()
.demandCommand()
.command(
'discover-new-conflicts <pr>',
'Check if a pending PR causes new conflicts for other pending PRs',
buildDiscoverNewConflictsCommand,
handleDiscoverNewConflictsCommand,
)
.command(
'rebase <pr>',
'Rebase a pending PR and push the rebased commits back to Github',
buildRebaseCommand,
handleRebaseCommand,
)
.command(DiscoverNewConflictsCommandModule)
.command(RebaseCommandModule)
.command(MergeCommandModule)
.command(CheckoutCommandModule)
.command(CheckTargetBranchesModule);
Expand Down
21 changes: 12 additions & 9 deletions ng-dev/pr/discover-new-conflicts/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,21 @@
* found in the LICENSE file at https://angular.io/license
*/

import {Arguments, Argv} from 'yargs';
import {Arguments, Argv, CommandModule} from 'yargs';

import {error} from '../../utils/console';
import {addGithubTokenOption} from '../../utils/git/github-yargs';

import {discoverNewConflictsForPr} from './index';

/** The options available to the discover-new-conflicts command via CLI. */
export interface DiscoverNewConflictsCommandOptions {
export interface DiscoverNewConflictsOptions {
date: number;
pr: number;
}

/** Builds the discover-new-conflicts pull request command. */
export function buildDiscoverNewConflictsCommand(
yargs: Argv,
): Argv<DiscoverNewConflictsCommandOptions> {
function builder(yargs: Argv): Argv<DiscoverNewConflictsOptions> {
return addGithubTokenOption(yargs)
.option('date', {
description: 'Only consider PRs updated since provided date',
Expand All @@ -34,10 +32,7 @@ export function buildDiscoverNewConflictsCommand(
}

/** Handles the discover-new-conflicts pull request command. */
export async function handleDiscoverNewConflictsCommand({
pr,
date,
}: Arguments<DiscoverNewConflictsCommandOptions>) {
async function handler({pr, date}: Arguments<DiscoverNewConflictsOptions>) {
// If a provided date is not able to be parsed, yargs provides it as NaN.
if (isNaN(date)) {
error('Unable to parse the value provided via --date flag');
Expand All @@ -55,3 +50,11 @@ function getThirtyDaysAgoDate() {
date.setDate(date.getDate() - 30);
return date.getTime();
}

/** yargs command module for discovering new conflicts for a PR */
export const DiscoverNewConflictsCommandModule: CommandModule<{}, DiscoverNewConflictsOptions> = {
handler,
builder,
command: 'checkout <pr>',
describe: 'Checkout a PR from the upstream repo',
};
19 changes: 12 additions & 7 deletions ng-dev/pr/rebase/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,32 @@
* found in the LICENSE file at https://angular.io/license
*/

import {Arguments, Argv} from 'yargs';
import {Arguments, Argv, CommandModule} from 'yargs';

import {addGithubTokenOption} from '../../utils/git/github-yargs';

import {rebasePr} from './index';

/** The options available to the rebase command via CLI. */
export interface RebaseCommandOptions {
export interface RebaseOptions {
githubToken: string;
pr: number;
}

/** Builds the rebase pull request command. */
export function buildRebaseCommand(yargs: Argv): Argv<RebaseCommandOptions> {
function builder(yargs: Argv): Argv<RebaseOptions> {
return addGithubTokenOption(yargs).positional('pr', {type: 'number', demandOption: true});
}

/** Handles the rebase pull request command. */
export async function handleRebaseCommand({
pr,
githubToken,
}: Arguments<RebaseCommandOptions>) {
async function handler({pr, githubToken}: Arguments<RebaseOptions>) {
process.exitCode = await rebasePr(pr, githubToken);
}

/** yargs command module for rebaseing a PR */
export const RebaseCommandModule: CommandModule<{}, RebaseOptions> = {
handler,
builder,
command: 'checkout <pr>',
describe: 'Checkout a PR from the upstream repo',
};

0 comments on commit a53d820

Please sign in to comment.