forked from the-wright-jamie/Update-PR-Info-Action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
200 lines (171 loc) · 6.44 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
195
196
197
198
199
200
const core = require("@actions/core");
const github = require("@actions/github");
async function run() {
try {
const baseTokenRegex = new RegExp("%basebranch%", "g");
const headTokenRegex = new RegExp("%headbranch%", "g");
const inputs = {
token: core.getInput("repo-token", { required: true }),
baseBranchRegex: core.getInput("base-branch-regex"),
headBranchRegex: core.getInput("head-branch-regex"),
lowercaseBranch:
core.getInput("lowercase-branch").toLowerCase() === "true",
titleTemplate: core.getInput("title-template"),
titleUpdateAction: core.getInput("title-update-action").toLowerCase(),
titleInsertSpace:
core.getInput("title-insert-space").toLowerCase() === "true",
titleUppercaseBaseMatch:
core.getInput("title-uppercase-base-match").toLowerCase() === "true",
titleUppercaseHeadMatch:
core.getInput("title-uppercase-head-match").toLowerCase() === "true",
bodyTemplate: core.getInput("body-template"),
bodyUpdateAction: core.getInput("body-update-action").toLowerCase(),
bodyNewlineCount: parseInt(core.getInput("body-newline-count")),
bodyUppercaseBaseMatch:
core.getInput("body-uppercase-base-match").toLowerCase() === "true",
bodyUppercaseHeadMatch:
core.getInput("body-uppercase-head-match").toLowerCase() === "true",
failOnError:
!(core.getInput("fail-on-error").toLowerCase() === "false"),
};
const error = (msg) => {
if (inputs.failOnError)
core.setFailed(msg);
else
core.warning(msg);
};
const baseBranchRegex = inputs.baseBranchRegex.trim();
const matchBaseBranch = baseBranchRegex.length > 0;
const headBranchRegex = inputs.headBranchRegex.trim();
const matchHeadBranch = headBranchRegex.length > 0;
if (!matchBaseBranch && !matchHeadBranch) {
error("No branch regex values have been specified");
return;
}
const matches = {
baseMatch: "",
headMatch: "",
};
if (matchBaseBranch) {
const baseBranchName = github.context.payload.pull_request.base.ref;
const baseBranch = inputs.lowercaseBranch
? baseBranchName.toLowerCase()
: baseBranchName;
core.info(`Base branch: ${baseBranch}`);
const baseMatches = baseBranch.match(new RegExp(baseBranchRegex));
if (!baseMatches) {
error("Base branch name does not match given regex");
return;
}
matches.baseMatch = baseMatches[0];
core.info(`Matched base branch text: ${matches.baseMatch}`);
core.setOutput("baseMatch", matches.baseMatch);
}
if (matchHeadBranch) {
const headBranchName = github.context.payload.pull_request.head.ref;
const headBranch = inputs.lowercaseBranch
? headBranchName.toLowerCase()
: headBranchName;
core.info(`Head branch: ${headBranch}`);
const headMatches = headBranch.match(new RegExp(headBranchRegex));
if (!headMatches) {
error("Head branch name does not match given regex");
return;
}
matches.headMatch = headMatches[0];
core.info(`Matched head branch text: ${matches.headMatch}`);
core.setOutput("headMatch", matches.headMatch);
}
const request = {
owner: github.context.repo.owner,
repo: github.context.repo.repo,
pull_number: github.context.payload.pull_request.number,
};
const upperCase = (upperCase, text) =>
upperCase ? text.toUpperCase() : text;
const title = github.context.payload.pull_request.title || "";
const processedTitleText = inputs.titleTemplate
.replace(
baseTokenRegex,
upperCase(inputs.titleUppercaseBaseMatch, matches.baseMatch)
)
.replace(
headTokenRegex,
upperCase(inputs.titleUppercaseHeadMatch, matches.headMatch)
);
core.info(`Processed title text: ${processedTitleText}`);
const updateTitle =
{
prefix: !title
.toLowerCase()
.startsWith(processedTitleText.toLowerCase()),
suffix: !title.toLowerCase().endsWith(processedTitleText.toLowerCase()),
replace: title.toLowerCase() !== processedTitleText.toLowerCase(),
}[inputs.titleUpdateAction] || false;
core.setOutput("titleUpdated", updateTitle.toString());
if (updateTitle) {
request.title = {
prefix: processedTitleText.concat(
inputs.titleInsertSpace ? " " : "",
title
),
suffix: title.concat(
inputs.titleInsertSpace ? " " : "",
processedTitleText
),
replace: processedTitleText,
}[inputs.titleUpdateAction];
core.info(`New title: ${request.title}`);
} else {
core.warning("No updates were made to PR title");
}
const body = github.context.payload.pull_request.body || "";
const processedBodyText = inputs.bodyTemplate
.replace(
baseTokenRegex,
upperCase(inputs.bodyUppercaseBaseMatch, matches.baseMatch)
)
.replace(
headTokenRegex,
upperCase(inputs.bodyUppercaseHeadMatch, matches.headMatch)
);
core.info(`Existing body text: [${body}]`);
core.info(`Processed body text: [${processedBodyText}]`);
const updateBody =
{
prefix: !body.trim().toLowerCase().startsWith(processedBodyText.trim().toLowerCase()),
suffix: !body.trim().toLowerCase().endsWith(processedBodyText.trim().toLowerCase()),
replace: body.trim().toLowerCase() !== processedBodyText.trim().toLowerCase(),
}[inputs.bodyUpdateAction] || false;
core.setOutput("bodyUpdated", updateBody.toString());
if (updateBody) {
request.body = {
prefix: processedBodyText.concat(
"\n".repeat(inputs.bodyNewlineCount),
body
),
suffix: body.concat(
"\n".repeat(inputs.bodyNewlineCount),
processedBodyText
),
replace: processedBodyText,
}[inputs.bodyUpdateAction];
core.info(`New body: ${request.body}`);
} else {
core.warning("No updates were made to PR body");
}
if (!updateTitle && !updateBody) {
return;
}
const octokit = github.getOctokit(inputs.token);
const response = await octokit.rest.pulls.update(request);
core.info(`Response: ${response.status}`);
if (response.status !== 200) {
core.error("Updating the pull request has failed");
}
} catch (error) {
core.error(error);
core.setFailed(error.message);
}
}
run();