-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathdangerfile.ts
241 lines (200 loc) · 6.45 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
export default async (): undefined => {
if (danger.github.pr.labels.length > 0) {
fail("PR is not allowed to have labels");
return;
}
if (danger.github.pr.milestone) {
fail("PR is not allowed to have milestone");
return;
}
if (danger.github.pr.assignee?.id !== danger.github.pr.user.id) {
fail("Only PR author could be assigned to PR");
return;
}
const commitTest = danger.git.commits.map((commit) =>
commit.message.match(/^(\w+):/),
);
if (!commitTest.every((match) => match)) {
fail("Unable to analyze commit types");
return;
}
const commitTypes = new Set(commitTest.map((commitMatch) => commitMatch[1])),
hasFeatureCommit = commitTypes.has("feat"),
hasFixCommit = commitTypes.has("fix"),
hasDocsCommit = commitTypes.has("docs"),
hasTestCommit = commitTypes.has("test");
const hasBreakingCommit = danger.git.commits
.map((commit) => commit.message.match(/\sBREAKING CHANGE:\s/g))
.some((match) => match);
const commitFiles = danger.git.created_files
.concat(danger.git.modified_files)
.concat(danger.git.deleted_files),
hasDocsChanges = commitFiles.some(
(fileName) => fileName.startsWith("docs/") || fileName === "mkdocs.yml",
),
hasTestChanges = commitFiles.some(
(fileName) => fileName.startsWith("tests/") || fileName === "pytest.ini",
),
hasSourceChanges = commitFiles.some(
(fileName) =>
fileName.startsWith("src/") || fileName === "pyproject.toml",
);
if (hasDocsCommit & !hasDocsChanges) {
fail("Commit with the 'docs' type should change documentation files");
return;
}
if (hasTestCommit & !hasTestChanges) {
fail("Commit with the 'test' type should change test files");
return;
}
if (hasFixCommit & !hasSourceChanges) {
fail("Commit with the 'fix' type should change source files");
return;
}
if (hasFeatureCommit & !hasSourceChanges) {
fail("Commit with the 'feat' type should change source files");
return;
}
if (danger.github.pr.commits > 3) {
fail("PR has too much commits");
return;
}
if (commitTest.length !== commitTypes.size) {
fail("PR can not contain commits with the same type");
return;
}
const branchTest = danger.github.pr.head.ref.match(/^issue-(\d+)$/);
if (!branchTest) {
fail("Branch name should be 'issue-N' where N is the issue number");
return;
}
const issueNumber = branchTest[1];
if (danger.github.pr.body !== `Resolves #${issueNumber}`) {
fail("PR should resolve related issue");
return;
}
for (const commit of danger.git.commits) {
if (!commit.message.split(/\r?\n/)[0].endsWith(` #${issueNumber}`)) {
fail("The first line of each commit should ends with the issue number");
return;
}
}
const issueJSON = await danger.github.api.issues.get({
owner: danger.github.thisPR.owner,
repo: danger.github.thisPR.repo,
issue_number: parseInt(issueNumber),
});
if (issueJSON.status !== 200) {
fail(`Unable to check issue #${issueNumber}`);
return;
}
if (issueJSON.data.closed_at) {
fail("Closed issue can't be implemented");
return;
}
if (issueJSON.data.milestone) {
if (issueJSON.data.milestone.closed_at) {
fail("Issues of closed milestone can't be implemented");
return;
}
}
if (issueJSON.data.assignee) {
fail("Issue can't have an assignee");
return;
}
const issueLabels = new Set(issueJSON.data.labels.map((label) => label.name)),
hasIncompatibleLabel = issueLabels.has("backward incompatible"),
hasFeatureLabel = issueLabels.has("feature"),
hasBugLabel = issueLabels.has("bug"),
hasDocumentationLabel = issueLabels.has("documentation"),
hasQuestionLabel = issueLabels.has("question"),
hasBlockedLabel = issueLabels.has("blocked");
if (hasFeatureLabel & hasBugLabel) {
fail("An issue can't be a bug and a feature simultaneously");
return;
}
if (hasIncompatibleLabel ^ hasBreakingCommit) {
fail(
"Only issue marked as backward incompatible is allowed to have breaking changes",
);
return;
}
if (hasFeatureLabel ^ hasFeatureCommit) {
fail("Only issue marked as feature is allowed to have feat commit");
return;
}
if (hasBugLabel ^ hasFixCommit) {
fail("Only issue marked as bug is allowed to have fix commit");
return;
}
if (hasDocumentationLabel & !hasDocsCommit) {
fail("Issue marked as documentation should have docs commit");
return;
}
if (hasFeatureLabel & !hasDocsCommit) {
fail("Issue marked as feature should have docs commit");
return;
}
if (hasQuestionLabel & !hasDocsCommit) {
fail("Issue marked as question should have docs commit");
return;
}
if (hasBlockedLabel) {
fail("Issues marked as blocked can not be implemented");
return;
}
const issueText = issueJSON.data.body || "",
issueLines = issueText.split(/\r?\n/).map((line) => line.trim());
for (const line of issueLines) {
if (line.match(/^[*+-] \[[xX]\] /)) {
fail("Create a milestone instead of the issue with the task list");
return;
}
}
const labelsJSON = await danger.github.api.issues.listLabelsForRepo({
owner: danger.github.thisPR.owner,
repo: danger.github.thisPR.repo,
});
if (labelsJSON.status !== 200) {
fail("Unable to check repository labels");
return;
}
const allowedLabels = new Set([
"backward incompatible",
"blocked",
"bug",
"documentation",
"feature",
"question",
"released",
]);
for (const repoLabel of labelsJSON.data) {
if (repoLabel.color !== "ededed") {
fail(`The color of the ${repoLabel.name} should be 'ededed'`);
return;
} else if (repoLabel.description !== "") {
fail(`The description of the ${repoLabel.name} should be empty`);
return;
} else if (!allowedLabels.has(repoLabel.name)) {
fail(`Unknown label ${repoLabel.name}`);
return;
}
}
const milestonesJSON = await danger.github.api.issues.listMilestones({
owner: danger.github.thisPR.owner,
repo: danger.github.thisPR.repo,
});
if (milestonesJSON.status !== 200) {
fail("Unable to check repository milestones");
return;
}
for (const milestone of milestonesJSON.data) {
if (milestone.description !== "") {
fail(`The description of the ${milestone.title} should be empty`);
return;
} else if (milestone.due_on) {
fail(`The due date of the ${milestone.title} should not be set`);
return;
}
}
};