-
Notifications
You must be signed in to change notification settings - Fork 10
163 lines (128 loc) · 5.34 KB
/
pr_validator.yml
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
# Original idea by SByteDev
# https://github.com/SByteDev
name: 🔍 PR Validator
on:
pull_request:
types:
- opened
- edited
- reopened
- synchronize
workflow_dispatch:
jobs:
Validate:
runs-on: ubuntu-latest
steps:
- name: Echo Credits
run: echo Credits to SByteDev''s branch validator. See https://github.com/SByteDev/BranchNameGenerator
- name: Validate
uses: actions/github-script@v5.0.0
with:
script: |
// See https://github.com/SByteDev/BranchNameGenerator
function generateBranchName(task, prefix, divider, version) {
if (task === undefined || task === "") {
return "";
}
// Replace non-word characters with divider.
var branch = task.replace(/([\W])/g, divider);
// Remove duplicated dividers.
if (divider == "-") {
branch = branch.replace(/-+/g, divider);
} else {
branch = branch.replace(/_+/g, divider);
}
// Replace uppercase characters with lovercase characters.
branch = branch.toLowerCase();
// Remove first divider.
if (branch[0] === divider) {
branch = branch.substring(1, branch.length);
}
// Remove last divider.
if (branch[branch.length - 1] === divider) {
branch = branch.substring(0, branch.length - 1);
}
if (prefix !== undefined && prefix !== "") {
// Add tack type folder.
branch = prefix + "/" + branch;
}
if (version !== undefined && version !== "") {
// Add version folder.
branch = version + "/" + branch;
}
return branch;
};
const prTitle = context.payload.pull_request.title;
const prDescription = context.payload.pull_request.body;
const prBranch = context.payload.pull_request.head.ref
const prDescriptionToLower = prDescription.toLowerCase();
var isIssueFound = false;
const regex = /(resolves) #(\d+)/g;
const matches = prDescriptionToLower.match(regex);
if (!matches) {
core.setFailed("The PR description must start with \"Resolves #{issue_number}\", where {issue_number} — is the number of the main linked issue.");
return;
}
console.log("Found a linked issue!");
var firstMatch = matches[0];
if (!prDescriptionToLower.startsWith(firstMatch)) {
core.setFailed("The PR description must start with \"Resolves #{issue_number}\", where {issue_number} — is the number of the main linked issue.");
return;
}
const issueId = firstMatch.replace('resolves #','').trim();
console.log("Getting data of \#" + issueId + " ...");
const issue = await github.rest.issues.get({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: issueId,
});
if (!issue) {
core.setFailed("Issue with such a number was not found.");
return;
}
console.log("Checking if \#" + issueId + " is not a PR ...");
if (issue.data.pull_request) {
core.setFailed("\#" + issueId + " is a PR, but must be an issue.");
return;
}
console.log("Comparing titles ...");
if (issue.data.title != prTitle) {
core.setFailed("The title of the PR must be equal with the linked issue title.");
return;
}
console.log("Looking for branch prefix depending on issue's labels ...");
var branchPrefix;
const labels = issue.data.labels
if (!labels) {
branchPrefix = ""
} else {
for (index in labels) {
const label = labels[index]
if (label.name.includes("bug")) {
console.log("Found a bug label!");
if (branchPrefix == "feature") {
core.setFailed("The issue contains both bug and feature labels: that's not correct");
}
branchPrefix = "bug"
}
if (label.name.includes("feature")) {
console.log("Found a feature label!");
if (branchPrefix == "bug") {
core.setFailed("The issue contains both bug and feature labels: that's not correct");
}
branchPrefix = "feature"
}
}
}
console.log("Validating branch name ...");
const expectedBranchName = generateBranchName(
issue.data.title,
branchPrefix,
"-",
""
);
if (expectedBranchName != prBranch) {
core.setFailed("The PR git branch doesn't follow the required rules.\nExpected: " + expectedBranchName + "\nActual: " + prBranch);
return;
}
console.log("The PR is valid! 👍");