-
-
Notifications
You must be signed in to change notification settings - Fork 9.9k
Expand file tree
/
Copy pathdangerfile.ts
More file actions
83 lines (70 loc) · 2.5 KB
/
dangerfile.ts
File metadata and controls
83 lines (70 loc) · 2.5 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
79
80
81
82
83
import { execSync } from 'child_process';
import { danger, fail } from 'danger';
execSync('npm install lodash');
// eslint-disable-next-line depend/ban-dependencies
const flatten = require('lodash/flatten.js');
// eslint-disable-next-line depend/ban-dependencies
const intersection = require('lodash/intersection.js');
// eslint-disable-next-line depend/ban-dependencies
const isEmpty = require('lodash/isEmpty.js');
const pkg = require('../code/package.json');
const prLogConfig = pkg['pr-log'];
const Versions = {
PATCH: 'PATCH',
MINOR: 'MINOR',
MAJOR: 'MAJOR',
};
const ciLabels = ['ci:normal', 'ci:merged', 'ci:daily', 'ci:docs'];
const branchVersion = Versions.MINOR;
const checkRequiredLabels = (labels: string[]) => {
const forbiddenLabels = flatten([
'ci: do not merge',
'in progress',
branchVersion !== Versions.MAJOR ? 'BREAKING CHANGE' : [],
branchVersion === Versions.PATCH ? 'feature request' : [],
]);
const requiredLabels = flatten([
prLogConfig.skipLabels || [],
(prLogConfig.validLabels || []).map((keyVal: string) => keyVal[0]),
]);
const blockingLabels = intersection(forbiddenLabels, labels);
if (!isEmpty(blockingLabels)) {
fail(
`PR is marked with ${blockingLabels.map((label: string) => `"${label}"`).join(', ')} label${
blockingLabels.length > 1 ? 's' : ''
}.`
);
}
const foundRequiredLabels = intersection(requiredLabels, labels);
if (isEmpty(foundRequiredLabels)) {
fail(`PR is not labeled with one of: ${JSON.stringify(requiredLabels)}`);
} else if (foundRequiredLabels.length > 1) {
fail(`Please choose only one of these labels: ${JSON.stringify(foundRequiredLabels)}`);
}
const foundCILabels = intersection(ciLabels, labels);
if (isEmpty(foundCILabels)) {
fail(`PR is not labeled with one of: ${JSON.stringify(ciLabels)}`);
} else if (foundCILabels.length > 1) {
fail(`Please choose only one of these labels: ${JSON.stringify(foundCILabels)}`);
}
};
const checkPrTitle = (title: string) => {
const match = title.match(/^[A-Z].+:\s[A-Z].+$/);
if (!match) {
fail(
`PR title must be in the format of "Area: Summary", With both Area and Summary starting with a capital letter
Good examples:
- "Docs: Describe Canvas Doc Block"
- "Svelte: Support Svelte v4"
Bad examples:
- "add new api docs"
- "fix: Svelte 4 support"
- "Vue: improve docs"`
);
}
};
if (prLogConfig) {
const { labels } = danger.github.issue;
checkRequiredLabels(labels.map((l) => l.name));
checkPrTitle(danger.github.pr.title);
}