Skip to content

Commit

Permalink
Add commit-list template helper
Browse files Browse the repository at this point in the history
Fixes #36 and fixes #39
  • Loading branch information
cookpete committed Apr 17, 2018
1 parent f12401a commit 77bb243
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 6 deletions.
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,33 @@ To see exactly what data is passed in to the templates, you can generate a JSON
auto-changelog --template json --output changelog-data.json
```

#### `commit-list` helper

Use `{{#commit-list}}` to render a list of commits depending on certain patterns in the commit messages:

```hbs
{{#each releases}}
### [{{title}}]({{href}})
{{! List commits with `Breaking change: ` somewhere in the message }}
{{#commit-list commits heading='### Breaking Changes' message='Breaking change: '}}
- {{subject}} [`{{shorthash}}`]({{href}})
{{/commit-list}}
{{! List commits that add new features, but not those already listed above }}
{{#commit-list commits heading='### New Features' message='feat: ' exclude='Breaking change: '}}
- {{subject}} [`{{shorthash}}`]({{href}})
{{/commit-list}}
{{/each}}
```

| Option | Description |
| --------- | ----------- |
| `heading` | A heading for the list, only renders if at least one commit matches |
| `message` | A regex pattern to match against the entire commit message |
| `subject` | A regex pattern to match against the commit subject only |
| `exclude` | A regex pattern to exclude from the list – useful for avoiding listing commits more than once |

#### Custom issue patterns

By default, `auto-changelog` will parse [GitHub-style issue fixes](https://help.github.com/articles/closing-issues-using-keywords/) in your commit messages. If you use Jira or an alternative pattern in your commits to reference issues, you can pass in a custom regular expression to `--issue-pattern` along with `--issue-url`:
Expand Down
33 changes: 33 additions & 0 deletions src/template.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,39 @@ Handlebars.registerHelper('json', function (object) {
return new Handlebars.SafeString(JSON.stringify(object, null, 2))
})

Handlebars.registerHelper('commit-list', function (context, options) {
if (!context || context.length === 0) {
return ''
}

const list = context
.filter(commit => {
if (options.hash.exclude) {
const pattern = new RegExp(options.hash.exclude, 'm')
if (pattern.test(commit.message)) {
return false
}
}
if (options.hash.message) {
const pattern = new RegExp(options.hash.message, 'm')
return pattern.test(commit.message)
}
if (options.hash.subject) {
const pattern = new RegExp(options.hash.subject)
return pattern.test(commit.subject)
}
return true
})
.map(item => options.fn(item))
.join('')

if (!list) {
return ''
}

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

async function getTemplate (template) {
if (await pathExists(template)) {
return readFile(template, 'utf-8')
Expand Down
9 changes: 3 additions & 6 deletions templates/keepachangelog.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,8 @@ Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog).
{{/each}}

{{/if}}
{{#if commits}}
### Commits
{{#each commits}}
- {{subject}} {{#if href}}[`{{shorthash}}`]({{href}}){{/if}}
{{/each}}
{{#commit-list commits heading='### Commits'}}
- {{subject}} {{#if href}}[`{{shorthash}}`]({{href}}){{/if}}
{{/commit-list}}

{{/if}}
{{/each}}
1 change: 1 addition & 0 deletions test/data/template-keepachangelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog).
### Fixed
- Commit 4 fixes #4 in the subject [`#4`](https://github.com/user/repo/issues/4)


## v0.0.1 - 2015-12-15
### Merged
- Third commit with same name as PR [`#3`](https://github.com/user/repo/pull/3)
Expand Down
82 changes: 82 additions & 0 deletions test/template.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ import { describe, it } from 'mocha'
import { expect } from 'chai'
import { readFile } from 'fs-extra'
import { join } from 'path'
import Handlebars from 'handlebars'

import releases from './data/releases'
import commits from './data/commits'
import { compileTemplate } from '../src/template'
import { removeIndentation } from '../src/utils'

describe('compileTemplate', () => {
it('compiles using compact template', async () => {
Expand Down Expand Up @@ -34,3 +37,82 @@ describe('compileTemplate', () => {
.catch(() => done())
})
})

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' }
]

it('returns nothing with no commits', () => {
const compile = Handlebars.compile(
'{{#commit-list commits heading="# Heading"}}\n' +
'- {{subject}}\n' +
'{{/commit-list}}'
)
const expected = ''
expect(compile({ commits: [] })).to.equal(expected)
})

it('returns all commits with no options', () => {
const compile = Handlebars.compile(
'{{#commit-list commits heading="# Heading"}}\n' +
'- {{subject}}\n' +
'{{/commit-list}}'
)
const expected =
'# Heading\n' +
'- Commit 1\n' +
'- Commit 2\n' +
'- feat: Commit 3\n'
expect(compile({ commits })).to.equal(expected)
})

it('supports subject pattern matching', () => {
const compile = Handlebars.compile(
'{{#commit-list commits heading="# Heading" subject="^feat: "}}\n' +
'- {{subject}}\n' +
'{{/commit-list}}'
)
const expected =
'# Heading\n' +
'- feat: Commit 3\n'
expect(compile({ commits })).to.equal(expected)
})

it('supports message pattern matching', () => {
const compile = Handlebars.compile(
'{{#commit-list commits heading="# Breaking Changes" message="^BREAKING CHANGE: "}}\n' +
'- {{subject}}\n' +
'{{/commit-list}}'
)
const expected =
'# Breaking Changes\n' +
'- Commit 2\n'
expect(compile({ commits })).to.equal(expected)
})

it('supports excludes option', () => {
const compile = Handlebars.compile(
'{{#commit-list commits heading="# Heading" exclude="^BREAKING CHANGE: "}}\n' +
'- {{subject}}\n' +
'{{/commit-list}}'
)
const expected =
'# Heading\n' +
'- Commit 1\n' +
'- feat: Commit 3\n'
expect(compile({ commits })).to.equal(expected)
})

it('returns nothing if nothing matches', () => {
const compile = Handlebars.compile(
'{{#commit-list commits heading="# Heading" message="A string that never appears"}}\n' +
'- {{subject}}\n' +
'{{/commit-list}}'
)
const expected = ''
expect(compile({ commits })).to.equal(expected)
})
})

0 comments on commit 77bb243

Please sign in to comment.