Skip to content

Commit 2a7611e

Browse files
committed
πŸ› Fix analyze commit on commit with RegExp characters
Refs: #76
1 parent ecec181 commit 2a7611e

File tree

3 files changed

+39
-4
lines changed

3 files changed

+39
-4
lines changed

β€Žlib/helper/parse-commits.jsβ€Ž

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,11 @@ function matchEmoji (emoji) {
3737
return (spec) => spec.emoji === emoji || spec.emoji === emoji + IMAGE_STYLE_SELECTOR
3838
}
3939

40-
function parseGitmoji ({ subject = '', message = '', body = '' } = {}, issues = []) {
40+
function parseGitmoji ({
41+
subject = '',
42+
message = '',
43+
body = ''
44+
} = {}, issues = []) {
4145
subject = emojify(subject.trim())
4246
if (issues.length > 0) {
4347
subject = issues.reduce((acc, curr) => acc.replace(curr.text, ''), subject).trim()
@@ -48,9 +52,24 @@ function parseGitmoji ({ subject = '', message = '', body = '' } = {}, issues =
4852

4953
const gitmoji = matched[0]
5054
const semver = gitmojis.find(matchEmoji(gitmoji))?.semver || 'other'
51-
subject = subject.replace(new RegExp('^' + gitmoji), '')
52-
53-
return { subject, message: subject + '\n\n' + body, gitmoji, semver }
55+
try {
56+
subject = subject.replace(new RegExp('^' + gitmoji), '')
57+
} catch (SyntaxError) {
58+
/**
59+
* The emojiRegex is not bugproof, it may return a regex character
60+
* (like * or .). The best option is to ignore this error
61+
* If there is an error, there is no emoji
62+
*
63+
* @see https://github.com/momocow/semantic-release-gitmoji/issues/76
64+
*/
65+
return null
66+
}
67+
return {
68+
subject,
69+
message: subject + '\n\n' + body,
70+
gitmoji,
71+
semver
72+
}
5473
}
5574

5675
module.exports = function parseCommits (commits = [], mixins = {}, options = {}) {

β€Žtest/integration/analyze-commits.test.jsβ€Ž

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,14 @@ const CASES = [
7171
},
7272
context: getContext('common', { commits: { boring: 2, patch: 4, minor: 2, major: 1 } }),
7373
expectedRelease: 'major'
74+
},
75+
{
76+
name: 'default config + common context w/ star in commit subject',
77+
pluginConfig: {
78+
semver: true
79+
},
80+
context: getContext('common', { commits: { star: 1 } }),
81+
expectedRelease: undefined
7482
}
7583
]
7684

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"commit": {
3+
"short": "commit_short"
4+
},
5+
"message": "*Commit starting with star",
6+
"subject": "*Commit starting with star",
7+
"body": ""
8+
}

0 commit comments

Comments
Β (0)