forked from cookpete/auto-changelog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommits.js
341 lines (304 loc) · 12.9 KB
/
commits.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
const { describe, it } = require('mocha')
const { expect } = require('chai')
const { join } = require('path')
const { readFile } = require('../src/utils')
const remotes = require('./data/remotes')
const commits = require('./data/commits')
const commitsNoRemote = require('./data/commits-no-remote')
const {
fetchCommits,
__get__,
__Rewire__: mock,
__ResetDependency__: unmock
} = require('../src/commits')
const parseCommits = __get__('parseCommits')
const getFixes = __get__('getFixes')
const getMerge = __get__('getMerge')
const getSubject = __get__('getSubject')
const getLogFormat = __get__('getLogFormat')
describe('fetchCommits', () => {
it('fetches commits', async () => {
const gitLog = await readFile(join(__dirname, 'data', 'git-log.txt'))
mock('cmd', () => gitLog)
expect(await fetchCommits('', remotes.github)).to.deep.equal(commits)
unmock('cmd')
})
})
describe('parseCommits', () => {
it('parses commits', async () => {
const gitLog = await readFile(join(__dirname, 'data', 'git-log.txt'))
expect(parseCommits(gitLog, remotes.github)).to.deep.equal(commits)
})
it('parses commits without remote', async () => {
const gitLog = await readFile(join(__dirname, 'data', 'git-log.txt'))
expect(parseCommits(gitLog, remotes.null)).to.deep.equal(commitsNoRemote)
})
it('parses bitbucket commits', async () => {
const gitLog = await readFile(join(__dirname, 'data', 'git-log.txt'))
const commits = parseCommits(gitLog, remotes.bitbucket)
expect(commits[0].href).to.equal('https://bitbucket.org/user/repo/commits/2401ee4706e94629f48830bab9ed5812c032734a')
})
it('supports breakingPattern option', async () => {
const gitLog = await readFile(join(__dirname, 'data', 'git-log.txt'))
const options = {
breakingPattern: 'Some breaking change',
...remotes.github
}
const result = parseCommits(gitLog, options)
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'))
const options = {
replaceText: {
breaking: '**BREAKING**'
},
...remotes.github
}
const result = parseCommits(gitLog, options)
expect(result.filter(c => c.subject === 'Some **BREAKING** change')).to.have.length(1)
})
})
describe('getFixes', () => {
it('returns null with no fixes', () => {
const message = 'Commit message with no fixes'
expect(getFixes(message, 'Commit Author', remotes.github)).to.equal(null)
})
it('parses a single fix', () => {
const message = 'Commit that fixes #12'
expect(getFixes(message, 'Commit Author', remotes.github)).to.deep.equal([
{ id: '12', href: 'https://github.com/user/repo/issues/12', author: 'Commit Author' }
])
})
it('parses fix in commit notes', () => {
const message = 'Commit message\n\nCloses #8'
expect(getFixes(message, 'Commit Author', remotes.github)).to.deep.equal([
{ id: '8', href: 'https://github.com/user/repo/issues/8', author: 'Commit Author' }
])
})
it('parses a commit that closes a pull request', () => {
const message = 'Commit message\n\nCloses https://github.com/user/repo/pull/14'
expect(getFixes(message, 'Commit Author', remotes.github)).to.deep.equal([
{ id: '14', href: 'https://github.com/user/repo/pull/14', author: 'Commit Author' }
])
})
it('parses multiple fixes', () => {
const message = 'Commit message\n\nFixes #1, fix #2, resolved #3, closes #4'
expect(getFixes(message, 'Commit Author', remotes.github)).to.deep.equal([
{ id: '1', href: 'https://github.com/user/repo/issues/1', author: 'Commit Author' },
{ id: '2', href: 'https://github.com/user/repo/issues/2', author: 'Commit Author' },
{ id: '3', href: 'https://github.com/user/repo/issues/3', author: 'Commit Author' },
{ id: '4', href: 'https://github.com/user/repo/issues/4', author: 'Commit Author' }
])
})
it('parses fixes by issue URL', () => {
const message = 'Commit message\n\nFixes https://github.com/user/repo/issues/1'
expect(getFixes(message, 'Commit Author', remotes.github)).to.deep.equal([
{ id: '1', href: 'https://github.com/user/repo/issues/1', author: 'Commit Author' }
])
})
it('parses multiple fixes by issue URL', () => {
const message = 'Commit message\n\nFixes https://github.com/user/repo/issues/1 and fixes https://github.com/user/repo/issues/2'
expect(getFixes(message, 'Commit Author', remotes.github)).to.deep.equal([
{ id: '1', href: 'https://github.com/user/repo/issues/1', author: 'Commit Author' },
{ id: '2', href: 'https://github.com/user/repo/issues/2', author: 'Commit Author' }
])
})
it('parses external repo issues', () => {
const message = 'Commit message\n\nFixes https://github.com/other-user/external-repo/issues/1'
expect(getFixes(message, 'Commit Author', remotes.github)).to.deep.equal([
{ id: '1', href: 'https://github.com/other-user/external-repo/issues/1', author: 'Commit Author' }
])
})
it('parses azure devops fix', () => {
const message = 'Commit message\n\nCloses #123'
expect(getFixes(message, 'Commit Author', remotes.azure)).to.deep.equal([
{ id: '123', href: 'https://dev.azure.com/user/project/_workitems/edit/123', author: 'Commit Author' }
])
})
it('parses visual studio fix', () => {
const message = 'Commit message\n\nCloses #123'
expect(getFixes(message, 'Commit Author', remotes.visualstudio)).to.deep.equal([
{ id: '123', href: 'https://user.visualstudio.com/project/_workitems/edit/123', author: 'Commit Author' }
])
})
it('supports issuePattern parameter', () => {
const options = {
issuePattern: '[A-Z]+-\\d+',
...remotes.github
}
const message = 'Commit message\n\nCloses ABC-1234'
expect(getFixes(message, 'Commit Author', options)).to.deep.equal([
{ id: 'ABC-1234', href: 'https://github.com/user/repo/issues/ABC-1234', author: 'Commit Author' }
])
})
it('supports issuePattern parameter with capture group', () => {
const options = {
issuePattern: '[Ff]ixes ([A-Z]+-\\d+)',
...remotes.github
}
const message = 'Commit message\n\nFixes ABC-1234 and fixes ABC-2345 but not BCD-2345'
expect(getFixes(message, 'Commit Author', options)).to.deep.equal([
{ id: 'ABC-1234', href: 'https://github.com/user/repo/issues/ABC-1234', author: 'Commit Author' },
{ id: 'ABC-2345', href: 'https://github.com/user/repo/issues/ABC-2345', author: 'Commit Author' }
])
})
})
describe('getMerge', () => {
const EXAMPLE_COMMIT = {
author: 'Commit Author',
id: 123
}
it('returns null on fail', () => {
const message = 'Not a merge commit'
expect(getMerge(EXAMPLE_COMMIT, message, remotes.github)).to.equal(null)
})
describe('github', () => {
it('parses a merge', () => {
const message = 'Merge pull request #3 from repo/branch\n\nPull request title'
expect(getMerge(EXAMPLE_COMMIT, message, remotes.github)).to.deep.equal({
id: '3',
message: 'Pull request title',
href: 'https://github.com/user/repo/pull/3',
author: 'Commit Author',
commit: EXAMPLE_COMMIT
})
})
it('parses a squash merge', () => {
const message = 'Update dependencies to enable Greenkeeper 🌴 (#10)\n\n* chore(package): update dependencies'
expect(getMerge(EXAMPLE_COMMIT, message, remotes.github)).to.deep.equal({
id: '10',
message: 'Update dependencies to enable Greenkeeper 🌴',
href: 'https://github.com/user/repo/pull/10',
author: 'Commit Author',
commit: EXAMPLE_COMMIT
})
})
it('parses a squash merge with no message', () => {
const message = 'Generate changelogs that show the commits between tags (#411)'
expect(getMerge(EXAMPLE_COMMIT, message, remotes.github)).to.deep.equal({
id: '411',
message: 'Generate changelogs that show the commits between tags',
href: 'https://github.com/user/repo/pull/411',
author: 'Commit Author',
commit: EXAMPLE_COMMIT
})
})
})
describe('gitlab', () => {
it('parses a merge', () => {
const message = 'Merge branch \'branch\' into \'master\'\n\nMemoize GitLab logger to reduce open file descriptors\n\nCloses gitlab-ee#3664\n\nSee merge request !15007'
expect(getMerge(EXAMPLE_COMMIT, message, remotes.gitlab)).to.deep.equal({
id: '15007',
message: 'Memoize GitLab logger to reduce open file descriptors',
href: 'https://gitlab.com/user/repo/merge_requests/15007',
author: 'Commit Author',
commit: EXAMPLE_COMMIT
})
})
it('parses a merge for subgroups', () => {
const message = 'Merge branch \'branch\' into \'master\'\n\nMemoize GitLab logger to reduce open file descriptors\n\nCloses gitlab-ee#3664\n\nSee merge request user/repo/subgroup!15007'
expect(getMerge(EXAMPLE_COMMIT, message, remotes.gitlabSubgroup)).to.deep.equal({
id: '15007',
message: 'Memoize GitLab logger to reduce open file descriptors',
href: 'https://gitlab.com/user/repo/subgroup/merge_requests/15007',
author: 'Commit Author',
commit: EXAMPLE_COMMIT
})
})
})
describe('bitbucket', () => {
it('parses a merge', () => {
const message = 'Merged in eshvedai/fix-schema-issue (pull request #4518)\n\nfix(component): re-export createSchema from editor-core\n\nApproved-by: Scott Sidwell <ssidwell@atlassian.com>'
expect(getMerge(EXAMPLE_COMMIT, message, remotes.bitbucket)).to.deep.equal({
id: '4518',
message: 'fix(component): re-export createSchema from editor-core',
href: 'https://bitbucket.org/user/repo/pull-requests/4518',
author: 'Commit Author',
commit: EXAMPLE_COMMIT
})
})
})
describe('azure devops', () => {
it('parses a merge', () => {
// Use github merge message until we can find out what an azure devops one looks like
const message = 'Merge pull request #3 from repo/branch\n\nPull request title'
expect(getMerge(EXAMPLE_COMMIT, message, remotes.azure)).to.deep.equal({
id: '3',
message: 'Pull request title',
href: 'https://dev.azure.com/user/project/_git/repo/pullrequest/3',
author: 'Commit Author',
commit: EXAMPLE_COMMIT
})
})
})
describe('visual studio', () => {
it('parses a merge', () => {
// Use github merge message until we can find out what a visual studio one looks like
const message = 'Merge pull request #3 from repo/branch\n\nPull request title'
expect(getMerge(EXAMPLE_COMMIT, message, remotes.visualstudio)).to.deep.equal({
id: '3',
message: 'Pull request title',
href: 'https://user.visualstudio.com/project/_git/repo/pullrequest/3',
author: 'Commit Author',
commit: EXAMPLE_COMMIT
})
})
})
it('supports mergePattern parameter', () => {
const options = {
mergePattern: 'PR #(\\d+) from .+\\n\\n.+\\n(.+)',
...remotes.github
}
const message = 'PR #37 from repo/branch\n\ncommit sha512\nPull request title'
expect(getMerge(EXAMPLE_COMMIT, message, options)).to.deep.equal({
id: '37',
message: 'Pull request title',
href: 'https://github.com/user/repo/pull/37',
author: 'Commit Author',
commit: EXAMPLE_COMMIT
})
})
it('supports replaceText option', () => {
const message = 'Merge pull request #3 from repo/branch\n\nPull request title'
const options = {
replaceText: {
'(..l)': '_$1_'
},
...remotes.github
}
expect(getMerge(EXAMPLE_COMMIT, message, options)).to.deep.equal({
id: '3',
message: '_Pul_l request t_itl_e',
href: 'https://github.com/user/repo/pull/3',
author: 'Commit Author',
commit: EXAMPLE_COMMIT
})
})
})
describe('getSubject', () => {
it('returns commit subject', () => {
const message = 'Commit message\n\nCloses ABC-1234'
expect(getSubject(message)).to.equal('Commit message')
})
it('returns no commit message', () => {
expect(getSubject('')).to.equal('_No commit message_')
})
})
describe('getLogFormat', () => {
it('returns modern format', async () => {
mock('getGitVersion', () => Promise.resolve('1.7.2'))
expect(await getLogFormat()).to.equal('__AUTO_CHANGELOG_COMMIT_SEPARATOR__%H%n%ai%n%an%n%ae%n%B__AUTO_CHANGELOG_MESSAGE_SEPARATOR__')
unmock('getGitVersion')
})
it('returns fallback format', async () => {
mock('getGitVersion', () => Promise.resolve('1.7.1'))
expect(await getLogFormat()).to.equal('__AUTO_CHANGELOG_COMMIT_SEPARATOR__%H%n%ai%n%an%n%ae%n%s%n%n%b__AUTO_CHANGELOG_MESSAGE_SEPARATOR__')
unmock('getGitVersion')
})
it('returns fallback format when null', async () => {
mock('getGitVersion', () => Promise.resolve(null))
expect(await getLogFormat()).to.equal('__AUTO_CHANGELOG_COMMIT_SEPARATOR__%H%n%ai%n%an%n%ae%n%s%n%n%b__AUTO_CHANGELOG_MESSAGE_SEPARATOR__')
unmock('getGitVersion')
})
})