|
| 1 | +import load from '@commitlint/load'; |
| 2 | +import lint from '@commitlint/lint'; |
| 3 | + |
| 4 | +const minPRDescriptionLength = 10; |
| 5 | + |
| 6 | +// Utility functions |
| 7 | +const processReport = (type, report, warnOnly = false) => { |
| 8 | + if (report.warnings.length > 0) { |
| 9 | + warn( |
| 10 | + `${type} '${report.input}': ${report.warnings |
| 11 | + .map((w) => w.message) |
| 12 | + .join(', ')}`, |
| 13 | + ); |
| 14 | + } |
| 15 | + |
| 16 | + if (report.errors.length > 0) { |
| 17 | + const reportFn = warnOnly ? warn : fail; |
| 18 | + |
| 19 | + reportFn( |
| 20 | + `${type} '${report.input}': ${report.errors |
| 21 | + .map((e) => e.message) |
| 22 | + .join(', ')}`, |
| 23 | + ); |
| 24 | + } |
| 25 | + |
| 26 | + return report.valid || warnOnly ? Promise.resolve() : Promise.reject(); |
| 27 | +}; |
| 28 | + |
| 29 | +const reportCommitMessage = (report) => processReport('Commit Message', report, true); |
| 30 | + |
| 31 | +const reportPRTitle = (report) => processReport('PR Title', report, false); |
| 32 | + |
| 33 | +const lintMessage = (message, opts, reporter) => |
| 34 | + lint( |
| 35 | + message, |
| 36 | + opts.rules, |
| 37 | + opts.parserPreset ? {parserOpts: opts.parserPreset.parserOpts} : {}, |
| 38 | + ).then(reporter); |
| 39 | + |
| 40 | +const pr = danger.github.pr; |
| 41 | + |
| 42 | +// check commit messages and PR name |
| 43 | +schedule( |
| 44 | + Promise.all([ |
| 45 | + load({}, {file: './.commitlintrc.json', cwd: process.cwd()}).then( |
| 46 | + (opts) => |
| 47 | + Promise.all( |
| 48 | + danger.git.commits |
| 49 | + .map((c) => c.message) |
| 50 | + .map((m) => lintMessage(m, opts, reportCommitMessage)), |
| 51 | + ) |
| 52 | + .catch(() => |
| 53 | + markdown( |
| 54 | + '> All commits should follow ' + |
| 55 | + '[Conventional commits](https://cheatography.com/albelop/cheat-sheets/conventional-commits). ' + |
| 56 | + 'It seems some of the commit messages are not following those rules, please fix them.', |
| 57 | + ), |
| 58 | + ) |
| 59 | + .then(() => lintMessage(pr.title, opts, reportPRTitle)) |
| 60 | + .catch(() => |
| 61 | + markdown( |
| 62 | + '> Pull request title should follow ' + |
| 63 | + '[Conventional commits](https://cheatography.com/albelop/cheat-sheets/conventional-commits).', |
| 64 | + ), |
| 65 | + ), |
| 66 | + ), |
| 67 | + new Promise((resolve, reject) => { |
| 68 | + // No PR is too small to include a description of why you made a change |
| 69 | + if (pr.body.length < minPRDescriptionLength) { |
| 70 | + warn(`:exclamation: Please include a description of your PR changes.`); |
| 71 | + |
| 72 | + markdown( |
| 73 | + '> Pull request should have a description of the underlying changes.', |
| 74 | + ); |
| 75 | + } |
| 76 | + }), |
| 77 | + ]), |
| 78 | +); |
0 commit comments