Joey Perrott 381ea9d7d4 refactor(dev-infra): set up new method for checking range of commits (#41341)
Check a range of commits by retrieving the log files to be parsed with the expected
format for the parser.

This change is in part of a larger set of changes making the process for obtaining
and parsing commits for release note creation and message validation consistent.
This consistency will make it easier to debug as well as ease the design of tooling
which is built on top of these processes.

PR Close #41341
2021-04-01 11:30:26 -07:00

60 lines
2.2 KiB
TypeScript

/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* 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, green, info, red} from '../../utils/console';
import {Commit} from '../parse';
import {getCommitsInRange} from '../utils';
import {printValidationErrors, validateCommitMessage, ValidateCommitMessageOptions} from '../validate';
// Whether the provided commit is a fixup commit.
const isNonFixup = (commit: Commit) => !commit.isFixup;
// Extracts commit header (first line of commit message).
const extractCommitHeader = (commit: Commit) => commit.header;
/** Validate all commits in a provided git commit range. */
export async function validateCommitRange(from: string, to: string) {
/** A list of tuples of the commit header string and a list of error messages for the commit. */
const errors: [commitHeader: string, errors: string[]][] = [];
/** A list of parsed commit messages from the range. */
const commits = await getCommitsInRange(from, to);
info(`Examining ${commits.length} commit(s) in the provided range: ${from}..${to}`);
/**
* Whether all commits in the range are valid, commits are allowed to be fixup commits for other
* commits in the provided commit range.
*/
const allCommitsInRangeValid = commits.every((commit, i) => {
const options: ValidateCommitMessageOptions = {
disallowSquash: true,
nonFixupCommitHeaders: isNonFixup(commit) ?
undefined :
commits.slice(i + 1).filter(isNonFixup).map(extractCommitHeader)
};
const {valid, errors: localErrors} = validateCommitMessage(commit, options);
if (localErrors.length) {
errors.push([commit.header, localErrors]);
}
return valid;
});
if (allCommitsInRangeValid) {
info(green('√ All commit messages in range valid.'));
} else {
error(red('✘ Invalid commit message'));
errors.forEach(([header, validationErrors]) => {
error.group(header);
printValidationErrors(validationErrors);
error.groupEnd();
});
// Exit with a non-zero exit code if invalid commit messages have
// been discovered.
process.exit(1);
}
}