-
Notifications
You must be signed in to change notification settings - Fork 6
/
dangerfile.ts
104 lines (89 loc) · 3.21 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
104
import {message, danger, warn} from 'danger';
import commitlint, {
CommitlintPluginConfig,
} from 'danger-plugin-conventional-commitlint';
import {rules} from './commitlint.config';
const mr = danger.gitlab.mr;
const method = mr.title.includes('Draft') ? warn : fail;
function markdonifyFiles(files: string[], mark: string): string {
if (files.length === 0) {
return '';
}
return `<br />- ${mark} ` + files.join(`<br /> - ${mark} `);
}
const editedMD = markdonifyFiles(danger.git.modified_files, 'MMM');
const createdMD = markdonifyFiles(danger.git.created_files, '+++');
const deletedMD = markdonifyFiles(danger.git.deleted_files, '---');
message('Changed files in this PR:' + editedMD + createdMD + deletedMD);
if (!mr.assignee) {
warn(
'This pull request needs an assignee, and optionally include a reviewer',
);
}
if (mr.description.length < 10) {
method('This merge request needs a description.');
}
const hasPackageChanges =
danger.git.modified_files.indexOf('package.json') > -1;
const hasLockfileChanges = danger.git.modified_files.indexOf('yarn.lock') > -1;
if (hasPackageChanges && !hasLockfileChanges) {
warn(
'There are package.json changes with no corresponding lockfile changes',
);
}
(async function dangerReport() {
const commitlintConfig: CommitlintPluginConfig = {
severity: mr.title.includes('Draft') ? 'warn' : 'fail',
};
// @ts-ignore
await commitlint(rules, commitlintConfig);
})();
const commits = danger.gitlab.commits;
if (commits.length > 5) {
warn(
'There are more than 5 commits in this merge request. Consider splitting into several MRs. This will make the review process much easier',
);
}
const modifiedFilesCount =
danger.git.modified_files.length + danger.git.created_files.length;
if (modifiedFilesCount > 20) {
warn(
'There are more than 20 files changed in this merge request. Consider splitting into several MRs. This will make the review process much easier',
);
}
for (const commit of commits) {
if (!commit.message.includes('Issue-URL')) {
warn(
`There is a problem with the commit message > ${commit.title} <br />` +
' - Issue-URL is missing. Please consider adding one',
);
}
}
for (const commit of commits) {
if (commit.title.includes('docs')) {
message(
`We love documentation! Thanks for adding some, ${commit.author_name}!`,
);
break;
}
}
function fileIsTest(fileName: string): boolean {
return (
fileName.includes('test') ||
fileName.includes('spec') ||
fileName.includes('stories')
);
}
// Check for testable files and skip other files like json, .env, scss,...
const testableFiles: string[] = ['.ts', '.js', '.jsx', '.tsx', '.sol'];
function fileIsTestable(fileName: string) {
return testableFiles.some((extension: string) =>
fileName.includes(extension),
);
}
const allFiles = [...danger.git.created_files, ...danger.git.modified_files];
if (allFiles.some(fileIsTest)) {
message(`We love tests! Thanks for adding some, ${mr.author.name}!`);
} else if (allFiles.some(fileIsTestable)) {
warn(`No tests were added. Consider adding some`);
}