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/jasmine",
"@npm//@types/node",
"@npm//inquirer",
],
)

View File

@ -6,6 +6,9 @@
* found in the LICENSE file at https://angular.io/license
*/
import * as yargs from 'yargs';
import {info} from '../utils/console';
import {validateFile} from './validate-file';
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
// being run on is an upstream branch.
if (process.env['CI'] && process.env['CI_PULL_REQUEST'] === 'false') {
console.info(
`Since valid commit messages are enforced by PR linting on CI, we do not\n` +
`need to validate commit messages on CI runs on upstream branches.\n\n` +
`Skipping check of provided commit range`);
info(`Since valid commit messages are enforced by PR linting on CI, we do not`);
info(`need to validate commit messages on CI runs on upstream branches.`);
info();
info(`Skipping check of provided commit range`);
return;
}
validateCommitRange(argv.range);

View File

@ -9,6 +9,7 @@ import {readFileSync} from 'fs';
import {resolve} from 'path';
import {getRepoBaseDir} from '../utils/config';
import {info} from '../utils/console';
import {validateCommitMessage} from './validate';
@ -16,7 +17,7 @@ import {validateCommitMessage} from './validate';
export function validateFile(filePath: string) {
const commitMessage = readFileSync(resolve(getRepoBaseDir(), filePath), 'utf8');
if (validateCommitMessage(commitMessage)) {
console.info('√ Valid commit message');
info('√ Valid commit message');
return;
}
// 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
*/
import {exec} from 'shelljs';
import {info} from '../utils/console';
import {parseCommitMessage, validateCommitMessage, ValidateCommitMessageOptions} from './validate';
// 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
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
// commits in the provided commit range.
@ -46,7 +49,7 @@ export function validateCommitRange(range: string) {
});
if (allCommitsInRangeValid) {
console.info('√ All commit messages in range valid.');
info('√ All commit messages in range valid.');
} else {
// Exit with a non-zero exit code if invalid commit messages have
// been discovered.

View File

@ -5,6 +5,8 @@
* 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
*/
import {error} from '../utils/console';
import {getCommitMessageConfig} from './config';
/** 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. */
export function validateCommitMessage(
commitMsg: string, options: ValidateCommitMessageOptions = {}) {
function error(errorMessage: string) {
console.error(
function printError(errorMessage: string) {
error(
`INVALID COMMIT MSG: \n` +
`${'─'.repeat(40)}\n` +
`${commitMsg}\n` +
@ -91,7 +93,7 @@ export function validateCommitMessage(
// the git history anyway, unless the options provided to not allow squash commits.
if (commit.isSquash) {
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 true;
@ -104,7 +106,7 @@ export function validateCommitMessage(
// check.
if (commit.isFixup) {
if (options.nonFixupCommitHeaders && !options.nonFixupCommitHeaders.includes(commit.header)) {
error(
printError(
'Unable to find match for fixup commit among prior commits: ' +
(options.nonFixupCommitHeaders.map(x => `\n ${x}`).join('') || '-'));
return false;
@ -117,22 +119,23 @@ export function validateCommitMessage(
// Checking commit header //
////////////////////////////
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;
}
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;
}
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;
}
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;
}
@ -146,14 +149,14 @@ export function validateCommitMessage(
//////////////////////////
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`);
return false;
}
const bodyByLine = commit.body.split('\n');
if (bodyByLine.some(line => line.length > config.maxLineLength)) {
error(
printError(
`The commit messsage body contains lines greater than ${config.maxLineLength} characters`);
return false;
}