feat(dev-infra): migrate merge tool to use new logging system (#37232)

Migrate the merge tool in ng-dev to use new logging system rather
than directly calling console.* to create a better experience
for users.

PR Close #37232
This commit is contained in:
Joey Perrott 2020-05-20 14:48:50 -07:00 committed by Matias Niemelä
parent e0002ef9ba
commit e28f13a102
3 changed files with 28 additions and 25 deletions

View File

@ -6,8 +6,10 @@
* found in the LICENSE file at https://angular.io/license * found in the LICENSE file at https://angular.io/license
*/ */
import chalk from 'chalk';
import {Arguments, Argv} from 'yargs'; import {Arguments, Argv} from 'yargs';
import {error, red, yellow} from '../../utils/console';
import {GITHUB_TOKEN_GENERATE_URL, mergePullRequest} from './index'; import {GITHUB_TOKEN_GENERATE_URL, mergePullRequest} from './index';
/** Builds the options for the merge command. */ /** Builds the options for the merge command. */
@ -22,10 +24,9 @@ export function buildMergeCommand(yargs: Argv) {
export async function handleMergeCommand(args: Arguments) { export async function handleMergeCommand(args: Arguments) {
const githubToken = args.githubToken || process.env.GITHUB_TOKEN || process.env.TOKEN; const githubToken = args.githubToken || process.env.GITHUB_TOKEN || process.env.TOKEN;
if (!githubToken) { if (!githubToken) {
console.error( error(red('No Github token set. Please set the `GITHUB_TOKEN` environment variable.'));
chalk.red('No Github token set. Please set the `GITHUB_TOKEN` environment variable.')); error(red('Alternatively, pass the `--github-token` command line flag.'));
console.error(chalk.red('Alternatively, pass the `--github-token` command line flag.')); error(yellow(`You can generate a token here: ${GITHUB_TOKEN_GENERATE_URL}`));
console.error(chalk.yellow(`You can generate a token here: ${GITHUB_TOKEN_GENERATE_URL}`));
process.exit(1); process.exit(1);
} }

View File

@ -8,6 +8,9 @@
import * as Octokit from '@octokit/rest'; import * as Octokit from '@octokit/rest';
import {spawnSync, SpawnSyncOptions, SpawnSyncReturns} from 'child_process'; import {spawnSync, SpawnSyncOptions, SpawnSyncReturns} from 'child_process';
import {info} from '../../utils/console';
import {MergeConfigWithRemote} from './config'; import {MergeConfigWithRemote} from './config';
/** Error for failed Github API requests. */ /** Error for failed Github API requests. */
@ -74,7 +77,7 @@ export class GitClient {
// To improve the debugging experience in case something fails, we print all executed // To improve the debugging experience in case something fails, we print all executed
// Git commands. Note that we do not want to print the token if is contained in the // Git commands. Note that we do not want to print the token if is contained in the
// command. It's common to share errors with others if the tool failed. // command. It's common to share errors with others if the tool failed.
console.info('Executing: git', this.omitGithubTokenFromMessage(args.join(' '))); info('Executing: git', this.omitGithubTokenFromMessage(args.join(' ')));
const result = spawnSync('git', args, { const result = spawnSync('git', args, {
cwd: this._projectRoot, cwd: this._projectRoot,

View File

@ -6,10 +6,9 @@
* found in the LICENSE file at https://angular.io/license * found in the LICENSE file at https://angular.io/license
*/ */
import chalk from 'chalk';
import {getRepoBaseDir} from '../../utils/config'; import {getRepoBaseDir} from '../../utils/config';
import {promptConfirm} from '../../utils/console'; import {error, green, info, promptConfirm, red, yellow} from '../../utils/console';
import {loadAndValidateConfig, MergeConfigWithRemote} from './config'; import {loadAndValidateConfig, MergeConfigWithRemote} from './config';
import {GithubApiRequestError} from './git'; import {GithubApiRequestError} from './git';
@ -40,8 +39,8 @@ export async function mergePullRequest(
if (config === undefined) { if (config === undefined) {
const {config: _config, errors} = loadAndValidateConfig(); const {config: _config, errors} = loadAndValidateConfig();
if (errors) { if (errors) {
console.error(chalk.red('Invalid configuration:')); error(red('Invalid configuration:'));
errors.forEach(desc => console.error(chalk.yellow(` - ${desc}`))); errors.forEach(desc => error(yellow(` - ${desc}`)));
process.exit(1); process.exit(1);
} }
config = _config!; config = _config!;
@ -65,9 +64,9 @@ export async function mergePullRequest(
// Catch errors to the Github API for invalid requests. We want to // Catch errors to the Github API for invalid requests. We want to
// exit the script with a better explanation of the error. // exit the script with a better explanation of the error.
if (e instanceof GithubApiRequestError && e.status === 401) { if (e instanceof GithubApiRequestError && e.status === 401) {
console.error(chalk.red('Github API request failed. ' + e.message)); error(red('Github API request failed. ' + e.message));
console.error(chalk.yellow('Please ensure that your provided token is valid.')); error(yellow('Please ensure that your provided token is valid.'));
console.error(chalk.yellow(`You can generate a token here: ${GITHUB_TOKEN_GENERATE_URL}`)); error(yellow(`You can generate a token here: ${GITHUB_TOKEN_GENERATE_URL}`));
process.exit(1); process.exit(1);
} }
throw e; throw e;
@ -99,25 +98,25 @@ export async function mergePullRequest(
switch (status) { switch (status) {
case MergeStatus.SUCCESS: case MergeStatus.SUCCESS:
console.info(chalk.green(`Successfully merged the pull request: ${prNumber}`)); info(green(`Successfully merged the pull request: ${prNumber}`));
return true; return true;
case MergeStatus.DIRTY_WORKING_DIR: case MergeStatus.DIRTY_WORKING_DIR:
console.error(chalk.red( error(
`Local working repository not clean. Please make sure there are ` + red(`Local working repository not clean. Please make sure there are ` +
`no uncommitted changes.`)); `no uncommitted changes.`));
return false; return false;
case MergeStatus.UNKNOWN_GIT_ERROR: case MergeStatus.UNKNOWN_GIT_ERROR:
console.error(chalk.red( error(
'An unknown Git error has been thrown. Please check the output ' + red('An unknown Git error has been thrown. Please check the output ' +
'above for details.')); 'above for details.'));
return false; return false;
case MergeStatus.FAILED: case MergeStatus.FAILED:
console.error(chalk.yellow(`Could not merge the specified pull request.`)); error(yellow(`Could not merge the specified pull request.`));
console.error(chalk.red(failure!.message)); error(red(failure!.message));
if (canForciblyMerge && !disableForceMergePrompt) { if (canForciblyMerge && !disableForceMergePrompt) {
console.info(); info();
console.info(chalk.yellow('The pull request above failed due to non-critical errors.')); info(yellow('The pull request above failed due to non-critical errors.'));
console.info(chalk.yellow(`This error can be forcibly ignored if desired.`)); info(yellow(`This error can be forcibly ignored if desired.`));
return await promptAndPerformForceMerge(); return await promptAndPerformForceMerge();
} }
return false; return false;