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

Migrate the commit-message 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:22:29 -07:00 committed by Matias Niemelä
parent 40e357411e
commit 171d967993
5 changed files with 28 additions and 17 deletions

View File

@ -32,6 +32,7 @@ ts_library(
"@npm//@types/events", "@npm//@types/events",
"@npm//@types/jasmine", "@npm//@types/jasmine",
"@npm//@types/node", "@npm//@types/node",
"@npm//inquirer",
], ],
) )

View File

@ -6,6 +6,9 @@
* found in the LICENSE file at https://angular.io/license * found in the LICENSE file at https://angular.io/license
*/ */
import * as yargs from 'yargs'; import * as yargs from 'yargs';
import {info} from '../utils/console';
import {validateFile} from './validate-file'; import {validateFile} from './validate-file';
import {validateCommitRange} from './validate-range'; import {validateCommitRange} from './validate-range';
@ -51,10 +54,10 @@ export function buildCommitMessageParser(localYargs: yargs.Argv) {
// If on CI, and not pull request number is provided, assume the branch // If on CI, and not pull request number is provided, assume the branch
// being run on is an upstream branch. // being run on is an upstream branch.
if (process.env['CI'] && process.env['CI_PULL_REQUEST'] === 'false') { if (process.env['CI'] && process.env['CI_PULL_REQUEST'] === 'false') {
console.info( info(`Since valid commit messages are enforced by PR linting on CI, we do not`);
`Since valid commit messages are enforced by PR linting on CI, we do not\n` + info(`need to validate commit messages on CI runs on upstream branches.`);
`need to validate commit messages on CI runs on upstream branches.\n\n` + info();
`Skipping check of provided commit range`); info(`Skipping check of provided commit range`);
return; return;
} }
validateCommitRange(argv.range); validateCommitRange(argv.range);

View File

@ -9,6 +9,7 @@ import {readFileSync} from 'fs';
import {resolve} from 'path'; import {resolve} from 'path';
import {getRepoBaseDir} from '../utils/config'; import {getRepoBaseDir} from '../utils/config';
import {info} from '../utils/console';
import {validateCommitMessage} from './validate'; import {validateCommitMessage} from './validate';
@ -16,7 +17,7 @@ import {validateCommitMessage} from './validate';
export function validateFile(filePath: string) { export function validateFile(filePath: string) {
const commitMessage = readFileSync(resolve(getRepoBaseDir(), filePath), 'utf8'); const commitMessage = readFileSync(resolve(getRepoBaseDir(), filePath), 'utf8');
if (validateCommitMessage(commitMessage)) { if (validateCommitMessage(commitMessage)) {
console.info('√ Valid commit message'); info('√ Valid commit message');
return; return;
} }
// If the validation did not return true, exit as a failure. // If the validation did not return true, exit as a failure.

View File

@ -6,6 +6,9 @@
* found in the LICENSE file at https://angular.io/license * found in the LICENSE file at https://angular.io/license
*/ */
import {exec} from 'shelljs'; import {exec} from 'shelljs';
import {info} from '../utils/console';
import {parseCommitMessage, validateCommitMessage, ValidateCommitMessageOptions} from './validate'; import {parseCommitMessage, validateCommitMessage, ValidateCommitMessageOptions} from './validate';
// Whether the provided commit is a fixup commit. // Whether the provided commit is a fixup commit.
@ -31,7 +34,7 @@ export function validateCommitRange(range: string) {
// Separate the commits from a single string into individual commits // Separate the commits from a single string into individual commits
const commits = result.split(randomValueSeparator).map(l => l.trim()).filter(line => !!line); const commits = result.split(randomValueSeparator).map(l => l.trim()).filter(line => !!line);
console.info(`Examining ${commits.length} commit(s) in the provided range: ${range}`); info(`Examining ${commits.length} commit(s) in the provided range: ${range}`);
// Check each commit in the commit range. Commits are allowed to be fixup commits for other // Check each commit in the commit range. Commits are allowed to be fixup commits for other
// commits in the provided commit range. // commits in the provided commit range.
@ -46,7 +49,7 @@ export function validateCommitRange(range: string) {
}); });
if (allCommitsInRangeValid) { if (allCommitsInRangeValid) {
console.info('√ All commit messages in range valid.'); info('√ All commit messages in range valid.');
} else { } else {
// Exit with a non-zero exit code if invalid commit messages have // Exit with a non-zero exit code if invalid commit messages have
// been discovered. // been discovered.

View File

@ -5,6 +5,8 @@
* Use of this source code is governed by an MIT-style license that can be * 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 * found in the LICENSE file at https://angular.io/license
*/ */
import {error} from '../utils/console';
import {getCommitMessageConfig} from './config'; import {getCommitMessageConfig} from './config';
/** Options for commit message validation. */ /** Options for commit message validation. */
@ -62,8 +64,8 @@ export function parseCommitMessage(commitMsg: string) {
/** Validate a commit message against using the local repo's config. */ /** Validate a commit message against using the local repo's config. */
export function validateCommitMessage( export function validateCommitMessage(
commitMsg: string, options: ValidateCommitMessageOptions = {}) { commitMsg: string, options: ValidateCommitMessageOptions = {}) {
function error(errorMessage: string) { function printError(errorMessage: string) {
console.error( error(
`INVALID COMMIT MSG: \n` + `INVALID COMMIT MSG: \n` +
`${'─'.repeat(40)}\n` + `${'─'.repeat(40)}\n` +
`${commitMsg}\n` + `${commitMsg}\n` +
@ -91,7 +93,7 @@ export function validateCommitMessage(
// the git history anyway, unless the options provided to not allow squash commits. // the git history anyway, unless the options provided to not allow squash commits.
if (commit.isSquash) { if (commit.isSquash) {
if (options.disallowSquash) { if (options.disallowSquash) {
error('The commit must be manually squashed into the target commit'); printError('The commit must be manually squashed into the target commit');
return false; return false;
} }
return true; return true;
@ -104,7 +106,7 @@ export function validateCommitMessage(
// check. // check.
if (commit.isFixup) { if (commit.isFixup) {
if (options.nonFixupCommitHeaders && !options.nonFixupCommitHeaders.includes(commit.header)) { if (options.nonFixupCommitHeaders && !options.nonFixupCommitHeaders.includes(commit.header)) {
error( printError(
'Unable to find match for fixup commit among prior commits: ' + 'Unable to find match for fixup commit among prior commits: ' +
(options.nonFixupCommitHeaders.map(x => `\n ${x}`).join('') || '-')); (options.nonFixupCommitHeaders.map(x => `\n ${x}`).join('') || '-'));
return false; return false;
@ -117,22 +119,23 @@ export function validateCommitMessage(
// Checking commit header // // Checking commit header //
//////////////////////////// ////////////////////////////
if (commit.header.length > config.maxLineLength) { if (commit.header.length > config.maxLineLength) {
error(`The commit message header is longer than ${config.maxLineLength} characters`); printError(`The commit message header is longer than ${config.maxLineLength} characters`);
return false; return false;
} }
if (!commit.type) { if (!commit.type) {
error(`The commit message header does not match the expected format.`); printError(`The commit message header does not match the expected format.`);
return false; return false;
} }
if (!config.types.includes(commit.type)) { if (!config.types.includes(commit.type)) {
error(`'${commit.type}' is not an allowed type.\n => TYPES: ${config.types.join(', ')}`); printError(`'${commit.type}' is not an allowed type.\n => TYPES: ${config.types.join(', ')}`);
return false; return false;
} }
if (commit.scope && !config.scopes.includes(commit.scope)) { if (commit.scope && !config.scopes.includes(commit.scope)) {
error(`'${commit.scope}' is not an allowed scope.\n => SCOPES: ${config.scopes.join(', ')}`); printError(
`'${commit.scope}' is not an allowed scope.\n => SCOPES: ${config.scopes.join(', ')}`);
return false; return false;
} }
@ -146,14 +149,14 @@ export function validateCommitMessage(
////////////////////////// //////////////////////////
if (commit.bodyWithoutLinking.trim().length < config.minBodyLength) { if (commit.bodyWithoutLinking.trim().length < config.minBodyLength) {
error(`The commit message body does not meet the minimum length of ${ printError(`The commit message body does not meet the minimum length of ${
config.minBodyLength} characters`); config.minBodyLength} characters`);
return false; return false;
} }
const bodyByLine = commit.body.split('\n'); const bodyByLine = commit.body.split('\n');
if (bodyByLine.some(line => line.length > config.maxLineLength)) { if (bodyByLine.some(line => line.length > config.maxLineLength)) {
error( printError(
`The commit messsage body contains lines greater than ${config.maxLineLength} characters`); `The commit messsage body contains lines greater than ${config.maxLineLength} characters`);
return false; return false;
} }