Skip to content
This repository was archived by the owner on Apr 1, 2020. It is now read-only.

Commit 6d33c5d

Browse files
committed
feat: write an issue comment with the details
1 parent 2ed6247 commit 6d33c5d

File tree

5 files changed

+141
-31
lines changed

5 files changed

+141
-31
lines changed

Diff for: lib/format.js

+27-14
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,37 @@
11
/**
22
* Formats commitlint report as GitHub status report
33
*
4-
* @param {*} report
4+
* @param {Object} report
55
*/
66
const format = report => {
7-
const { errors, warnings } = report
7+
const { commits } = report
88

9-
// Summary
10-
let message = `found ${errors.length} problems, ${warnings.length} warnings`
9+
// Keep errors/warnings count
10+
let errorsCount = 0
11+
let warnsCount = 0
12+
13+
// Details message
14+
let message = ''
1115

12-
// TODO: write PR comment instead of multi-line description
13-
// // Errors
14-
// if (errors.length > 0) {
15-
// message += '\n' + errors.map(e => '✖ ' + e.message).join('\n')
16-
// }
17-
// // Warnings
18-
// if (warnings.length > 0) {
19-
// message += '\n' + warnings.map(e => '⚠ ' + e.message).join('\n')
20-
// }
16+
for (const commit in commits) {
17+
message += `* Commit: ${commit}\n`
18+
const { errors, warnings } = commits[commit]
19+
for (const e of errors) {
20+
message += ` - ✖ ${e.message}\n`
21+
}
22+
for (const w of warnings) {
23+
message += ` - ⚠ ${w.message}\n`
24+
}
25+
errorsCount += errors.length
26+
warnsCount += warnings.length
27+
}
2128

22-
return message
29+
// Summary
30+
const summary = `found ${errorsCount} problems, ${warnsCount} warnings`
31+
if (errorsCount > 0 || warnsCount > 0) {
32+
message = `There were the following issues with this Pull Request\n${message}`
33+
}
34+
return { summary, message }
2335
}
36+
2437
module.exports = format

Diff for: lib/lint.js

+23-10
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const commitlint = async ({ github, payload }) => {
2222
}
2323

2424
// Pending
25-
github.repos.createStatus({
25+
await github.repos.createStatus({
2626
...statusInfo,
2727
state: 'pending',
2828
description: 'Waiting for the status to be reported'
@@ -33,25 +33,38 @@ const commitlint = async ({ github, payload }) => {
3333
github.pullRequests.getCommits({ repo: name, owner: owner.login, number }),
3434
async commits => {
3535
// empty summary
36-
const report = { valid: true, errors: [], warnings: [] }
36+
const report = { valid: true, commits: {} }
3737
const { rules } = await load(config)
3838

39-
// Iterate over all commits
39+
// Iterates over all commits
4040
for (const d of commits.data) {
41-
const rep = await lint(d.commit.message, rules)
42-
if (!rep.valid) {
41+
const { valid, errors, warnings } = await lint(d.commit.message, rules)
42+
if (!valid) {
4343
report.valid = false
4444
}
45-
report.errors = report.errors.concat(rep.errors)
46-
report.warnings = report.errors.concat(rep.warnings)
45+
46+
if (errors.length > 0 || warnings.length > 0) {
47+
report.commits[d.sha] = { errors, warnings }
48+
}
4749
}
4850

49-
// Final report
50-
github.repos.createStatus({
51+
const { summary, message } = format(report)
52+
53+
// Final status
54+
await github.repos.createStatus({
5155
...statusInfo,
5256
state: report.valid ? 'success' : 'error',
53-
description: format(report)
57+
description: summary
5458
})
59+
// Write a comment with the details (if any)
60+
if (message) {
61+
await github.issues.createComment({
62+
repo: name,
63+
owner: owner.login,
64+
number,
65+
body: message
66+
})
67+
}
5568
}
5669
)
5770
}

Diff for: test/bot.test.js

+38-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ describe('commitlint-bot', () => {
3030
robot.auth = () => Promise.resolve(github)
3131
})
3232

33-
describe('updates status to pending', () => {
33+
describe('status update to pending', () => {
3434
const pending = {
3535
...baseStatus,
3636
state: 'pending',
@@ -48,7 +48,7 @@ describe('commitlint-bot', () => {
4848
})
4949
})
5050

51-
describe('gets the list of commits for PRs', () => {
51+
describe('fetching the list of commits', () => {
5252
const info = { repo: 'repo', owner: 'user', number: 1 }
5353

5454
it('works with new PRs', async () => {
@@ -61,4 +61,40 @@ describe('commitlint-bot', () => {
6161
expect(github.pullRequests.getCommits).toHaveBeenCalledWith(info)
6262
})
6363
})
64+
65+
describe('no comment when no errors/warnings', () => {
66+
it('works with new PRs', async () => {
67+
github = githubMock(['fix: bug #1'])
68+
robot.auth = () => Promise.resolve(github)
69+
70+
await robot.receive(events.opened)
71+
expect(github.issues.createComment).toNotHaveBeenCalled()
72+
})
73+
74+
it('works with updated PRs', async () => {
75+
github = githubMock(['fix: bug #1'])
76+
robot.auth = () => Promise.resolve(github)
77+
78+
await robot.receive(events.synchronize)
79+
expect(github.issues.createComment).toNotHaveBeenCalled()
80+
})
81+
})
82+
83+
// describe('write comment on errors/warnings', () => {
84+
// it('works with new PRs', async () => {
85+
// github = githubMock(['fix: bug #1'])
86+
// robot.auth = () => Promise.resolve(github)
87+
88+
// await robot.receive(events.opened)
89+
// expect(github.issues.createComment).toHaveBeenCalled()
90+
// })
91+
92+
// it('works with updated PRs', async () => {
93+
// github = githubMock(['fix: bug #1'])
94+
// robot.auth = () => Promise.resolve(github)
95+
96+
// await robot.receive(events.synchronize)
97+
// expect(github.issues.createComment).toHaveBeenCalled()
98+
// })
99+
// })
64100
})

Diff for: test/format.test.js

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Packages
2+
const expect = require('expect')
3+
4+
// Ours
5+
const format = require('../lib/format')
6+
7+
describe('lib/format', () => {
8+
const report1 = { commits: {} }
9+
const report2 = {
10+
commits: {
11+
'1': { errors: [], warnings: [{ message: 'warning message' }] }
12+
}
13+
}
14+
const report3 = {
15+
commits: {
16+
'2': {
17+
errors: [{ message: 'error message' }],
18+
warnings: [{ message: 'warning message' }]
19+
}
20+
}
21+
}
22+
23+
it('generates summary', () => {
24+
// Report 1
25+
expect(format(report1).summary).toEqual('found 0 problems, 0 warnings')
26+
// Report 2
27+
expect(format(report2).summary).toEqual('found 0 problems, 1 warnings')
28+
// Report 3
29+
expect(format(report3).summary).toEqual('found 1 problems, 1 warnings')
30+
})
31+
32+
it('generates comment body', () => {
33+
// Report 1
34+
expect(format(report1).message).toEqual('')
35+
// Report 2
36+
expect(format(report2).message).toMatch(/Commit: 1/)
37+
expect(format(report2).message).toMatch(/warning message/)
38+
// Report 3
39+
expect(format(report3).message).toMatch(/Commit: 2/)
40+
expect(format(report3).message).toMatch(/error message/)
41+
expect(format(report3).message).toMatch(/warning message/)
42+
})
43+
})

Diff for: test/mocks/github.js

+10-5
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,22 @@
22
const expect = require('expect')
33

44
// Mock necessary GitHub APIs here
5-
module.exports = (data = ['fix: issue #1']) => {
5+
module.exports = (data = []) => {
66
return {
7+
issues: {
8+
createComment: expect.createSpy()
9+
},
710
repos: {
811
createStatus: expect.createSpy()
912
},
1013
pullRequests: {
11-
getCommits: expect.createSpy().andReturn({
12-
data: data.map(e => {
13-
return { commit: { message: e } }
14+
getCommits: expect.createSpy().andReturn(
15+
Promise.resolve({
16+
data: data.map(e => {
17+
return { sha: 'abcd', commit: { message: e } }
18+
})
1419
})
15-
})
20+
)
1621
},
1722
paginate: async (fn, callback) => {
1823
callback(await fn)

0 commit comments

Comments
 (0)