Skip to content

Commit

Permalink
Add replaceText package option
Browse files Browse the repository at this point in the history
  • Loading branch information
cookpete committed May 23, 2018
1 parent 1eea44d commit 77d73ee
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 13 deletions.
18 changes: 17 additions & 1 deletion README.md
Expand Up @@ -222,7 +222,7 @@ Or, in your `package.json`:
{
"name": "my-awesome-package",
"auto-changelog": {
"issueUrl": "https://issues.apache.org/jira/browse/",
"issueUrl": "https://issues.apache.org/jira/browse/{id}",
"issuePattern": "[A-Z]+-\d+"
}
}
Expand All @@ -235,6 +235,22 @@ If you use a certain pattern before or after the issue number, like `fixes {id}`
auto-changelog --issue-pattern "[Ff]ixes ([A-Z]+-\d+)"
```

#### Replacing text

To insert links or other markup to PR titles and commit messages that appear in the log, use the `replaceText` option in your `package.json`:

```js
{
"name": "my-awesome-package",
"auto-changelog": {
"replaceText": {
"(ABC-\\d+)": "[`$1`](https://issues.apache.org/jira/browse/$1)"
}
}
}
```

Here, any time a pattern like `ABC-123` appears in your log, it will be replaced with a link to the relevant issue in Jira. Each pattern is applied using `string.replace(new RegExp(key, 'g'), value)`.

### Migrating to `1.x`

Expand Down
12 changes: 6 additions & 6 deletions src/commits.js
@@ -1,6 +1,6 @@
import semver from 'semver'

import { cmd, isLink } from './utils'
import { cmd, isLink, replaceText } from './utils'

const COMMIT_SEPARATOR = '__AUTO_CHANGELOG_COMMIT_SEPARATOR__'
const MESSAGE_SEPARATOR = '__AUTO_CHANGELOG_MESSAGE_SEPARATOR__'
Expand Down Expand Up @@ -57,10 +57,10 @@ function parseCommit (commit, remote, options = {}) {
email,
date: new Date(date).toISOString(),
tag: getTag(refs, options),
subject: getSubject(message),
subject: replaceText(getSubject(message), options),
message: message.trim(),
fixes: getFixes(message, remote, options),
merge: getMerge(message, remote),
merge: getMerge(message, remote, options),
href: getCommitLink(hash, remote),
breaking: !!options.breakingPattern && new RegExp(options.breakingPattern).test(message),
...getStats(stats.trim())
Expand Down Expand Up @@ -128,16 +128,16 @@ function getFixPattern (options) {
return DEFAULT_FIX_PATTERN
}

function getMerge (message, remote, mergeUrl) {
function getMerge (message, remote, options = {}) {
for (let pattern of MERGE_PATTERNS) {
const match = message.match(pattern)
if (match) {
const id = /^\d+$/.test(match[1]) ? match[1] : match[2]
const message = /^\d+$/.test(match[1]) ? match[2] : match[1]
return {
id,
message,
href: getMergeLink(id, remote, mergeUrl)
message: replaceText(message, options),
href: getMergeLink(id, remote)
}
}
}
Expand Down
9 changes: 9 additions & 0 deletions src/utils.js
Expand Up @@ -36,3 +36,12 @@ export function isLink (string) {
export function parseLimit (limit) {
return limit === 'false' ? false : parseInt(limit, 10)
}

export function replaceText (string, options) {
if (!options.replaceText) {
return string
}
return Object.keys(options.replaceText).reduce((string, pattern) => {
return string.replace(new RegExp(pattern, 'g'), options.replaceText[pattern])
}, string)
}
6 changes: 3 additions & 3 deletions templates/compact.hbs
Expand Up @@ -17,13 +17,13 @@ Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog).
> {{niceDate}}
{{/if}}
{{#each merges}}
- {{message}}{{#if href}} [`#{{id}}`]({{href}}){{/if}}
- {{{message}}}{{#if href}} [`#{{id}}`]({{href}}){{/if}}
{{/each}}
{{#each fixes}}
- {{commit.subject}}{{#each fixes}}{{#if href}} [`#{{id}}`]({{href}}){{/if}}{{/each}}
- {{{commit.subject}}}{{#each fixes}}{{#if href}} [`#{{id}}`]({{href}}){{/if}}{{/each}}
{{/each}}
{{#each commits}}
- {{#if breaking}}**Breaking change:** {{/if}}{{subject}}{{#if href}} [`{{shorthash}}`]({{href}}){{/if}}
- {{#if breaking}}**Breaking change:** {{/if}}{{{subject}}}{{#if href}} [`{{shorthash}}`]({{href}}){{/if}}
{{/each}}

{{/each}}
6 changes: 3 additions & 3 deletions templates/keepachangelog.hbs
Expand Up @@ -15,19 +15,19 @@ Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog).
{{#if merges}}
### Merged
{{#each merges}}
- {{message}} {{#if href}}[`#{{id}}`]({{href}}){{/if}}
- {{{message}}} {{#if href}}[`#{{id}}`]({{href}}){{/if}}
{{/each}}

{{/if}}
{{#if fixes}}
### Fixed
{{#each fixes}}
- {{commit.subject}}{{#each fixes}} {{#if href}}[`#{{id}}`]({{href}}){{/if}}{{/each}}
- {{{commit.subject}}}{{#each fixes}} {{#if href}}[`#{{id}}`]({{href}}){{/if}}{{/each}}
{{/each}}

{{/if}}
{{#commit-list commits heading='### Commits'}}
- {{#if breaking}}**Breaking change:** {{/if}}{{subject}} {{#if href}}[`{{shorthash}}`]({{href}}){{/if}}
- {{#if breaking}}**Breaking change:** {{/if}}{{{subject}}} {{#if href}}[`{{shorthash}}`]({{href}}){{/if}}
{{/commit-list}}

{{/each}}
25 changes: 25 additions & 0 deletions test/commits.js
Expand Up @@ -69,6 +69,17 @@ describe('parseCommits', () => {
expect(result.filter(c => c.breaking)).to.have.length(1)
})

it('supports replaceText option', async () => {
const gitLog = await readFile(join(__dirname, 'data', 'git-log.txt'), 'utf-8')
const options = {
replaceText: {
'breaking': '**BREAKING**'
}
}
const result = parseCommits(gitLog, remotes.github, options)
expect(result.filter(c => c.subject === 'Some **BREAKING** change')).to.have.length(1)
})

it('invalid startingCommit throws an error', done => {
const options = { startingCommit: 'not-a-hash' }
readFile(join(__dirname, 'data', 'git-log.txt'), 'utf-8')
Expand Down Expand Up @@ -254,6 +265,20 @@ describe('getMerge', () => {
})
})
})

it('supports replaceText option', () => {
const message = 'Merge pull request #3 from repo/branch\n\nPull request title'
const options = {
replaceText: {
'(..l)': '_$1_'
}
}
expect(getMerge(message, remotes.github, options)).to.deep.equal({
id: '3',
message: '_Pul_l request t_itl_e',
href: 'https://github.com/user/repo/pull/3'
})
})
})

describe('getSubject', () => {
Expand Down

0 comments on commit 77d73ee

Please sign in to comment.