-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathdangerfile.ts
80 lines (68 loc) · 1.67 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
import {
danger,
fail,
type GitHubPRDSL as LibGitHubDSL,
type GitHubMergeRef,
type GitHubRepo,
} from 'danger';
import { ProjectPrefix } from './project.config';
type GitHubPRDSL = LibGitHubDSL & {
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: {
PATTERN: new RegExp(
`^((${ProjectPrefix.APPS.join(
'|',
)})-[0-9]{1,6}): (.*\\S)$|(${ProjectPrefix.ENVIRONMENTS.join(
'|',
)}) to (${ProjectPrefix.ENVIRONMENTS.join('|')})$`,
),
},
BRANCH: {
PATTERN: new RegExp(
`^((${Object.values(BranchPrefix).join('|')})/(${ProjectPrefix.APPS.join(
'|',
)})-[0-9]{1,6})-[a-zA-Z0-9-]+$|(${ProjectPrefix.ENVIRONMENTS.join(
'|',
)})$`,
),
},
};
const pr = danger.github.pr as GitHubPRDSL;
const checkTitle = (titlePattern: RegExp) => {
const isTitleValid = titlePattern.test(pr.title);
if (!isTitleValid) {
fail(
`The pull request title should match the following pattern: ${titlePattern}.`,
);
}
};
const checkBranch = (branchPattern: RegExp) => {
const isBranchValid = branchPattern.test(pr.head.ref);
if (!isBranchValid) {
fail(
`The pull request branch should match the following pattern: ${branchPattern}.`,
);
}
};
const applyDanger = () => {
if (DangerConfig.TITLE.PATTERN) {
checkTitle(DangerConfig.TITLE.PATTERN);
}
if (DangerConfig.BRANCH.PATTERN) {
checkBranch(DangerConfig.BRANCH.PATTERN);
}
};
applyDanger();