-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
194 lines (162 loc) · 6.25 KB
/
index.js
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
const core = require("@actions/core");
const github = require("@actions/github");
const octokit = getOctokit();
async function run() {
if (isTriggeredByPullRequestEvent()) {
try {
await checkTargetBranch();
} catch (err) {
console.error(err);
core.setFailed("An unexpected error occurred.");
}
}
}
function isTriggeredByPullRequestEvent() {
const eventName = github.context.eventName;
if (eventName === "pull_request" || eventName === "pull_request_target") {
return true;
}
core.setFailed("This action should only be used with 'pull_request' or 'pull_request_target' events.");
return false;
}
async function checkTargetBranch() {
const base = getBaseBranch();
const head = getHeadBranch();
if (isFeatureBranch(head)) {
if (isDevelopmentBranch(base)) {
console.log("Head branch is a feature branch and targets a development branch.");
} else if (isSemestryTestingBranch(base)) {
console.log("Head branch is a feature branch and targets a Semestry version-test branch.");
} else if (isFeatureBranch(base)) {
console.log("Head branch is a feature branch and targets another feature branch.");
if (!isPullRequestDraft()) {
await convertPullRequestToDraft();
let msg = "This pull request was converted to a draft, because it targets another feature branch. After that branch has been merged, the base of this pull request will be updated automatically and you may mark it ready for review again.";
console.log(msg);
await postComment(msg);
}
} else {
let msg = "⛔ Pull requests for feature branches should target a development branch or another feature branch.";
console.log(msg);
await postCommentIfBaseChanged(msg);
}
} else if (isHotfixBranch(head)) {
if (isDevelopmentBranch(base)) {
console.log("Head branch is a hotfix branch and targets a development branch.");
} else if (isMainBranch(base)) {
console.log("Head branch is a hotfix branch and targets a main branch.");
} else {
let msg = "⛔ Pull requests for hotfix branches should target a development branch or a main branch.";
console.log(msg);
await postCommentIfBaseChanged(msg);
}
} else if (isFixBranch(head)) {
if (isSemestryTestingBranch(base)) {
console.log("Head branch is a fix branch and targets a Semestry version-test branch.");
} else {
let msg = "⛔ Pull requests for fix branches should target a version-test branch.";
console.log(msg);
await postCommentIfBaseChanged(msg);
}
} else if (isStagingBranch(head)) {
if (isSemestryStagingBranch(base)) {
console.log("Head branch is a staging branch and targets a Semestry version-staging branch.");
} else {
let msg = "⛔ Pull requests for staging branches should target a version-staging branch.";
console.log(msg);
await postCommentIfBaseChanged(msg);
}
} else {
let msg = `⚠ The head branch name doesn't have the \`${getFeatureBranchPrefix()}\` or \`${getHotfixBranchPrefix()}\` prefix.`;
console.log(msg);
await postCommentIfBaseChanged(msg);
}
}
function getBaseBranch() {
return github.context.payload.pull_request.base.ref;
}
function getHeadBranch() {
return github.context.payload.pull_request.head.ref;
}
function getFeatureBranchPrefix() {
return core.getInput("feature_branch_prefix", { required: true });
}
function getHotfixBranchPrefix() {
return core.getInput("hotfix_branch_prefix", { required: true });
}
function getFixBranchPrefix() {
return core.getInput("fix_branch_prefix", { required: true });
}
function getStagingBranchPrefix() {
return core.getInput("staging_branch_prefix", { required: true });
}
function isFeatureBranch(branch) {
return branch.startsWith(getFeatureBranchPrefix());
}
function isHotfixBranch(branch) {
return branch.startsWith(getHotfixBranchPrefix());
}
function isFixBranch(branch) {
return branch.startsWith(getFixBranchPrefix());
}
function isStagingBranch(branch) {
return branch.startsWith(getStagingBranchPrefix());
}
function isMainBranch(branch) {
const pattern = core.getInput("main_branch_pattern", { required: true });
return new RegExp(pattern).test(branch);
}
function isDevelopmentBranch(branch) {
const pattern = core.getInput("development_branch_pattern", { required: true });
return new RegExp(pattern).test(branch);
}
function isSemestryStagingBranch(branch) {
const pattern = core.getInput("semestry_staging_branch_pattern", { required: true });
return new RegExp(pattern).test(branch);
}
function isSemestryTestingBranch(branch) {
const pattern = core.getInput("semestry_testing_branch_pattern", { required: true });
return new RegExp(pattern).test(branch);
}
function isPullRequestDraft() {
return github.context.payload.pull_request.draft;
}
function getOctokit() {
let token = process.env.GITHUB_TOKEN;
if (token) {
return github.getOctokit(token);
} else {
throw "The GITHUB_TOKEN environment variable must be set.";
}
}
async function convertPullRequestToDraft() {
let pullRequestId = github.context.payload.pull_request.node_id;
await octokit.graphql(`
mutation {
convertPullRequestToDraft(input: {pullRequestId: "${pullRequestId}"}) {
pullRequest {
id
}
}
}
`);
}
async function postComment(comment) {
let pr = github.context.payload.pull_request;
let owner = pr.base.repo.owner.login;
let repo = pr.base.repo.name;
let prNumber = pr.number;
await octokit.rest.issues.createComment({
owner: owner,
repo: repo,
issue_number: prNumber,
body: comment
});
}
async function postCommentIfBaseChanged(comment) {
let payload = github.context.payload;
if (payload.action === "opened" || (payload.changes != null && payload.changes.hasOwnProperty('base'))) {
await postComment(comment);
}
}
run();