-
Notifications
You must be signed in to change notification settings - Fork 0
/
dangerfile.ts
131 lines (110 loc) · 2.9 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
import {
type GitHubMergeRef,
type GitHubPRDSL as LibraryGitHubDSL,
type GitHubRepo,
danger,
fail,
} from 'danger';
import { ProjectPrefix } from './project.config';
type GitHubPRDSL = LibraryGitHubDSL & {
head: GitHubMergeRef & {
repo: GitHubRepo & {
has_projects: boolean;
};
};
milestone: Record<string, unknown> | null;
labels: unknown[];
project_id: string | null;
};
const BranchPrefix = {
TASK: 'task',
FIX: 'fix',
} as const;
const DangerConfig = {
TITLE: {
IS_REQUIRED: true,
PATTERN: new RegExp(
`^((${
ProjectPrefix.APP
})-[0-9]{1,6}): (.*\\S)$|(${ProjectPrefix.ENVIRONMENTS.join(
'|',
)}) to (${ProjectPrefix.ENVIRONMENTS.join('|')})$`,
),
},
ASSIGNEES: {
IS_REQUIRED: true,
},
MILESTONE: {
IS_REQUIRED: true,
},
LABELS: {
IS_REQUIRED: true,
},
BRANCH: {
IS_REQUIRED: true,
PATTERN: new RegExp(
`^((${Object.values(BranchPrefix).join('|')})/(${
ProjectPrefix.APP
})-[0-9]{1,6})-[a-zA-Z0-9-]+$|(${ProjectPrefix.ENVIRONMENTS.join(
'|',
)})$`,
),
},
};
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 > 0;
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 (DangerConfig.TITLE.IS_REQUIRED) {
checkTitle(DangerConfig.TITLE.PATTERN);
}
if (DangerConfig.ASSIGNEES.IS_REQUIRED) {
checkAssignees();
}
if (DangerConfig.MILESTONE.IS_REQUIRED) {
checkMilestone();
}
if (DangerConfig.LABELS.IS_REQUIRED) {
checkLabels();
}
if (DangerConfig.BRANCH.IS_REQUIRED) {
checkBranch(DangerConfig.BRANCH.PATTERN);
}
};
applyDanger();