Skip to content

Commit b6fc850

Browse files
author
Eunjae Lee
authored
fix: ignore commits out of convention instead of throwing (#176)
1 parent 50c4813 commit b6fc850

File tree

3 files changed

+68
-26
lines changed

3 files changed

+68
-26
lines changed

packages/shipjs-lib/src/lib/util/__tests__/getNextVersion.spec.js

Lines changed: 47 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@ describe('getNextVersionFromCommitMessages', () => {
1414
test: abc
1515
chore: abc`;
1616
const bodies = '';
17-
const actual = getNextVersionFromCommitMessages(version, titles, bodies);
17+
const { version: actual } = getNextVersionFromCommitMessages(
18+
version,
19+
titles,
20+
bodies
21+
);
1822
expect(actual).toBe('1.2.4');
1923
});
2024

@@ -28,7 +32,11 @@ describe('getNextVersionFromCommitMessages', () => {
2832
test(abcdef): abc
2933
chore(abcdefg): abc`;
3034
const bodies = '';
31-
const actual = getNextVersionFromCommitMessages(version, titles, bodies);
35+
const { version: actual } = getNextVersionFromCommitMessages(
36+
version,
37+
titles,
38+
bodies
39+
);
3240
expect(actual).toBe('1.2.4');
3341
});
3442

@@ -43,7 +51,11 @@ describe('getNextVersionFromCommitMessages', () => {
4351
chore(abcdefg): abc
4452
feat(abcdefgh): abc`;
4553
const bodies = '';
46-
const actual = getNextVersionFromCommitMessages(version, titles, bodies);
54+
const { version: actual } = getNextVersionFromCommitMessages(
55+
version,
56+
titles,
57+
bodies
58+
);
4759
expect(actual).toBe('1.3.0');
4860
});
4961

@@ -58,7 +70,11 @@ describe('getNextVersionFromCommitMessages', () => {
5870
chore: abc
5971
feat: abc`;
6072
const bodies = '';
61-
const actual = getNextVersionFromCommitMessages(version, titles, bodies);
73+
const { version: actual } = getNextVersionFromCommitMessages(
74+
version,
75+
titles,
76+
bodies
77+
);
6278
expect(actual).toBe('1.3.0');
6379
});
6480

@@ -73,25 +89,36 @@ describe('getNextVersionFromCommitMessages', () => {
7389
chore: abc
7490
feat: abc`;
7591
const bodies = 'BREAKING CHANGE: this breaks the previous behavior.';
76-
const actual = getNextVersionFromCommitMessages(version, titles, bodies);
92+
const { version: actual } = getNextVersionFromCommitMessages(
93+
version,
94+
titles,
95+
bodies
96+
);
7797
expect(actual).toBe('1.0.0');
7898
});
7999

80100
it('gets a null with no commit messages', () => {
81101
const version = '0.0.1';
82102
const titles = '';
83103
const bodies = '';
84-
const actual = getNextVersionFromCommitMessages(version, titles, bodies);
104+
const { version: actual } = getNextVersionFromCommitMessages(
105+
version,
106+
titles,
107+
bodies
108+
);
85109
expect(actual).toBe(null);
86110
});
87111

88112
it('throws when there is a commit message out of convention', () => {
89113
const version = '0.0.1';
90114
const titles = `hello: abc`;
91115
const bodies = '';
92-
expect(() => {
93-
getNextVersionFromCommitMessages(version, titles, bodies);
94-
}).toThrow();
116+
const { ignoredMessages } = getNextVersionFromCommitMessages(
117+
version,
118+
titles,
119+
bodies
120+
);
121+
expect(ignoredMessages).toEqual([titles]);
95122
});
96123

97124
it('increases version with postfixes', () => {
@@ -108,7 +135,11 @@ describe('getNextVersionFromCommitMessages', () => {
108135
chore: abc
109136
feat: abc`;
110137
const bodies = '';
111-
const actual = getNextVersionFromCommitMessages(version, titles, bodies);
138+
const { version: actual } = getNextVersionFromCommitMessages(
139+
version,
140+
titles,
141+
bodies
142+
);
112143
expect(actual).toBe(`0.0.1-${tag}.124`);
113144
});
114145
});
@@ -117,32 +148,31 @@ describe('getNextVersionFromCommitMessages', () => {
117148
describe('getNextVersion', () => {
118149
it('gets next version with patch updated', () => {
119150
silentExec('./tests/bootstrap-examples/patch-version-up.sh');
120-
const actual = getNextVersion('sandbox/patch-version-up');
151+
const { version: actual } = getNextVersion('sandbox/patch-version-up');
121152
expect(actual).toBe('0.0.2');
122153
});
123154

124155
it('gets next version with minor updated', () => {
125156
silentExec('./tests/bootstrap-examples/minor-version-up.sh');
126-
const actual = getNextVersion('sandbox/minor-version-up');
157+
const { version: actual } = getNextVersion('sandbox/minor-version-up');
127158
expect(actual).toBe('0.1.0');
128159
});
129160

130161
it('gets next version with major updated', () => {
131162
silentExec('./tests/bootstrap-examples/major-version-up.sh');
132-
const actual = getNextVersion('sandbox/major-version-up');
163+
const { version: actual } = getNextVersion('sandbox/major-version-up');
133164
expect(actual).toBe('1.0.0');
134165
});
135166

136167
it('gets a null with no commit messages', () => {
137168
silentExec('./tests/bootstrap-examples/empty.sh no-commit-log');
138-
const actual = getNextVersion('sandbox/no-commit-log');
169+
const { version: actual } = getNextVersion('sandbox/no-commit-log');
139170
expect(actual).toBe(null);
140171
});
141172

142173
it('throws when there is a commit message out of convention', () => {
143174
silentExec('./tests/bootstrap-examples/out-of-convention.sh');
144-
expect(() => {
145-
getNextVersion('sandbox/out-of-convention');
146-
}).toThrow();
175+
const { ignoredMessages } = getNextVersion('sandbox/out-of-convention');
176+
expect(ignoredMessages).toEqual(['hello: add a']);
147177
});
148178
});

packages/shipjs-lib/src/lib/util/getNextVersion.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@ import silentExec from '../shell/silentExec';
99

1010
export function getNextVersionFromCommitMessages(version, titles, bodies) {
1111
if (prerelease(version)) {
12-
return inc(version, 'prerelease');
12+
return { version: inc(version, 'prerelease') };
1313
}
1414
if (bodies.toUpperCase().includes(GIT_COMMIT_BREAKING_CHANGE)) {
15-
return inc(version, 'major');
15+
return { version: inc(version, 'major') };
1616
}
1717
let patch = false;
1818
let minor = false;
19+
const ignoredMessages = [];
1920
titles.split('\n').forEach(rawTitle => {
2021
const title = rawTitle.trim();
2122
if (!title) {
@@ -26,24 +27,25 @@ export function getNextVersionFromCommitMessages(version, titles, bodies) {
2627
}
2728
const match = title.match(/(.*?)(\(.*?\))?:.*/);
2829
if (!match || !match[1]) {
29-
throw new Error(`Invalid commit message format.\n > ${title}`);
30+
ignoredMessages.push(title);
31+
return;
3032
}
3133
const prefix = match[1].toLowerCase();
3234
if (GIT_COMMIT_PREFIX_PATCH.has(prefix)) {
3335
patch = true;
3436
} else if (GIT_COMMIT_PREFIX_MINOR.has(prefix)) {
3537
minor = true;
3638
} else {
37-
throw new Error(`Out of convention.\n > ${title}`);
39+
ignoredMessages.push(title);
3840
}
3941
});
4042

4143
if (minor) {
42-
return inc(version, 'minor');
44+
return { version: inc(version, 'minor'), ignoredMessages };
4345
} else if (patch) {
44-
return inc(version, 'patch');
46+
return { version: inc(version, 'patch'), ignoredMessages };
4547
} else {
46-
return null;
48+
return { version: null, ignoredMessages };
4749
}
4850
}
4951

packages/shipjs/src/step/prepare/getNextVersion.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,18 @@ import runStep from '../runStep';
44
export default ({ dir }) =>
55
runStep(
66
{ title: 'Calculating the next version.' },
7-
({ print, error, exitProcess }) => {
8-
const nextVersion = getNextVersion(dir);
7+
({ print, warning, error, exitProcess }) => {
8+
const { version: nextVersion, ignoredMessages = [] } = getNextVersion(
9+
dir
10+
);
11+
if (ignoredMessages.length > 0) {
12+
print(
13+
warning(
14+
'The following commit messages out of convention are ignored:'
15+
)
16+
);
17+
ignoredMessages.forEach(message => print(` ${message}`));
18+
}
919
if (nextVersion === null) {
1020
print(error('Nothing to release!'));
1121
exitProcess(1);

0 commit comments

Comments
 (0)