refactor(dev-infra): use conventional-commits-parser for commit parsing (#41286)

Use conventional-commits-parser for parsing commits for validation, this is being done
in anticipation of relying on this parser for release note creation.  Unifying how commits
are parsed will provide the most consistency in our tooling.

PR Close #41286
This commit is contained in:
Joey Perrott
2021-03-19 12:11:21 -07:00
parent eba1289ec9
commit 0516fbb180
16 changed files with 304 additions and 131 deletions
+87 -15
View File
@@ -6,27 +6,40 @@
* found in the LICENSE file at https://angular.io/license
*/
import {parseCommitMessage, ParsedCommitMessage} from './parse';
import {parseCommitMessage} from './parse';
const commitValues = {
prefix: '',
type: 'fix',
npmScope: '',
scope: 'changed-area',
summary: 'This is a short summary of the change',
body: 'This is a longer description of the change Closes #1',
body: 'This is a longer description of the change',
footer: 'Closes #1',
};
function buildCommitMessage(params = {}) {
const {prefix, type, scope, summary, body} = {...commitValues, ...params};
return `${prefix}${type}${scope ? '(' + scope + ')' : ''}: ${summary}\n\n${body}`;
function buildCommitMessage(params: Partial<typeof commitValues> = {}) {
const {prefix, npmScope, type, scope, summary, body, footer} = {...commitValues, ...params};
const scopeSlug = npmScope ? `${npmScope}/${scope}` : scope;
return `${prefix}${type}${scopeSlug ? '(' + scopeSlug + ')' : ''}: ${summary}\n\n${body}\n\n${
footer}`;
}
describe('commit message parsing:', () => {
it('parses the scope', () => {
const message = buildCommitMessage();
expect(parseCommitMessage(message).scope).toBe(commitValues.scope);
describe('parses the scope', () => {
it('when only a scope is defined', () => {
const message = buildCommitMessage();
expect(parseCommitMessage(message).scope).toBe(commitValues.scope);
expect(parseCommitMessage(message).npmScope).toBe('');
});
it('when an npmScope and scope are defined', () => {
const message = buildCommitMessage({npmScope: 'myNpmPackage'});
expect(parseCommitMessage(message).scope).toBe(commitValues.scope);
expect(parseCommitMessage(message).npmScope).toBe('myNpmPackage');
});
});
it('parses the type', () => {
@@ -45,12 +58,6 @@ describe('commit message parsing:', () => {
expect(parseCommitMessage(message).body).toBe(commitValues.body);
});
it('parses the body without Github linking', () => {
const body = 'This has linking\nCloses #1';
const message = buildCommitMessage({body});
expect(parseCommitMessage(message).bodyWithoutLinking).toBe('This has linking\n');
});
it('parses the subject', () => {
const message = buildCommitMessage();
expect(parseCommitMessage(message).subject).toBe(commitValues.summary);
@@ -100,6 +107,71 @@ describe('commit message parsing:', () => {
expect(parsedMessage.body)
.toBe(
'This is line 1 of the actual body.\n' +
'This is line 2 of the actual body (and it also contains a # but it not a comment).\n');
'This is line 2 of the actual body (and it also contains a # but it not a comment).');
});
describe('parses breaking change notes', () => {
const summary = 'This breaks things';
const description = 'This is how it breaks things.';
it('when only a summary is provided', () => {
const message = buildCommitMessage({
footer: `BREAKING CHANGE: ${summary}`,
});
const parsedMessage = parseCommitMessage(message);
expect(parsedMessage.breakingChanges[0].text).toBe(summary);
expect(parsedMessage.breakingChanges.length).toBe(1);
});
it('when only a description is provided', () => {
const message = buildCommitMessage({
footer: `BREAKING CHANGE:\n\n${description}`,
});
const parsedMessage = parseCommitMessage(message);
expect(parsedMessage.breakingChanges[0].text).toBe(description);
expect(parsedMessage.breakingChanges.length).toBe(1);
});
it('when a summary and description are provied', () => {
const message = buildCommitMessage({
footer: `BREAKING CHANGE: ${summary}\n\n${description}`,
});
const parsedMessage = parseCommitMessage(message);
expect(parsedMessage.breakingChanges[0].text).toBe(`${summary}\n\n${description}`);
expect(parsedMessage.breakingChanges.length).toBe(1);
});
});
describe('parses deprecation notes', () => {
const summary = 'This will break things later';
const description = 'This is a long winded explanation of why it \nwill break things later.';
it('when only a summary is provided', () => {
const message = buildCommitMessage({
footer: `DEPRECATED: ${summary}`,
});
const parsedMessage = parseCommitMessage(message);
expect(parsedMessage.deprecations[0].text).toBe(summary);
expect(parsedMessage.deprecations.length).toBe(1);
});
it('when only a description is provided', () => {
const message = buildCommitMessage({
footer: `DEPRECATED:\n\n${description}`,
});
const parsedMessage = parseCommitMessage(message);
expect(parsedMessage.deprecations[0].text).toBe(description);
expect(parsedMessage.deprecations.length).toBe(1);
});
it('when a summary and description are provied', () => {
const message = buildCommitMessage({
footer: `DEPRECATED: ${summary}\n\n${description}`,
});
const parsedMessage = parseCommitMessage(message);
expect(parsedMessage.deprecations[0].text).toBe(`${summary}\n\n${description}`);
expect(parsedMessage.deprecations.length).toBe(1);
});
});
});