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

Commit 949b84f

Browse files
committed
refactor(format): it's only about comment right now
1 parent 1813dbe commit 949b84f

File tree

2 files changed

+40
-66
lines changed

2 files changed

+40
-66
lines changed

lib/format.js

+18-33
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,7 @@
1-
/**
2-
* Formats commitlint report as GitHub status report
3-
*
4-
* @param {Object} report
5-
*/
6-
function format({ commits }) {
7-
// Keep errors/warnings count
8-
let errorsCount = 0
9-
let warnsCount = 0
10-
11-
// Details message
12-
let message = ''
13-
14-
for (const commit in commits) {
15-
message += `* Commit: ${commit}\n`
16-
const { errors, warnings } = commits[commit]
17-
for (const e of errors) {
18-
message += ` - ✖ ${e.message}\n`
19-
}
20-
for (const w of warnings) {
21-
message += ` - ⚠ ${w.message}\n`
22-
}
23-
errorsCount += errors.length
24-
warnsCount += warnings.length
25-
}
26-
27-
// Summary
28-
const summary = `found ${errorsCount} problems, ${warnsCount} warnings`
29-
if (errorsCount > 0 || warnsCount > 0) {
30-
message = `
1+
const template = `
312
There were the following issues with this Pull Request
323
33-
${message}
4+
<PLACEHOLDER>
345
356
You may need to [change the commit messages][ref] to comply with the \
367
repository contributing guidelines.
@@ -46,8 +17,22 @@ Happy coding!
4617
[repo]: https://github.com/ahmed-taj/commitlint-bot
4718
[issues]: https://github.com/ahmed-taj/commitlint-bot/issues
4819
`
49-
}
50-
return { summary, message }
20+
21+
/**
22+
* Formats array of commits warnings/errors as GitHub comment
23+
*
24+
* @param {Array} report
25+
*/
26+
function format(commits) {
27+
let message = ''
28+
29+
commits.forEach(c => {
30+
message += `* Commit: ${c.sha}\n`
31+
message += c.errors.map(e => ` - ✖ ${e.message}`).join('\n')
32+
message += c.warnings.map(w => ` - ⚠ ${w.message}`).join('\n')
33+
})
34+
35+
return template.replace('<PLACEHOLDER>', message)
5136
}
5237

5338
module.exports = format

test/format.test.js

+22-33
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,36 @@
1-
// Packages
2-
const expect = require('expect')
3-
41
// Ours
52
const format = require('../lib/format')
63

7-
const values = [
4+
const commits = [
85
// #1
9-
{ commits: {} },
10-
// #2
11-
{
12-
commits: {
13-
'1': { errors: [], warnings: [{ message: 'warning message' }] }
6+
[
7+
{
8+
sha: 'abc',
9+
errors: [],
10+
warnings: [{ message: 'warning message' }]
1411
}
15-
},
16-
// #3
17-
{
18-
commits: {
19-
'2': {
20-
errors: [{ message: 'error message' }],
21-
warnings: [{ message: 'warning message' }]
22-
}
12+
],
13+
14+
// #2
15+
[
16+
{
17+
sha: 'def',
18+
errors: [{ message: 'error message' }],
19+
warnings: [{ message: 'warning message' }]
2320
}
24-
}
21+
]
2522
]
2623

27-
test('generates summary', () => {
28-
// #1
29-
expect(format(values[0]).summary).toEqual('found 0 problems, 0 warnings')
30-
// #2
31-
expect(format(values[1]).summary).toEqual('found 0 problems, 1 warnings')
32-
// #3
33-
expect(format(values[2]).summary).toEqual('found 1 problems, 1 warnings')
24+
test('repalces placeholder', () => {
25+
expect(format(commits[0])).not.toMatch(/PLACEHOLDER/)
3426
})
3527

3628
test('generates comment body', () => {
3729
// #1
38-
expect(format(values[0]).message).toEqual('')
39-
30+
expect(format(commits[0])).toMatch(/Commit: abc/)
31+
expect(format(commits[0])).toMatch(/warning message/)
4032
// #2
41-
expect(format(values[1]).message).toMatch(/Commit: 1/)
42-
expect(format(values[1]).message).toMatch(/warning message/)
43-
// #3
44-
expect(format(values[2]).message).toMatch(/Commit: 2/)
45-
expect(format(values[2]).message).toMatch(/error message/)
46-
expect(format(values[2]).message).toMatch(/warning message/)
33+
expect(format(commits[1])).toMatch(/Commit: def/)
34+
expect(format(commits[1])).toMatch(/error message/)
35+
expect(format(commits[1])).toMatch(/warning message/)
4736
})

0 commit comments

Comments
 (0)