feat(dev-infra): allow local ng-dev configuration to error on invalid commit messages (#38784)
As part of the commit message conformance check, local commit message checks are made to be warnings rather than failures. An additional local option is also in place to allow for the commit message validation failures to be considered errors instead. PR Close #38784
This commit is contained in:
parent
3934f0a833
commit
78e1ecb161
|
@ -6,6 +6,7 @@
|
||||||
* 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 {getUserConfig} from '../utils/config';
|
||||||
|
|
||||||
import {info} from '../utils/console';
|
import {info} from '../utils/console';
|
||||||
|
|
||||||
|
@ -78,11 +79,17 @@ export function buildCommitMessageParser(localYargs: yargs.Argv) {
|
||||||
}
|
}
|
||||||
return file;
|
return file;
|
||||||
},
|
},
|
||||||
|
},
|
||||||
|
'error': {
|
||||||
|
type: 'boolean',
|
||||||
|
description:
|
||||||
|
'Whether invalid commit messages should be treated as failures rather than a warning',
|
||||||
|
default: !!getUserConfig().commitMessage?.errorOnInvalidMessage || !!process.env['CI']
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
args => {
|
args => {
|
||||||
const file = args.file || args['file-env-variable'] || '.git/COMMIT_EDITMSG';
|
const file = args.file || args['file-env-variable'] || '.git/COMMIT_EDITMSG';
|
||||||
validateFile(file);
|
validateFile(file, args.error);
|
||||||
})
|
})
|
||||||
.command(
|
.command(
|
||||||
'validate-range', 'Validate a range of commit messages', {
|
'validate-range', 'Validate a range of commit messages', {
|
||||||
|
|
|
@ -8,29 +8,40 @@
|
||||||
import {readFileSync} from 'fs';
|
import {readFileSync} from 'fs';
|
||||||
import {resolve} from 'path';
|
import {resolve} from 'path';
|
||||||
|
|
||||||
import {getRepoBaseDir} from '../utils/config';
|
import {getRepoBaseDir, getUserConfig} from '../utils/config';
|
||||||
import {error, green, info, red} from '../utils/console';
|
import {error, green, info, log, red, yellow} from '../utils/console';
|
||||||
|
|
||||||
import {deleteCommitMessageDraft, saveCommitMessageDraft} from './commit-message-draft';
|
import {deleteCommitMessageDraft, saveCommitMessageDraft} from './commit-message-draft';
|
||||||
import {printValidationErrors, validateCommitMessage} from './validate';
|
import {printValidationErrors, validateCommitMessage} from './validate';
|
||||||
|
|
||||||
/** Validate commit message at the provided file path. */
|
/** Validate commit message at the provided file path. */
|
||||||
export function validateFile(filePath: string) {
|
export function validateFile(filePath: string, isErrorMode: boolean) {
|
||||||
const commitMessage = readFileSync(resolve(getRepoBaseDir(), filePath), 'utf8');
|
const commitMessage = readFileSync(resolve(getRepoBaseDir(), filePath), 'utf8');
|
||||||
const {valid, errors} = validateCommitMessage(commitMessage);
|
const {valid, errors} = validateCommitMessage(commitMessage);
|
||||||
if (valid) {
|
if (valid) {
|
||||||
info(`${green('√')} Valid commit message`);
|
info(`${green('√')} Valid commit message`);
|
||||||
deleteCommitMessageDraft(filePath);
|
deleteCommitMessageDraft(filePath);
|
||||||
|
process.exitCode = 0;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
error(`${red('✘')} Invalid commit message`);
|
/** Function used to print to the console log. */
|
||||||
printValidationErrors(errors);
|
let printFn = isErrorMode ? error : log;
|
||||||
error('Aborting commit attempt due to invalid commit message.');
|
|
||||||
|
printFn(`${isErrorMode ? red('✘') : yellow('!')} Invalid commit message`);
|
||||||
|
printValidationErrors(errors, printFn);
|
||||||
|
if (isErrorMode) {
|
||||||
|
printFn(red('Aborting commit attempt due to invalid commit message.'));
|
||||||
|
printFn(
|
||||||
|
red('Commit message aborted as failure rather than warning due to local configuration.'));
|
||||||
|
} else {
|
||||||
|
printFn(yellow('Before this commit can be merged into the upstream repository, it must be'));
|
||||||
|
printFn(yellow('amended to follow commit message guidelines.'));
|
||||||
|
}
|
||||||
|
|
||||||
// On all invalid commit messages, the commit message should be saved as a draft to be
|
// On all invalid commit messages, the commit message should be saved as a draft to be
|
||||||
// restored on the next commit attempt.
|
// restored on the next commit attempt.
|
||||||
saveCommitMessageDraft(filePath, commitMessage);
|
saveCommitMessageDraft(filePath, commitMessage);
|
||||||
// If the validation did not return true, exit as a failure.
|
// Set the correct exit code based on if invalid commit message is an error.
|
||||||
process.exit(1);
|
process.exitCode = isErrorMode ? 1 : 0;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue