forked from sudoStatus200/create-sync-pr
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
67 lines (56 loc) · 2.31 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
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 targetBranches = core.getInput("TARGET_BRANCH", { required: true });
const githubToken = core.getInput("GITHUB_TOKEN", { required: true });
const targetBranchesArray = targetBranches.split(",");
for (let branch of targetBranchesArray) {
console.log(`Making a pull request for ${branch} from ${sourceBranch}.`);
const {
payload: { repository },
} = github.context;
const octokit = new github.GitHub(githubToken);
//part of test
const { data: currentPulls } = await octokit.pulls.list({
owner: repository.owner.login,
repo: repository.name,
});
//create new branch from master branch and PR between new branch and target branch
const context = github.context;
const newBranch = `${branch}-sync-${context.sha.slice(-4)}`;
await createBranch(octokit, context, newBranch);
const currentPull = currentPulls.find((pull) => {
return pull.head.ref === newBranch && pull.base.ref === branch;
});
if (!currentPull) {
const { data: pullRequest } = await octokit.pulls.create({
owner: repository.owner.login,
repo: repository.name,
head: newBranch,
base: branch,
title: `sync: ${branch} with ${newBranch}`,
body: `sync-branches: syncing branch with ${newBranch}`,
draft: false,
});
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();