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