-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
75 lines (60 loc) · 2.69 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
const core = require("@actions/core");
const github = require("@actions/github");
const createBranch = require("./create-branch");
async function run() {
try {
const sourceBranch = core.getInput("SOURCE_BRANCH", { required: true });
const githubRepo = core.getInput("GITHUB_REPO", { required: true });
let sourceRepo = core.getInput("SOURCE_REPO", { required: false });
const targetBranches = core.getInput("TARGET_BRANCH", { required: true });
const githubToken = core.getInput("GITHUB_TOKEN", { required: true });
const title = core.getInput("TITLE", { required: false });
const body = core.getInput("BODY", { required: false });
const draft = core.getInput("DRAFT", { required: false });
const targetBranchesArray = targetBranches.split(",");
const repo = { repo: githubRepo.split("/")[1], owner: githubRepo.split("/")[0] };
const octokit = new github.GitHub(githubToken);
const { data: currentPulls } = await octokit.pulls.list(repo);
if (!sourceRepo) {
sourceRepo = repo.owner;
}
const { data: { commit: { sha } } } = await octokit.repos.getBranch({
owner: sourceRepo,
repo: repo.repo,
branch: sourceBranch,
})
for (let branch of targetBranchesArray) {
console.log(`Making a pull request for ${branch} from ${sourceRepo}:${sourceBranch}.`);
const newBranch = `${branch}-catchup-${sha.slice(0,7)}`;
await createBranch(octokit, repo, sha, newBranch);
const currentPull = currentPulls.find((pull) => {
return pull.head.ref === newBranch && pull.base.ref === branch;
});
if (!currentPull) {
const { data: pullRequest } = await octokit.pulls.create({
...repo,
head: newBranch,
base: branch,
title: title || `[Catchup]: Merge ${sourceRepo}:${sourceBranch} to ${branch}`,
body: body || `Catchup PR to merge ${sourceRepo}:${sourceBranch} in ${branch} with ${newBranch}`,
draft: draft === "true",
});
console.log(
`Pull request (${pullRequest.number}) successful! You can view it here: ${pullRequest.url}.`
);
core.setOutput("PULL_REQUEST_URL", pullRequest.url.toString());
core.setOutput("PULL_REQUEST_NUMBER", pullRequest.number.toString());
} else {
console.log(
`There is already a pull request (${currentPull.number}) to ${branch} from ${newBranch}.`,
`You can view it here: ${currentPull.url}`
);
core.setOutput("PULL_REQUEST_URL", currentPull.url.toString());
core.setOutput("PULL_REQUEST_NUMBER", currentPull.number.toString());
}
}
} catch (error) {
core.setFailed(error.message);
}
}
run();