2020-03-10 13:29:44 -04:00
|
|
|
/**
|
|
|
|
* @license
|
2020-05-19 15:08:49 -04:00
|
|
|
* Copyright Google LLC All Rights Reserved.
|
2020-03-10 13:29:44 -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
|
|
|
|
*/
|
2020-05-20 17:22:29 -04:00
|
|
|
import {error} from '../utils/console';
|
|
|
|
|
2020-05-08 17:51:29 -04:00
|
|
|
import {getCommitMessageConfig} from './config';
|
2020-08-12 12:40:37 -04:00
|
|
|
import {parseCommitMessage} from './parse';
|
2020-03-10 13:29:44 -04:00
|
|
|
|
2020-03-20 15:24:12 -04:00
|
|
|
/** Options for commit message validation. */
|
|
|
|
export interface ValidateCommitMessageOptions {
|
|
|
|
disallowSquash?: boolean;
|
|
|
|
nonFixupCommitHeaders?: string[];
|
|
|
|
}
|
|
|
|
|
2020-08-12 12:40:37 -04:00
|
|
|
/** Regex matching a URL for an entire commit body line. */
|
2020-07-07 09:58:11 -04:00
|
|
|
const COMMIT_BODY_URL_LINE_RE = /^https?:\/\/.*$/;
|
2020-03-10 13:29:44 -04:00
|
|
|
|
|
|
|
/** Validate a commit message against using the local repo's config. */
|
|
|
|
export function validateCommitMessage(
|
2020-03-20 15:24:12 -04:00
|
|
|
commitMsg: string, options: ValidateCommitMessageOptions = {}) {
|
2020-05-20 17:22:29 -04:00
|
|
|
function printError(errorMessage: string) {
|
|
|
|
error(
|
2020-03-10 13:29:44 -04:00
|
|
|
`INVALID COMMIT MSG: \n` +
|
|
|
|
`${'─'.repeat(40)}\n` +
|
|
|
|
`${commitMsg}\n` +
|
|
|
|
`${'─'.repeat(40)}\n` +
|
|
|
|
`ERROR: \n` +
|
|
|
|
` ${errorMessage}` +
|
|
|
|
`\n\n` +
|
|
|
|
`The expected format for a commit is: \n` +
|
|
|
|
`<type>(<scope>): <subject>\n\n<body>`);
|
|
|
|
}
|
|
|
|
|
2020-05-08 17:51:29 -04:00
|
|
|
const config = getCommitMessageConfig().commitMessage;
|
2020-03-10 13:29:44 -04:00
|
|
|
const commit = parseCommitMessage(commitMsg);
|
|
|
|
|
2020-04-14 18:25:05 -04:00
|
|
|
////////////////////////////////////
|
|
|
|
// Checking revert, squash, fixup //
|
|
|
|
////////////////////////////////////
|
2020-04-06 16:18:59 -04:00
|
|
|
|
|
|
|
// All revert commits are considered valid.
|
2020-03-10 13:29:44 -04:00
|
|
|
if (commit.isRevert) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-04-06 16:18:59 -04:00
|
|
|
// All squashes are considered valid, as the commit will be squashed into another in
|
|
|
|
// the git history anyway, unless the options provided to not allow squash commits.
|
|
|
|
if (commit.isSquash) {
|
|
|
|
if (options.disallowSquash) {
|
2020-05-20 17:22:29 -04:00
|
|
|
printError('The commit must be manually squashed into the target commit');
|
2020-04-06 16:18:59 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2020-03-10 13:29:44 -04:00
|
|
|
}
|
|
|
|
|
2020-04-06 16:18:59 -04:00
|
|
|
// Fixups commits are considered valid, unless nonFixupCommitHeaders are provided to check
|
|
|
|
// against. If `nonFixupCommitHeaders` is not empty, we check whether there is a corresponding
|
|
|
|
// non-fixup commit (i.e. a commit whose header is identical to this commit's header after
|
|
|
|
// stripping the `fixup! ` prefix), otherwise we assume this verification will happen in another
|
|
|
|
// check.
|
|
|
|
if (commit.isFixup) {
|
|
|
|
if (options.nonFixupCommitHeaders && !options.nonFixupCommitHeaders.includes(commit.header)) {
|
2020-05-20 17:22:29 -04:00
|
|
|
printError(
|
2020-03-10 13:29:44 -04:00
|
|
|
'Unable to find match for fixup commit among prior commits: ' +
|
2020-03-20 15:24:12 -04:00
|
|
|
(options.nonFixupCommitHeaders.map(x => `\n ${x}`).join('') || '-'));
|
2020-03-10 13:29:44 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-04-14 18:25:05 -04:00
|
|
|
////////////////////////////
|
|
|
|
// Checking commit header //
|
|
|
|
////////////////////////////
|
2020-03-10 13:29:44 -04:00
|
|
|
if (commit.header.length > config.maxLineLength) {
|
2020-05-20 17:22:29 -04:00
|
|
|
printError(`The commit message header is longer than ${config.maxLineLength} characters`);
|
2020-03-10 13:29:44 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!commit.type) {
|
2020-05-20 17:22:29 -04:00
|
|
|
printError(`The commit message header does not match the expected format.`);
|
2020-03-10 13:29:44 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!config.types.includes(commit.type)) {
|
2020-05-20 17:22:29 -04:00
|
|
|
printError(`'${commit.type}' is not an allowed type.\n => TYPES: ${config.types.join(', ')}`);
|
2020-03-10 13:29:44 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (commit.scope && !config.scopes.includes(commit.scope)) {
|
2020-05-20 17:22:29 -04:00
|
|
|
printError(
|
|
|
|
`'${commit.scope}' is not an allowed scope.\n => SCOPES: ${config.scopes.join(', ')}`);
|
2020-03-10 13:29:44 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-05-14 12:34:30 -04:00
|
|
|
// Commits with the type of `release` do not require a commit body.
|
|
|
|
if (commit.type === 'release') {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-04-14 18:25:05 -04:00
|
|
|
//////////////////////////
|
|
|
|
// Checking commit body //
|
|
|
|
//////////////////////////
|
|
|
|
|
2020-06-25 20:39:55 -04:00
|
|
|
if (!config.minBodyLengthTypeExcludes?.includes(commit.type) &&
|
|
|
|
commit.bodyWithoutLinking.trim().length < config.minBodyLength) {
|
2020-05-20 17:22:29 -04:00
|
|
|
printError(`The commit message body does not meet the minimum length of ${
|
2020-03-20 15:24:12 -04:00
|
|
|
config.minBodyLength} characters`);
|
2020-03-10 13:29:44 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const bodyByLine = commit.body.split('\n');
|
2020-07-07 09:58:11 -04:00
|
|
|
const lineExceedsMaxLength = bodyByLine.some(line => {
|
|
|
|
// Check if any line exceeds the max line length limit. The limit is ignored for
|
|
|
|
// lines that just contain an URL (as these usually cannot be wrapped or shortened).
|
|
|
|
return line.length > config.maxLineLength && !COMMIT_BODY_URL_LINE_RE.test(line);
|
|
|
|
});
|
|
|
|
|
|
|
|
if (lineExceedsMaxLength) {
|
2020-05-20 17:22:29 -04:00
|
|
|
printError(
|
2020-06-25 20:39:55 -04:00
|
|
|
`The commit message body contains lines greater than ${config.maxLineLength} characters`);
|
2020-03-10 13:29:44 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|