-
Notifications
You must be signed in to change notification settings - Fork 1
/
dangerfile.ts
125 lines (103 loc) · 2.56 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
import { type GitHubPRDSL as LibraryGitHubDSL, danger, fail } from "danger";
import { ProjectPrefix } from "./project.config";
const LABELS_EMPTY_LENGTH = 0;
type GitHubPRDSL = LibraryGitHubDSL & {
labels: unknown[];
milestone: Record<string, unknown> | null;
project_id: null | string;
};
type DangerConfig = {
ASSIGNEES: {
IS_REQUIRED: boolean;
};
BRANCH: {
PATTERN: RegExp | null;
};
LABELS: {
IS_REQUIRED: boolean;
};
MILESTONE: {
IS_REQUIRED: boolean;
};
TITLE: {
PATTERN: RegExp | null;
};
};
const config: DangerConfig = {
ASSIGNEES: {
IS_REQUIRED: true,
},
BRANCH: {
PATTERN: new RegExp(
`^[0-9]{1,6}-${ProjectPrefix.CHANGE_TYPES.join("|")}-[a-zA-Z0-9-]+$|(${ProjectPrefix.ENVIRONMENT})$`,
),
},
LABELS: {
IS_REQUIRED: true,
},
MILESTONE: {
IS_REQUIRED: true,
},
TITLE: {
PATTERN: new RegExp(
`^(${ProjectPrefix.CHANGE_TYPES.join("|")})(\\((${ProjectPrefix.SCOPES.APPS.join("|")}|${ProjectPrefix.SCOPES.PACKAGES.join("|")})(\\/(${ProjectPrefix.SCOPES.APPS.join("|")}|${ProjectPrefix.SCOPES.PACKAGES.join("|")}))*\\))?: (.*\\S )?(${ProjectPrefix.ISSUE_PREFIXES.join("|")})-[0-9]{1,6}((\\.[0-9]+){1,2})?$`,
),
},
};
const pr = danger.github.pr as GitHubPRDSL;
const checkAssignees = (): void => {
const hasAssignees = Boolean(pr.assignee);
if (!hasAssignees) {
fail("This pull request should have at least one assignee.");
}
};
const checkTitle = (titlePattern: RegExp): void => {
const isTitleValid = titlePattern.test(pr.title);
if (!isTitleValid) {
fail(
`The pull request title should match the following pattern: ${String(
titlePattern,
)}.`,
);
}
};
const checkMilestone = (): void => {
const hasMilestone = Boolean(pr.milestone);
if (!hasMilestone) {
fail("This pull request should have a milestone.");
}
};
const checkLabels = (): void => {
const hasLabels = pr.labels.length > LABELS_EMPTY_LENGTH;
if (!hasLabels) {
fail("This pull request should have at least one label.");
}
};
const checkBranch = (branchPattern: RegExp): void => {
const isBranchValid = branchPattern.test(pr.head.ref);
if (!isBranchValid) {
fail(
`The pull request branch should match the following pattern: ${String(
branchPattern,
)}.`,
);
}
};
const applyDanger = (): void => {
if (config.TITLE.PATTERN) {
checkTitle(config.TITLE.PATTERN);
}
if (config.ASSIGNEES.IS_REQUIRED) {
checkAssignees();
}
if (config.MILESTONE.IS_REQUIRED) {
checkMilestone();
}
if (config.LABELS.IS_REQUIRED) {
checkLabels();
}
if (config.BRANCH.PATTERN) {
checkBranch(config.BRANCH.PATTERN);
}
};
applyDanger();