Skip to content
This repository was archived by the owner on Apr 1, 2020. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions lib/format.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
const template = `
There were the following issues with this Pull Request
The following commits do not follow the [Conventional Commits][cc] rules:

<PLACEHOLDER>

<details>

<DETAILS>

</details>

You may need to [change the commit messages][ref] to comply with the \
repository contributing guidelines.

Expand All @@ -13,6 +19,7 @@ issues [here][issues].

Happy coding!

[cc]: https://www.conventionalcommits.org
[ref]: https://help.github.com/articles/changing-a-commit-message/
[repo]: https://github.com/z0al/commitlint-bot
[issues]: https://github.com/z0al/commitlint-bot/issues
Expand All @@ -24,15 +31,18 @@ Happy coding!
* @param {Array} report
*/
function format(commits) {
let message = "";
let details = "";
let message = commits.map(c => `* ${c.sha}`).join("\n")

commits.forEach(c => {
message += `* Commit: ${c.sha}\n`;
message += c.errors.map(e => ` - ✖ ${e.message}\n`).join("");
message += c.warnings.map(w => ` - ⚠ ${w.message}\n`).join("");
details += `* Commit: ${c.sha}\n`;
details += c.errors.map(e => ` - ✖ ${e.message}\n`).join("");
details += c.warnings.map(w => ` - ⚠ ${w.message}\n`).join("");
});

return template.replace("<PLACEHOLDER>", message);
return template
.replace("<PLACEHOLDER>", message)
.replace("<DETAILS>", details);
}

module.exports = format;
58 changes: 33 additions & 25 deletions test/format.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,43 @@
const format = require('../lib/format')

const commits = [
// #1
[
{
sha: 'abc',
errors: [],
warnings: [{ message: 'warning message' }]
}
],
// #1
[{
sha: 'abc',
errors: [],
warnings: [{
message: 'warning message'
}]
}],

// #2
[
{
sha: 'def',
errors: [{ message: 'error message' }],
warnings: [{ message: 'warning message' }]
}
]
// #2
[{
sha: 'def',
errors: [{
message: 'error message'
}],
warnings: [{
message: 'warning message'
}]
}]
]

test('repalces placeholder', () => {
expect(format(commits[0])).not.toMatch(/PLACEHOLDER/)
test('replaces placeholder', () => {
expect(format(commits[0])).not.toMatch(/PLACEHOLDER/)
})

test('replaces details', () => {
expect(format(commits[0])).not.toMatch(/DETAILS/)
})

test('generates comment body', () => {
// #1
expect(format(commits[0])).toMatch(/Commit: abc/)
expect(format(commits[0])).toMatch(/warning message/)
// #2
expect(format(commits[1])).toMatch(/Commit: def/)
expect(format(commits[1])).toMatch(/error message/)
expect(format(commits[1])).toMatch(/warning message/)
// #1
expect(format(commits[0])).toMatch(/Commit: abc/)
expect(format(commits[0])).toMatch(/warning message/)
// #2
expect(format(commits[1])).toMatch(/Commit: def/)
expect(format(commits[1])).toMatch(/error message/)
expect(format(commits[1])).toMatch(/warning message/)
// test both
expect(format(commits.flat())).toMatch(/\* abc\n\* def/)
})