Skip to content

Commit 32a0f68

Browse files
committed
Support multi-line git commits by stopping at the end of the first line.
1 parent bd5a94a commit 32a0f68

File tree

3 files changed

+176
-4
lines changed

3 files changed

+176
-4
lines changed

packages/commitlint-github-utils/src/commitlintGitHubConstants.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export const ISSUE_NUMBERS_PATTERN = /^\((?<issues>.*?)\)(?: (?<type>\S+?):)?(?!
3131

3232
// Allow WIPs to avoid specifying issue number as should only exist on feature branches which are per issue
3333
// Allow either 'WIP: ...', 'WIP - ...', 'WIP2', 'WIP 2', 'WIP 2: ...', 'WIP 2 - ...' etc.
34-
export const WIP_WITHOUT_ISSUE_NUMBER_PATTERN = /^WIP(?:\s*\d+)?(?:(?:\.|:|(?:\s*-))(?<description>.*))?$/;
34+
export const WIP_WITHOUT_ISSUE_NUMBER_PATTERN = /^WIP(?:\s*\d+)?(?:(?:\.|:|(?:\s*-))(?<description>.*))?(?:\n|$)/;
3535
export const WIP_WITH_JUST_ISSUE_NUMBERS_PATTERN = /^\((?<issues>.*?)\) WIP\b/;
3636
export const SUBJECT_PATTERN = /^(?<subjectSeparator>\s*)?(?<subject>.*)/;
3737
export const WIP_TYPE = 'WIP';

packages/commitlint-github-utils/src/tests/parseCommitMessage.test.ts

Lines changed: 72 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
/* eslint-disable prettier/prettier */
22
import parseCommitMessage from '../parseCommitMessage';
33

4-
const COMMIT_MESSAGE = 'Test commit message.';
4+
import {
5+
COMMIT_MESSAGE,
6+
ISSUE_NUMBER,
7+
COMMIT_TYPE,
8+
MULTI_LINE_COMMIT_MESSAGE,
9+
MULTI_LINE_COMMIT_MESSAGE_WITH_TYPE,
10+
MULTI_LINE_WIP_COMMIT_MESSAGES,
11+
MULTI_LINE_WIP_COMMIT_MESSAGE_WITH_ISSUE_NUMBERS,
12+
} from './parserTestData';
513

614
describe('commitlintPluginGitHubTests', () => {
715
it('should return correct issue numbers', () => {
@@ -79,6 +87,14 @@ describe('commitlintPluginGitHubTests', () => {
7987
).toEqual([123, 45]);
8088
});
8189

90+
it('should return correct issue numbers for multi-line commit messages', () => {
91+
[
92+
MULTI_LINE_COMMIT_MESSAGE,
93+
MULTI_LINE_COMMIT_MESSAGE_WITH_TYPE,
94+
MULTI_LINE_WIP_COMMIT_MESSAGE_WITH_ISSUE_NUMBERS,
95+
].forEach(commitMessage => expect(parseCommitMessage(commitMessage).issueNumbers).toEqual([ISSUE_NUMBER]));
96+
});
97+
8298
it('should return raw issue numbers', () => {
8399
// Non-numeric issue numbers prefix
84100
expect(
@@ -103,6 +119,14 @@ describe('commitlintPluginGitHubTests', () => {
103119
).toEqual('#123,#45');
104120
});
105121

122+
it('should return correct raw issue numbers for multi-line commit messages', () => {
123+
[
124+
MULTI_LINE_COMMIT_MESSAGE,
125+
MULTI_LINE_COMMIT_MESSAGE_WITH_TYPE,
126+
MULTI_LINE_WIP_COMMIT_MESSAGE_WITH_ISSUE_NUMBERS,
127+
].forEach(commitMessage => expect(parseCommitMessage(commitMessage).rawIssueNumbers).toEqual(`#${ISSUE_NUMBER}`));
128+
});
129+
106130
it('should return no raw issue numbers', () => {
107131
expect(
108132
parseCommitMessage('My commit message').rawIssueNumbers
@@ -170,6 +194,11 @@ describe('commitlintPluginGitHubTests', () => {
170194
).toEqual('');
171195
});
172196

197+
it('should return no raw issue numbers for multi-line WIP commit messages without them', () => {
198+
MULTI_LINE_WIP_COMMIT_MESSAGES
199+
.forEach(commitMessage => expect(parseCommitMessage(commitMessage).rawIssueNumbers).toBeUndefined);
200+
});
201+
173202
it('should return no issue numbers', () => {
174203
// Missing issue numbers prefix entirely
175204
expect(
@@ -294,7 +323,12 @@ describe('commitlintPluginGitHubTests', () => {
294323
).toEqual([]);
295324
});
296325

297-
it('should return correct type', () => {
326+
it('should return no issue numbers for multi-line WIP commit messages without them', () => {
327+
MULTI_LINE_WIP_COMMIT_MESSAGES
328+
.forEach(commitMessage => expect(parseCommitMessage(commitMessage).issueNumbers).toEqual([]));
329+
});
330+
331+
it('should return the correct type', () => {
298332
expect(
299333
parseCommitMessage(`(#1) ${COMMIT_MESSAGE}`).type
300334
).toBeUndefined();
@@ -320,7 +354,11 @@ describe('commitlintPluginGitHubTests', () => {
320354
).toEqual('chore');
321355
});
322356

323-
it('should return an empty type for WIPs', () => {
357+
it('should return the correct type for multi-line commit messages with them', () => {
358+
expect(parseCommitMessage(MULTI_LINE_COMMIT_MESSAGE_WITH_TYPE).type).toEqual(COMMIT_TYPE);
359+
});
360+
361+
it('should return no type for WIP commit messages', () => {
324362
expect(
325363
parseCommitMessage(`(#123) WIP: ${COMMIT_MESSAGE}`).type
326364
).toBeUndefined();
@@ -382,6 +420,14 @@ describe('commitlintPluginGitHubTests', () => {
382420
).toBeUndefined();
383421
});
384422

423+
it('should return no type for multi-line commit messages without them', () => {
424+
[
425+
MULTI_LINE_COMMIT_MESSAGE,
426+
MULTI_LINE_WIP_COMMIT_MESSAGE_WITH_ISSUE_NUMBERS,
427+
...MULTI_LINE_WIP_COMMIT_MESSAGES,
428+
].forEach(commitMessage => expect(parseCommitMessage(commitMessage).type).toBeUndefined());
429+
});
430+
385431
it('should return correct WIP status', () => {
386432
expect(
387433
parseCommitMessage(`(#1) ${COMMIT_MESSAGE}`).isWip
@@ -505,6 +551,20 @@ describe('commitlintPluginGitHubTests', () => {
505551
).toBe(true);
506552
});
507553

554+
it('should return the correct WIP status for multi-line non-WIP commit messages', () => {
555+
[
556+
MULTI_LINE_COMMIT_MESSAGE,
557+
MULTI_LINE_COMMIT_MESSAGE_WITH_TYPE,
558+
].forEach(commitMessage => expect(parseCommitMessage(commitMessage).isWip).toBe(false));
559+
});
560+
561+
it('should return the correct WIP status for multi-line WIP commit messages', () => {
562+
[
563+
MULTI_LINE_WIP_COMMIT_MESSAGE_WITH_ISSUE_NUMBERS,
564+
...MULTI_LINE_WIP_COMMIT_MESSAGES,
565+
].forEach(commitMessage => expect(parseCommitMessage(commitMessage).isWip).toBe(true));
566+
});
567+
508568
it('should return correct subject', () => {
509569
expect(
510570
parseCommitMessage(`(#1) ${COMMIT_MESSAGE}`).subject
@@ -604,4 +664,13 @@ describe('commitlintPluginGitHubTests', () => {
604664
parseCommitMessage(`WIP: ${COMMIT_MESSAGE} (#22)`).subject
605665
).toEqual(`${COMMIT_MESSAGE} (#22)`);
606666
});
667+
668+
it('should return the correct subject for multi-line commit messages', () => {
669+
[
670+
MULTI_LINE_COMMIT_MESSAGE,
671+
MULTI_LINE_COMMIT_MESSAGE_WITH_TYPE,
672+
MULTI_LINE_WIP_COMMIT_MESSAGE_WITH_ISSUE_NUMBERS,
673+
...MULTI_LINE_WIP_COMMIT_MESSAGES,
674+
].forEach(commitMessage => expect(parseCommitMessage(commitMessage).subject).toEqual(COMMIT_MESSAGE));
675+
});
607676
});
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
export const COMMIT_MESSAGE = 'Test commit message.';
2+
export const ISSUE_NUMBER = 42;
3+
export const COMMIT_TYPE = 'chore';
4+
5+
// Note: We need to trim the beginning of each multiline String to it starts with the first non-whitespace character
6+
// As otherwise it adds a leading line break and whitespace which is invalid.
7+
// We deliberately don't trim the end to leave a trailing new line and ensure that works.
8+
9+
export const MULTI_LINE_COMMIT_MESSAGE = `
10+
(#${ISSUE_NUMBER}) ${COMMIT_MESSAGE}
11+
12+
My commit message description
13+
- SUBTASK-1: I added a new feature
14+
* SUBTASK-2: I fixed a issue
15+
16+
For more information see: https://github.com/conventional-changelog/commitlint/
17+
`.trimStart();
18+
19+
export const MULTI_LINE_COMMIT_MESSAGE_WITH_TYPE = `
20+
(#${ISSUE_NUMBER}) ${COMMIT_TYPE}: ${COMMIT_MESSAGE}
21+
22+
My commit message description
23+
- SUBTASK-1: I added a new feature
24+
* SUBTASK-2: I fixed a issue
25+
26+
For more information see: https://github.com/conventional-changelog/commitlint/
27+
`.trimStart();
28+
29+
export const MULTI_LINE_WIP_COMMIT_MESSAGES = [
30+
`
31+
WIP: ${COMMIT_MESSAGE}
32+
33+
My commit message description
34+
- SUBTASK-1: I added a new feature
35+
* SUBTASK-2: I fixed a issue
36+
37+
For more information see: https://github.com/conventional-changelog/commitlint/
38+
`.trimStart(),
39+
`
40+
WIP2: ${COMMIT_MESSAGE}
41+
42+
My commit message description
43+
- SUBTASK-1: I added a new feature
44+
* SUBTASK-2: I fixed a issue
45+
46+
For more information see: https://github.com/conventional-changelog/commitlint/
47+
`.trimStart(),
48+
`
49+
WIP 2: ${COMMIT_MESSAGE}
50+
51+
My commit message description
52+
- SUBTASK-1: I added a new feature
53+
* SUBTASK-2: I fixed a issue
54+
55+
For more information see: https://github.com/conventional-changelog/commitlint/
56+
`.trimStart(),
57+
`
58+
WIP2:${COMMIT_MESSAGE}
59+
60+
My commit message description
61+
- SUBTASK-1: I added a new feature
62+
* SUBTASK-2: I fixed a issue
63+
64+
For more information see: https://github.com/conventional-changelog/commitlint/
65+
`.trimStart(),
66+
`
67+
WIP 2:${COMMIT_MESSAGE}
68+
69+
My commit message description
70+
- SUBTASK-1: I added a new feature
71+
* SUBTASK-2: I fixed a issue
72+
73+
For more information see: https://github.com/conventional-changelog/commitlint/
74+
`.trimStart(),
75+
`
76+
WIP 2 - ${COMMIT_MESSAGE}
77+
78+
My commit message description
79+
- SUBTASK-1: I added a new feature
80+
* SUBTASK-2: I fixed a issue
81+
82+
For more information see: https://github.com/conventional-changelog/commitlint/
83+
`.trimStart(),
84+
`
85+
WIP2 - ${COMMIT_MESSAGE}
86+
87+
My commit message description
88+
- SUBTASK-1: I added a new feature
89+
* SUBTASK-2: I fixed a issue
90+
91+
For more information see: https://github.com/conventional-changelog/commitlint/
92+
`.trimStart(),
93+
];
94+
95+
export const MULTI_LINE_WIP_COMMIT_MESSAGE_WITH_ISSUE_NUMBERS = `
96+
(#${ISSUE_NUMBER}) WIP: ${COMMIT_MESSAGE}
97+
98+
My commit message description
99+
- SUBTASK-1: I added a new feature
100+
* SUBTASK-2: I fixed a issue
101+
102+
For more information see: https://github.com/conventional-changelog/commitlint/
103+
`.trimStart();

0 commit comments

Comments
 (0)