Skip to content

Commit

Permalink
feat: combine multiple commit-lists to one if heading is undefined (#236
Browse files Browse the repository at this point in the history
)

* feat: combine multiple commit-lists to one

* fix: lints and tests

* Tidy up

---------

Co-authored-by: Stefan Nygren <N/A>
Co-authored-by: Pete Cook <[email protected]>
  • Loading branch information
itayo and cookpete authored May 15, 2023
1 parent 37cdfed commit 79acd55
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 13 deletions.
20 changes: 13 additions & 7 deletions src/template.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,23 @@ Handlebars.registerHelper('commit-list', (context, options) => {
return ''
}

const { exclude, message, subject, heading } = options.hash

const list = context
.filter(item => {
const commit = item.commit || item
if (options.hash.exclude) {
const pattern = new RegExp(options.hash.exclude, 'm')
if (exclude) {
const pattern = new RegExp(exclude, 'm')
if (pattern.test(commit.message)) {
return false
}
}
if (options.hash.message) {
const pattern = new RegExp(options.hash.message, 'm')
if (message) {
const pattern = new RegExp(message, 'm')
return pattern.test(commit.message)
}
if (options.hash.subject) {
const pattern = new RegExp(options.hash.subject)
if (subject) {
const pattern = new RegExp(subject)
return pattern.test(commit.subject)
}
return true
Expand All @@ -44,7 +46,11 @@ Handlebars.registerHelper('commit-list', (context, options) => {
return ''
}

return `${options.hash.heading}\n\n${list}`
if (!heading) {
return list
}

return `${heading}\n\n${list}`
})

Handlebars.registerHelper('matches', function (val, pattern, options) {
Expand Down
34 changes: 28 additions & 6 deletions test/commit-list-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ describe('commit-list helper', () => {
const commits = [
{ subject: 'Commit 1', message: 'Commit 1\n\nThis is commit 1, nothing special' },
{ subject: 'Commit 2', message: 'Commit 2\n\nBREAKING CHANGE: This commit breaks something' },
{ subject: 'feat: Commit 3', message: 'feat: Commit 3\n\nThis commit adds a feature' }
{ subject: 'feat: Commit 3', message: 'feat: Commit 3\n\nThis commit adds a feature' },
{ subject: 'fix: Commit 4', message: 'fix: Commit 4\n\nThis commit adds a fix' }
]

const merges = [
{ commit: { subject: 'Commit 1', message: 'Commit 1\n\nThis is commit 1, nothing special' } },
{ commit: { subject: 'Commit 2', message: 'Commit 2\n\nBREAKING CHANGE: This commit breaks something' } },
{ commit: { subject: 'feat: Commit 3', message: 'feat: Commit 3\n\nThis commit adds a feature' } }
{ commit: commits[0] },
{ commit: commits[1] },
{ commit: commits[2] },
{ commit: commits[3] }
]

it('returns nothing with no commits', () => {
Expand All @@ -35,7 +37,9 @@ describe('commit-list helper', () => {
'# Heading\n\n' +
'- Commit 1\n' +
'- Commit 2\n' +
'- feat: Commit 3\n'
'- feat: Commit 3\n' +
'- fix: Commit 4\n'

expect(compile({ commits })).to.equal(expected)
})

Expand Down Expand Up @@ -63,6 +67,23 @@ describe('commit-list helper', () => {
expect(compile({ merges })).to.equal(expected)
})

it('supports commit lists with no heading', () => {
const compile = Handlebars.compile(
'{{#commit-list merges heading="# Heading" subject="^fix: "}}\n' +
'- {{commit.subject}}\n' +
'{{/commit-list}}\n' +
'{{#commit-list commits subject="^fix: "}}\n' +
'- {{subject}}\n' +
'{{/commit-list}}\n'
)
const expectedMerges =
'# Heading\n\n' +
'- fix: Commit 4\n'
const expectedCommits = '- fix: Commit 4\n'
expect(compile({ merges })).to.equal(expectedMerges)
expect(compile({ commits })).to.equal(expectedCommits)
})

it('supports message pattern matching', () => {
const compile = Handlebars.compile(
'{{#commit-list commits heading="# Breaking Changes" message="^BREAKING CHANGE: "}}\n' +
Expand All @@ -84,7 +105,8 @@ describe('commit-list helper', () => {
const expected =
'# Heading\n\n' +
'- Commit 1\n' +
'- feat: Commit 3\n'
'- feat: Commit 3\n' +
'- fix: Commit 4\n'
expect(compile({ commits })).to.equal(expected)
})

Expand Down

0 comments on commit 79acd55

Please sign in to comment.