Skip to content

Commit 77d73ee

Browse files
committed
Add replaceText package option
1 parent 1eea44d commit 77d73ee

File tree

6 files changed

+63
-13
lines changed

6 files changed

+63
-13
lines changed

README.md

+17-1
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ Or, in your `package.json`:
222222
{
223223
"name": "my-awesome-package",
224224
"auto-changelog": {
225-
"issueUrl": "https://issues.apache.org/jira/browse/",
225+
"issueUrl": "https://issues.apache.org/jira/browse/{id}",
226226
"issuePattern": "[A-Z]+-\d+"
227227
}
228228
}
@@ -235,6 +235,22 @@ If you use a certain pattern before or after the issue number, like `fixes {id}`
235235
auto-changelog --issue-pattern "[Ff]ixes ([A-Z]+-\d+)"
236236
```
237237

238+
#### Replacing text
239+
240+
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`:
241+
242+
```js
243+
{
244+
"name": "my-awesome-package",
245+
"auto-changelog": {
246+
"replaceText": {
247+
"(ABC-\\d+)": "[`$1`](https://issues.apache.org/jira/browse/$1)"
248+
}
249+
}
250+
}
251+
```
252+
253+
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)`.
238254

239255
### Migrating to `1.x`
240256

src/commits.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import semver from 'semver'
22

3-
import { cmd, isLink } from './utils'
3+
import { cmd, isLink, replaceText } from './utils'
44

55
const COMMIT_SEPARATOR = '__AUTO_CHANGELOG_COMMIT_SEPARATOR__'
66
const MESSAGE_SEPARATOR = '__AUTO_CHANGELOG_MESSAGE_SEPARATOR__'
@@ -57,10 +57,10 @@ function parseCommit (commit, remote, options = {}) {
5757
email,
5858
date: new Date(date).toISOString(),
5959
tag: getTag(refs, options),
60-
subject: getSubject(message),
60+
subject: replaceText(getSubject(message), options),
6161
message: message.trim(),
6262
fixes: getFixes(message, remote, options),
63-
merge: getMerge(message, remote),
63+
merge: getMerge(message, remote, options),
6464
href: getCommitLink(hash, remote),
6565
breaking: !!options.breakingPattern && new RegExp(options.breakingPattern).test(message),
6666
...getStats(stats.trim())
@@ -128,16 +128,16 @@ function getFixPattern (options) {
128128
return DEFAULT_FIX_PATTERN
129129
}
130130

131-
function getMerge (message, remote, mergeUrl) {
131+
function getMerge (message, remote, options = {}) {
132132
for (let pattern of MERGE_PATTERNS) {
133133
const match = message.match(pattern)
134134
if (match) {
135135
const id = /^\d+$/.test(match[1]) ? match[1] : match[2]
136136
const message = /^\d+$/.test(match[1]) ? match[2] : match[1]
137137
return {
138138
id,
139-
message,
140-
href: getMergeLink(id, remote, mergeUrl)
139+
message: replaceText(message, options),
140+
href: getMergeLink(id, remote)
141141
}
142142
}
143143
}

src/utils.js

+9
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,12 @@ export function isLink (string) {
3636
export function parseLimit (limit) {
3737
return limit === 'false' ? false : parseInt(limit, 10)
3838
}
39+
40+
export function replaceText (string, options) {
41+
if (!options.replaceText) {
42+
return string
43+
}
44+
return Object.keys(options.replaceText).reduce((string, pattern) => {
45+
return string.replace(new RegExp(pattern, 'g'), options.replaceText[pattern])
46+
}, string)
47+
}

templates/compact.hbs

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog).
1717
> {{niceDate}}
1818
{{/if}}
1919
{{#each merges}}
20-
- {{message}}{{#if href}} [`#{{id}}`]({{href}}){{/if}}
20+
- {{{message}}}{{#if href}} [`#{{id}}`]({{href}}){{/if}}
2121
{{/each}}
2222
{{#each fixes}}
23-
- {{commit.subject}}{{#each fixes}}{{#if href}} [`#{{id}}`]({{href}}){{/if}}{{/each}}
23+
- {{{commit.subject}}}{{#each fixes}}{{#if href}} [`#{{id}}`]({{href}}){{/if}}{{/each}}
2424
{{/each}}
2525
{{#each commits}}
26-
- {{#if breaking}}**Breaking change:** {{/if}}{{subject}}{{#if href}} [`{{shorthash}}`]({{href}}){{/if}}
26+
- {{#if breaking}}**Breaking change:** {{/if}}{{{subject}}}{{#if href}} [`{{shorthash}}`]({{href}}){{/if}}
2727
{{/each}}
2828

2929
{{/each}}

templates/keepachangelog.hbs

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,19 @@ Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog).
1515
{{#if merges}}
1616
### Merged
1717
{{#each merges}}
18-
- {{message}} {{#if href}}[`#{{id}}`]({{href}}){{/if}}
18+
- {{{message}}} {{#if href}}[`#{{id}}`]({{href}}){{/if}}
1919
{{/each}}
2020

2121
{{/if}}
2222
{{#if fixes}}
2323
### Fixed
2424
{{#each fixes}}
25-
- {{commit.subject}}{{#each fixes}} {{#if href}}[`#{{id}}`]({{href}}){{/if}}{{/each}}
25+
- {{{commit.subject}}}{{#each fixes}} {{#if href}}[`#{{id}}`]({{href}}){{/if}}{{/each}}
2626
{{/each}}
2727

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

3333
{{/each}}

test/commits.js

+25
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,17 @@ describe('parseCommits', () => {
6969
expect(result.filter(c => c.breaking)).to.have.length(1)
7070
})
7171

72+
it('supports replaceText option', async () => {
73+
const gitLog = await readFile(join(__dirname, 'data', 'git-log.txt'), 'utf-8')
74+
const options = {
75+
replaceText: {
76+
'breaking': '**BREAKING**'
77+
}
78+
}
79+
const result = parseCommits(gitLog, remotes.github, options)
80+
expect(result.filter(c => c.subject === 'Some **BREAKING** change')).to.have.length(1)
81+
})
82+
7283
it('invalid startingCommit throws an error', done => {
7384
const options = { startingCommit: 'not-a-hash' }
7485
readFile(join(__dirname, 'data', 'git-log.txt'), 'utf-8')
@@ -254,6 +265,20 @@ describe('getMerge', () => {
254265
})
255266
})
256267
})
268+
269+
it('supports replaceText option', () => {
270+
const message = 'Merge pull request #3 from repo/branch\n\nPull request title'
271+
const options = {
272+
replaceText: {
273+
'(..l)': '_$1_'
274+
}
275+
}
276+
expect(getMerge(message, remotes.github, options)).to.deep.equal({
277+
id: '3',
278+
message: '_Pul_l request t_itl_e',
279+
href: 'https://github.com/user/repo/pull/3'
280+
})
281+
})
257282
})
258283

259284
describe('getSubject', () => {

0 commit comments

Comments
 (0)