2020-03-26 13:45:09 -04:00
|
|
|
/**
|
|
|
|
* @license
|
2020-05-19 15:08:49 -04:00
|
|
|
* Copyright Google LLC All Rights Reserved.
|
2020-03-26 13:45:09 -04:00
|
|
|
*
|
|
|
|
* 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 * as yargs from 'yargs';
|
2020-09-08 17:38:04 -04:00
|
|
|
import {getUserConfig} from '../utils/config';
|
2020-05-20 17:22:29 -04:00
|
|
|
|
|
|
|
import {info} from '../utils/console';
|
|
|
|
|
2020-07-30 17:01:03 -04:00
|
|
|
import {restoreCommitMessage} from './restore-commit-message';
|
2020-03-26 13:45:09 -04:00
|
|
|
import {validateFile} from './validate-file';
|
2020-03-20 15:24:12 -04:00
|
|
|
import {validateCommitRange} from './validate-range';
|
2020-07-29 17:19:15 -04:00
|
|
|
import {runWizard} from './wizard';
|
2020-03-26 13:45:09 -04:00
|
|
|
|
|
|
|
/** Build the parser for the commit-message commands. */
|
|
|
|
export function buildCommitMessageParser(localYargs: yargs.Argv) {
|
2020-03-20 15:24:12 -04:00
|
|
|
return localYargs.help()
|
|
|
|
.strict()
|
2020-07-30 17:01:03 -04:00
|
|
|
.command(
|
2020-08-14 15:20:55 -04:00
|
|
|
'restore-commit-message-draft', false,
|
|
|
|
args => {
|
|
|
|
return args.option('file-env-variable', {
|
2020-07-30 17:01:03 -04:00
|
|
|
type: 'string',
|
2020-08-14 15:20:55 -04:00
|
|
|
array: true,
|
2020-07-30 17:01:03 -04:00
|
|
|
conflicts: ['file'],
|
|
|
|
required: true,
|
|
|
|
description:
|
2020-08-14 15:20:55 -04:00
|
|
|
'The key for the environment variable which holds the arguments for the\n' +
|
|
|
|
'prepare-commit-msg hook as described here:\n' +
|
2020-07-30 17:01:03 -04:00
|
|
|
'https://git-scm.com/docs/githooks#_prepare_commit_msg',
|
|
|
|
coerce: arg => {
|
|
|
|
const [file, source] = (process.env[arg] || '').split(' ');
|
|
|
|
if (!file) {
|
|
|
|
throw new Error(`Provided environment variable "${arg}" was not found.`);
|
|
|
|
}
|
|
|
|
return [file, source];
|
|
|
|
},
|
2020-08-14 15:20:55 -04:00
|
|
|
});
|
2020-07-30 17:01:03 -04:00
|
|
|
},
|
|
|
|
args => {
|
2020-08-14 15:20:55 -04:00
|
|
|
restoreCommitMessage(args['file-env-variable'][0], args['file-env-variable'][1] as any);
|
2020-07-30 17:01:03 -04:00
|
|
|
})
|
2020-07-29 17:19:15 -04:00
|
|
|
.command(
|
|
|
|
'wizard <filePath> [source] [commitSha]', '', ((args: any) => {
|
|
|
|
return args
|
|
|
|
.positional(
|
|
|
|
'filePath',
|
|
|
|
{description: 'The file path to write the generated commit message into'})
|
|
|
|
.positional('source', {
|
|
|
|
choices: ['message', 'template', 'merge', 'squash', 'commit'],
|
|
|
|
description: 'The source of the commit message as described here: ' +
|
|
|
|
'https://git-scm.com/docs/githooks#_prepare_commit_msg'
|
|
|
|
})
|
|
|
|
.positional(
|
|
|
|
'commitSha', {description: 'The commit sha if source is set to `commit`'});
|
|
|
|
}),
|
|
|
|
async (args: any) => {
|
|
|
|
await runWizard(args);
|
|
|
|
})
|
2020-03-20 15:24:12 -04:00
|
|
|
.command(
|
2020-04-08 16:56:05 -04:00
|
|
|
'pre-commit-validate', 'Validate the most recent commit message', {
|
|
|
|
'file': {
|
|
|
|
type: 'string',
|
|
|
|
conflicts: ['file-env-variable'],
|
|
|
|
description: 'The path of the commit message file.',
|
|
|
|
},
|
|
|
|
'file-env-variable': {
|
|
|
|
type: 'string',
|
|
|
|
conflicts: ['file'],
|
|
|
|
description:
|
|
|
|
'The key of the environment variable for the path of the commit message file.',
|
|
|
|
coerce: arg => {
|
|
|
|
const file = process.env[arg];
|
|
|
|
if (!file) {
|
|
|
|
throw new Error(`Provided environment variable "${arg}" was not found.`);
|
|
|
|
}
|
|
|
|
return file;
|
|
|
|
},
|
2020-09-08 17:38:04 -04:00
|
|
|
},
|
|
|
|
'error': {
|
|
|
|
type: 'boolean',
|
|
|
|
description:
|
|
|
|
'Whether invalid commit messages should be treated as failures rather than a warning',
|
|
|
|
default: !!getUserConfig().commitMessage?.errorOnInvalidMessage || !!process.env['CI']
|
2020-04-08 16:56:05 -04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
args => {
|
2020-08-14 15:20:55 -04:00
|
|
|
const file = args.file || args['file-env-variable'] || '.git/COMMIT_EDITMSG';
|
2020-09-08 17:38:04 -04:00
|
|
|
validateFile(file, args.error);
|
2020-03-20 15:24:12 -04:00
|
|
|
})
|
|
|
|
.command(
|
|
|
|
'validate-range', 'Validate a range of commit messages', {
|
|
|
|
'range': {
|
|
|
|
description: 'The range of commits to check, e.g. --range abc123..xyz456',
|
|
|
|
demandOption: ' A range must be provided, e.g. --range abc123..xyz456',
|
|
|
|
type: 'string',
|
|
|
|
requiresArg: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
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') {
|
2020-05-20 17:22:29 -04:00
|
|
|
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`);
|
2020-03-20 15:24:12 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
validateCommitRange(argv.range);
|
|
|
|
});
|
2020-03-26 13:45:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (require.main == module) {
|
|
|
|
buildCommitMessageParser(yargs).parse();
|
|
|
|
}
|