-
Notifications
You must be signed in to change notification settings - Fork 3
/
dangerfile.ts
103 lines (85 loc) · 3.69 KB
/
dangerfile.ts
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import { danger, warn, fail, message, TextDiff } from 'danger';
const SMALL_PR_FILES = 10;
const SMALL_PR_LINES = 200;
const DOC_FILE_MATCH = '**/*.md';
const SRC_FILE_REGEXP = /test.*\.([tj]s?)$/;
// No PR is too small to include a description of why you made a change
if (!danger.github.pr.body || !danger.github.pr.body.includes('## What\'s this PR do?')) {
const title = ':clipboard: Missing Summary';
const idea =
'Can you add a Summary? ' +
'To do so, add a `## What\'s this PR do?` section to your PR description. ' +
'This is a good place to explain the motivation for making this change.';
message(`${title} - <i>${idea}</i>`);
}
if (!danger.github.pr.title) {
const title = ':id: Missing PR Title';
const idea = 'Can you add the relevant title?';
warn(`${title} - <i>${idea}</i>`);
}
const touchedFiles = danger.git.created_files.concat(danger.git.modified_files);
const allFiles = touchedFiles.concat(danger.git.deleted_files);
const diffsList: Promise<(TextDiff | null)[]> = Promise.all(allFiles.map((p) => danger.git.diffForFile(p)));
diffsList
.then((diffs) => diffs.filter(Boolean) as TextDiff[])
.then((diffs) => ({
removed: diffs.reduce((lines, diff) => lines + diff.removed.split('\n').length, 0),
added: diffs.reduce((lines, diff) => lines + diff.added.split('\n').length, 0),
lines: diffs.reduce((lines, diff) => lines + diff.added.split('\n').length + diff.removed.split('\n').length, 0),
files: diffs.length,
}))
.then((diff) => {
if (diff.added < diff.removed) {
message('Thanks! We :heart: removing more lines than added!');
}
if (diff.lines <= SMALL_PR_LINES && diff.files <= SMALL_PR_FILES) {
message('Thanks! We :heart: small PRs!');
}
if (diff.lines > SMALL_PR_LINES) {
warn(`This PR is changing more than ${SMALL_PR_LINES} lines.`);
}
if (diff.files > SMALL_PR_FILES) {
warn(`This PR is changing more than ${SMALL_PR_FILES} files.`);
}
});
// Request changes to src also include changes to tests.
const docs = danger.git.fileMatch(DOC_FILE_MATCH);
const appModified = touchedFiles.some((p) => p.match(SRC_FILE_REGEXP));
if (docs.edited) {
message('Thanks for updating docs! We :heart: documentation!');
}
if (appModified) {
message('Thanks for updating tests! Only YOU can prevent production fires. :fire::evergreen_tree::bear:');
}
// Warns if there are changes to package.json, and tags the team.
const packageJSON = danger.git.fileMatch('package.json');
const yarnLockfile = danger.git.fileMatch('yarn.lock');
const npmLockfile = danger.git.fileMatch('package-lock.json');
if (packageJSON.modified) {
const title = ':lock: package.json';
const idea = 'Changes were made to package.json.';
warn(`${title} - <i>${idea}</i>`);
}
if (packageJSON.modified && !yarnLockfile.modified) {
const title = ':lock: package.json';
const idea =
'If you’ve changed any dependencies (added, removed or updated any packages), ' +
'please run `yarn` and commit changes in yarn.lock file. ' +
'Make sure you’re using the correct yarn and node versions.';
fail(`${title} - <i>${idea}</i>`);
}
if (!packageJSON.modified && yarnLockfile.modified) {
const title = ':lock: package.json';
const idea =
'Changes were made to `yarn.lock`, but not to `package.json`. ' +
'Please remove `yarn.lock` changes from your pull request. ' +
'Try to run `git checkout master -- yarn.lock` and commit changes.';
fail(`${title} - <i>${idea}</i>`);
}
if (npmLockfile.modified) {
const title = ':rage: package-lock.json';
const idea =
'This PR is adding file `package-lock.json` and we don\'t use npm!. ' +
'Please remove `package-lock.json` and commit changes.';
fail(`${title} - <i>${idea}</i>`);
}