Skip to content

Commit

Permalink
Add author to fixes and merges
Browse files Browse the repository at this point in the history
Fixes #58
  • Loading branch information
cookpete committed Oct 8, 2018
1 parent 2b4aa45 commit 5b0c44c
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 79 deletions.
13 changes: 7 additions & 6 deletions src/commits.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ function parseCommit (commit, remote, options = {}) {
tag: getTag(refs, options),
subject: replaceText(getSubject(message), options),
message: message.trim(),
fixes: getFixes(message, remote, options),
merge: getMerge(message, remote, options),
fixes: getFixes(message, author, remote, options),
merge: getMerge(message, author, remote, options),
href: getCommitLink(hash, remote),
breaking: !!options.breakingPattern && new RegExp(options.breakingPattern).test(message),
...getStats(stats.trim())
Expand Down Expand Up @@ -105,15 +105,15 @@ function getStats (stats) {
}
}

function getFixes (message, remote, options = {}) {
function getFixes (message, author, remote, options = {}) {
const pattern = getFixPattern(options)
let fixes = []
let match = pattern.exec(message)
if (!match) return null
while (match) {
const id = getFixID(match)
const href = getIssueLink(match, id, remote, options.issueUrl)
fixes.push({ id, href })
fixes.push({ id, href, author })
match = pattern.exec(message)
}
return fixes
Expand All @@ -135,7 +135,7 @@ function getFixPattern (options) {
return DEFAULT_FIX_PATTERN
}

function getMerge (message, remote, options = {}) {
function getMerge (message, author, remote, options = {}) {
for (let pattern of MERGE_PATTERNS) {
const match = message.match(pattern)
if (match) {
Expand All @@ -144,7 +144,8 @@ function getMerge (message, remote, options = {}) {
return {
id,
message: replaceText(message, options),
href: getMergeLink(id, remote)
href: getMergeLink(id, remote),
author
}
}
}
Expand Down
97 changes: 52 additions & 45 deletions test/commits.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,73 +92,73 @@ describe('parseCommits', () => {
describe('getFixes', () => {
it('returns null with no fixes', () => {
const message = 'Commit message with no fixes'
expect(getFixes(message, remotes.github)).to.equal(null)
expect(getFixes(message, 'Commit Author', remotes.github)).to.equal(null)
})

it('parses a single fix', () => {
const message = 'Commit that fixes #12'
expect(getFixes(message, remotes.github)).to.deep.equal([
{ id: '12', href: 'https://github.com/user/repo/issues/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, remotes.github)).to.deep.equal([
{ id: '8', href: 'https://github.com/user/repo/issues/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, remotes.github)).to.deep.equal([
{ id: '14', href: '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 a commit that closes a bitbucket pull request', () => {
const message = 'Commit message\n\nCloses https://github.com/user/repo/pull-requests/14'
expect(getFixes(message, remotes.github)).to.deep.equal([
{ id: '14', href: 'https://github.com/user/repo/pull-requests/14' }
expect(getFixes(message, 'Commit Author', remotes.github)).to.deep.equal([
{ id: '14', href: 'https://github.com/user/repo/pull-requests/14', author: 'Commit Author' }
])
})

it('parses a commit that closes a gitlab pull request', () => {
const message = 'Commit message\n\nCloses https://github.com/user/repo/merge_requests/14'
expect(getFixes(message, remotes.github)).to.deep.equal([
{ id: '14', href: 'https://github.com/user/repo/merge_requests/14' }
expect(getFixes(message, 'Commit Author', remotes.github)).to.deep.equal([
{ id: '14', href: 'https://github.com/user/repo/merge_requests/14', author: 'Commit Author' }
])
})

it('parses multiple fixes', () => {
const message = 'Commit message\n\nFixes #1, fix #2, resolved #3, closes #4'
expect(getFixes(message, remotes.github)).to.deep.equal([
{ id: '1', href: 'https://github.com/user/repo/issues/1' },
{ id: '2', href: 'https://github.com/user/repo/issues/2' },
{ id: '3', href: 'https://github.com/user/repo/issues/3' },
{ id: '4', href: 'https://github.com/user/repo/issues/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, remotes.github)).to.deep.equal([
{ id: '1', href: '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, remotes.github)).to.deep.equal([
{ id: '1', href: 'https://github.com/user/repo/issues/1' },
{ id: '2', href: '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, remotes.github)).to.deep.equal([
{ id: '1', href: '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' }
])
})

Expand All @@ -167,8 +167,8 @@ describe('getFixes', () => {
issueUrl: 'http://example.com/issues/{id}'
}
const message = 'Commit message\n\nCloses #8'
expect(getFixes(message, remotes.github, options)).to.deep.equal([
{ id: '8', href: 'http://example.com/issues/8' }
expect(getFixes(message, 'Commit Author', remotes.github, options)).to.deep.equal([
{ id: '8', href: 'http://example.com/issues/8', author: 'Commit Author' }
])
})

Expand All @@ -178,8 +178,8 @@ describe('getFixes', () => {
issueUrl: 'http://example.com/issues/{id}'
}
const message = 'Commit message\n\nCloses ABC-1234'
expect(getFixes(message, remotes.github, options)).to.deep.equal([
{ id: 'ABC-1234', href: 'http://example.com/issues/ABC-1234' }
expect(getFixes(message, 'Commit Author', remotes.github, options)).to.deep.equal([
{ id: 'ABC-1234', href: 'http://example.com/issues/ABC-1234', author: 'Commit Author' }
])
})

Expand All @@ -189,55 +189,59 @@ describe('getFixes', () => {
issueUrl: 'http://example.com/issues/{id}'
}
const message = 'Commit message\n\nFixes ABC-1234 and fixes ABC-2345 but not BCD-2345'
expect(getFixes(message, remotes.github, options)).to.deep.equal([
{ id: 'ABC-1234', href: 'http://example.com/issues/ABC-1234' },
{ id: 'ABC-2345', href: 'http://example.com/issues/ABC-2345' }
expect(getFixes(message, 'Commit Author', remotes.github, options)).to.deep.equal([
{ id: 'ABC-1234', href: 'http://example.com/issues/ABC-1234', author: 'Commit Author' },
{ id: 'ABC-2345', href: 'http://example.com/issues/ABC-2345', author: 'Commit Author' }
])
})
})

describe('getMerge', () => {
it('returns null on fail', () => {
const message = 'Not a merge commit'
expect(getMerge(message, remotes.github)).to.equal(null)
expect(getMerge(message, 'Commit Author', 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(message, remotes.github)).to.deep.equal({
expect(getMerge(message, 'Commit Author', remotes.github)).to.deep.equal({
id: '3',
message: 'Pull request title',
href: 'https://github.com/user/repo/pull/3'
href: 'https://github.com/user/repo/pull/3',
author: 'Commit Author'
})
})

it('parses a squash merge', () => {
const message = 'Update dependencies to enable Greenkeeper 🌴 (#10)\n\n* chore(package): update dependencies'
expect(getMerge(message, remotes.github)).to.deep.equal({
expect(getMerge(message, 'Commit Author', remotes.github)).to.deep.equal({
id: '10',
message: 'Update dependencies to enable Greenkeeper 🌴',
href: 'https://github.com/user/repo/pull/10'
href: 'https://github.com/user/repo/pull/10',
author: 'Commit Author'
})
})

it('parses a squash merge with no message', () => {
const message = 'Generate changelogs that show the commits between tags (#411)'
expect(getMerge(message, remotes.github)).to.deep.equal({
expect(getMerge(message, 'Commit Author', remotes.github)).to.deep.equal({
id: '411',
message: 'Generate changelogs that show the commits between tags',
href: 'https://github.com/user/repo/pull/411'
href: 'https://github.com/user/repo/pull/411',
author: 'Commit Author'
})
})
})

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(message, remotes.gitlab)).to.deep.equal({
expect(getMerge(message, 'Commit Author', 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'
href: 'https://gitlab.com/user/repo/merge_requests/15007',
author: 'Commit Author'
})
})

Expand All @@ -247,21 +251,23 @@ describe('getMerge', () => {
hostname: 'gitlab.com',
url: 'https://gitlab.com/user/repo/subgroup'
}
expect(getMerge(message, remote)).to.deep.equal({
expect(getMerge(message, 'Commit Author', remote)).to.deep.equal({
id: '15007',
message: 'Memoize GitLab logger to reduce open file descriptors',
href: 'https://gitlab.com/user/repo/subgroup/merge_requests/15007'
href: 'https://gitlab.com/user/repo/subgroup/merge_requests/15007',
author: 'Commit Author'
})
})
})

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 <[email protected]>'
expect(getMerge(message, remotes.bitbucket)).to.deep.equal({
expect(getMerge(message, 'Commit Author', 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'
href: 'https://bitbucket.org/user/repo/pull-requests/4518',
author: 'Commit Author'
})
})
})
Expand All @@ -273,10 +279,11 @@ describe('getMerge', () => {
'(..l)': '_$1_'
}
}
expect(getMerge(message, remotes.github, options)).to.deep.equal({
expect(getMerge(message, 'Commit Author', remotes.github, options)).to.deep.equal({
id: '3',
message: '_Pul_l request t_itl_e',
href: 'https://github.com/user/repo/pull/3'
href: 'https://github.com/user/repo/pull/3',
author: 'Commit Author'
})
})
})
Expand Down
18 changes: 12 additions & 6 deletions test/data/commits-no-remote.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ export default [
"fixes": [
{
"id": "6",
"href": null
"href": null,
"author": "Pete Cook"
}
],
"merge": null,
Expand Down Expand Up @@ -136,7 +137,8 @@ export default [
"merge": {
"id": "5",
"message": "Should not parse #4 in PR title",
"href": null
"href": null,
"author": "Pete Cook"
},
"href": null,
"breaking": false
Expand Down Expand Up @@ -170,7 +172,8 @@ export default [
"fixes": [
{
"id": "4",
"href": null
"href": null,
"author": "Pete Cook"
}
],
"merge": null,
Expand Down Expand Up @@ -210,7 +213,8 @@ export default [
"merge": {
"id": "3",
"message": "Third commit with same name as PR",
"href": null
"href": null,
"author": "Pete Cook"
},
"href": null,
"breaking": false
Expand Down Expand Up @@ -244,11 +248,13 @@ export default [
"fixes": [
{
"id": "1",
"href": null
"href": null,
"author": "Pete Cook"
},
{
"id": "2",
"href": null
"href": null,
"author": "Pete Cook"
}
],
"merge": null,
Expand Down
18 changes: 12 additions & 6 deletions test/data/commits.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ export default [
"fixes": [
{
"id": "6",
"href": "https://github.com/user/repo/issues/6"
"href": "https://github.com/user/repo/issues/6",
"author": "Pete Cook"
}
],
"merge": null,
Expand Down Expand Up @@ -136,7 +137,8 @@ export default [
"merge": {
"id": "5",
"message": "Should not parse #4 in PR title",
"href": "https://github.com/user/repo/pull/5"
"href": "https://github.com/user/repo/pull/5",
"author": "Pete Cook"
},
"href": "https://github.com/user/repo/commit/0e24bf427a51eac52133cc731b4b5d74a7e04672",
"breaking": false
Expand Down Expand Up @@ -170,7 +172,8 @@ export default [
"fixes": [
{
"id": "4",
"href": "https://github.com/user/repo/issues/4"
"href": "https://github.com/user/repo/issues/4",
"author": "Pete Cook"
}
],
"merge": null,
Expand Down Expand Up @@ -210,7 +213,8 @@ export default [
"merge": {
"id": "3",
"message": "Third commit with same name as PR",
"href": "https://github.com/user/repo/pull/3"
"href": "https://github.com/user/repo/pull/3",
"author": "Pete Cook"
},
"href": "https://github.com/user/repo/commit/31b7d3da24d64e32a0a7e558f254d01c348613f3",
"breaking": false
Expand Down Expand Up @@ -244,11 +248,13 @@ export default [
"fixes": [
{
"id": "1",
"href": "https://github.com/user/repo/issues/1"
"href": "https://github.com/user/repo/issues/1",
"author": "Pete Cook"
},
{
"id": "2",
"href": "https://github.com/user/repo/issues/2"
"href": "https://github.com/user/repo/issues/2",
"author": "Pete Cook"
}
],
"merge": null,
Expand Down
Loading

0 comments on commit 5b0c44c

Please sign in to comment.