-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
index.js
394 lines (334 loc) · 11.3 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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
const assert = require("assert");
const { inspect } = require("util");
const { command } = require("execa");
const core = require("@actions/core");
const { Octokit } = require("@octokit/core");
const TEMPORARY_BRANCH_NAME = `tmp-create-or-update-pull-request-action-${Math.random()
.toString(36)
.substr(2)}`;
main();
async function main() {
if (!process.env.GITHUB_TOKEN) {
core.setFailed(
`GITHUB_TOKEN is not configured. Make sure you made it available to your action
uses: gr2m/create-or-update-pull-request-action@master
env:
GITHUB_TOKEN: \${{ secrets.GITHUB_TOKEN }}`
);
return;
}
if (!process.env.GITHUB_REPOSITORY) {
core.setFailed(
'GITHUB_REPOSITORY missing, must be set to "<repo owner>/<repo name>"'
);
return;
}
if (!process.env.GITHUB_REF) {
core.setFailed(
"GITHUB_REF missing, must be set to the repository's default branch"
);
return;
}
const octokit = new Octokit({
auth: process.env.GITHUB_TOKEN,
});
const [owner, repo] = process.env.GITHUB_REPOSITORY.split("/");
try {
const inputs = {
title: core.getInput("title"),
body: core.getInput("body"),
branch: core.getInput("branch").replace(/^refs\/heads\//, ""),
path: core.getInput("path"),
commitMessage: core.getInput("commit-message"),
author: core.getInput("author"),
labels: core.getInput("labels"),
assignees: core.getInput("assignees"),
reviewers: core.getInput("reviewers"),
team_reviewers: core.getInput("team_reviewers"),
autoMerge: core.getInput("auto-merge"),
updatePRTitleAndBody: core.getInput("update-pull-request-title-and-body"),
};
core.debug(`Inputs: ${inspect(inputs)}`);
if (
inputs.autoMerge &&
!["merge", "squash", "rebase"].includes(inputs.autoMerge)
) {
core.setFailed(
`auto-merge is set to "${inputs.autoMerge}", but must be one of "merge", "squash", "rebase"`
);
process.exit(1);
}
const {
data: { default_branch },
} = await octokit.request(`GET /repos/{owner}/{repo}`, {
owner,
repo,
});
const DEFAULT_BRANCH = default_branch;
core.debug(`DEFAULT_BRANCH: ${DEFAULT_BRANCH}`);
const { hasChanges } = await getLocalChanges(inputs.path);
if (!hasChanges) {
if (inputs.path) {
core.info(`No local changes matching "${inputs.path}"`);
} else {
core.info("No local changes");
}
core.setOutput("result", "unchanged");
process.exit(0); // there is currently no neutral exit code
}
core.debug(`Local changes found`);
await runShellCommand(`git checkout -b '${TEMPORARY_BRANCH_NAME}'`);
const gitUser = await getGitUser();
if (gitUser) {
core.debug(`Git User already configured as: ${inspect(gitUser)}`);
} else {
const matches = inputs.author.match(/^([^<]+)\s*<([^>]+)>$/);
assert(
matches,
`The "author" input "${inputs.author}" does not conform to the "Name <email@domain.test>" format`
);
const [, name, email] = matches;
await setGitUser({
name,
email,
});
}
if (inputs.path) {
core.debug(`Committing local changes matching "${inputs.path}"`);
await runShellCommand(`git add "${inputs.path}"`);
} else {
core.debug(`Committing all local changes`);
await runShellCommand("git add .");
}
await runShellCommand(
`git commit -m '${inputs.commitMessage}' --author '${inputs.author}'`
);
const currentBranch = await runShellCommand(
`git rev-parse --abbrev-ref HEAD`
);
if (currentBranch === DEFAULT_BRANCH) {
core.info(`Already in base branch "${currentBranch}".`);
} else {
core.debug(`rebase all local changes on base branch`);
await runShellCommand(
`git fetch https://x-access-token:${process.env.GITHUB_TOKEN}@github.com/${process.env.GITHUB_REPOSITORY}.git ${DEFAULT_BRANCH}:${DEFAULT_BRANCH}`
);
await runShellCommand(`git stash --include-untracked`);
await runShellCommand(`git rebase -X theirs '${DEFAULT_BRANCH}'`);
}
core.debug(`Try to fetch and checkout remote branch "${inputs.branch}"`);
const remoteBranchExists = await checkOutRemoteBranch(inputs.branch);
core.debug(`Pushing local changes`);
await runShellCommand(
`git push -f https://x-access-token:${process.env.GITHUB_TOKEN}@github.com/${process.env.GITHUB_REPOSITORY}.git HEAD:refs/heads/${inputs.branch}`
);
if (remoteBranchExists) {
const q = `head:${inputs.branch} type:pr is:open repo:${process.env.GITHUB_REPOSITORY}`;
const { data } = await octokit.request("GET /search/issues", {
q,
});
if (data.total_count > 0) {
const prInfo = data.items[0]; // Assuming there is only one PR for given branch
core.setOutput(`pull-request-number`, prInfo.number);
core.setOutput(`result`, `updated`);
core.info(
`Existing pull request for branch "${inputs.branch}" updated: ${prInfo.html_url}`
);
if (inputs.updatePRTitleAndBody === "false") return;
await octokit.request(`POST /repos/{owner}/{repo}/pulls/{number}`, {
owner,
repo,
number: prInfo.number,
title: inputs.title,
body: inputs.body,
});
core.info(`PR title and body are updated`);
return;
}
}
core.debug(`Creating pull request`);
const {
data: { html_url, number, node_id },
} = await octokit.request(`POST /repos/{owner}/{repo}/pulls`, {
owner,
repo,
title: inputs.title,
body: inputs.body,
head: inputs.branch,
base: DEFAULT_BRANCH,
});
core.info(`Pull request created: ${html_url} (#${number})`);
core.setOutput(`pull-request-number`, number);
core.setOutput(`result`, `created`);
if (inputs.labels) {
core.debug(`Adding labels: ${inputs.labels}`);
const labels = inputs.labels.trim().split(/\s*,\s*/);
const { data } = await octokit.request(
`POST /repos/{owner}/{repo}/issues/{issue_number}/labels`,
{
owner,
repo,
issue_number: number,
labels,
}
);
core.info(`Labels added: ${labels.join(", ")}`);
core.debug(inspect(data));
}
if (inputs.assignees) {
core.debug(`Adding assignees: ${inputs.assignees}`);
const assignees = inputs.assignees.trim().split(/\s*,\s*/);
const { data } = await octokit.request(
`POST /repos/{owner}/{repo}/issues/{issue_number}/assignees`,
{
owner,
repo,
issue_number: number,
assignees,
}
);
core.info(`Assignees added: ${assignees.join(", ")}`);
core.debug(inspect(data));
}
if (inputs.reviewers || inputs.team_reviewers) {
let params = {
owner,
repo,
pull_number: number
}
let reviewers = null;
let team_reviewers = null;
if(inputs.reviewers) {
core.debug(`Adding reviewers: ${inputs.reviewers}`)
reviewers = (inputs.reviewers || "").trim().split(/\s*,\s*/);
params = {
...params,
reviewers
}
};
if(inputs.team_reviewers) {
core.debug(`Adding team reviewers: ${inputs.team_reviewers}`)
team_reviewers = (inputs.team_reviewers || "").trim().split(/\s*,\s*/);
params = {
...params,
team_reviewers
}
} ;
const { data } = await octokit.request(
`POST /repos/{owner}/{repo}/pulls/{pull_number}/requested_reviewers`,
params
);
if(reviewers) {
core.info(`Reviewers added: ${reviewers.join(", ")}`);
}
if(team_reviewers) {
core.info(`Team reviewers added: ${team_reviewers.join(", ")}`);
}
core.debug(inspect(data));
}
if (inputs.autoMerge) {
const query = `
mutation($pullRequestId: ID!, $mergeMethod: PullRequestMergeMethod!, $commitHeadline: String!) {
enablePullRequestAutoMerge(input: {pullRequestId: $pullRequestId, mergeMethod: $mergeMethod, commitHeadline: $commitHeadline}) {
actor {
login
}
}
}
`;
try {
const result = await octokit.graphql(query, {
pullRequestId: node_id,
mergeMethod: inputs.autoMerge.toUpperCase(),
commitHeadline: inputs.title,
});
core.info(`Auto merge enabled`);
core.debug(inspect(result));
} catch (error) {
core.warning(
`Auto merge could not be enabled for the pull request. Make sure the feature is enabled in the repository settings`
);
}
}
await runShellCommand(`git stash pop || true`);
} catch (error) {
core.info(inspect(error));
core.setFailed(error.message);
}
}
async function getLocalChanges(path) {
const output = await runShellCommand(`git status ${path}`);
if (/nothing to commit, working tree clean/i.test(output)) {
return {};
}
const hasUncommitedChanges =
/(Changes to be committed|Changes not staged|Untracked files)/.test(output);
return {
hasUncommitedChanges,
hasChanges: hasUncommitedChanges,
};
}
async function getGitUser() {
try {
const name = await runShellCommand("git config --get user.name");
const email = await runShellCommand("git config --get user.email");
return {
name,
email,
};
} catch (error) {
return;
}
}
async function setGitUser({ name, email }) {
core.debug(`Configuring user.name as "${name}"`);
await runShellCommand(`git config --global user.name '${name}'`);
core.debug(`Configuring user.email as "${email}"`);
await runShellCommand(`git config --global user.email '${email}'`);
}
async function checkOutRemoteBranch(branch) {
try {
const currentBranch = await runShellCommand(
`git rev-parse --abbrev-ref HEAD`
);
if (currentBranch === branch) {
core.info(`Already in "${branch}".`);
return true;
}
core.debug(`fetching "${branch}" branch from remote`);
await runShellCommand(
`git fetch https://x-access-token:${process.env.GITHUB_TOKEN}@github.com/${process.env.GITHUB_REPOSITORY}.git ${branch}:${branch}`
);
await runShellCommand(`git branch`);
core.debug(`Checking out "${branch}" branch locally`);
await runShellCommand(`git checkout ${branch}`);
core.info(`Remote branch "${branch}" checked out locally.`);
try {
await runShellCommand(
`git cherry-pick --strategy recursive --strategy-option theirs ${TEMPORARY_BRANCH_NAME}`
);
} catch (error) {
// https://github.com/gr2m/create-or-update-pull-request-action/issues/245
if (/The previous cherry-pick is now empty/.test(error.stderr)) {
await runShellCommand(`git cherry-pick --skip`);
}
}
return true;
} catch (error) {
core.info(`Branch "${branch}" does not yet exist on remote.`);
await runShellCommand(`git checkout -b ${branch}`);
return false;
}
}
async function runShellCommand(commandString) {
core.debug(`$ ${commandString}`);
try {
const { stdout, stderr } = await command(commandString, { shell: true });
const output = [stdout, stderr].filter(Boolean).join("\n");
core.debug(output);
return output;
} catch (error) {
core.debug(inspect(error));
throw error;
}
}