-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
87 lines (73 loc) · 2.9 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
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 targetBranchPattern = core.getInput("TARGET_BRANCH_STARTS_WITH", {
required: true,
});
const githubToken = core.getInput("GITHUB_TOKEN", { required: true });
const {
payload: { repository },
} = github.context;
const octokit = new github.GitHub(githubToken);
const { data: targetBranches } = await octokit.git.listMatchingRefs({
owner: repository.owner.login,
repo: repository.name,
ref: `heads/${targetBranchPattern}`,
});
for (let branchData of targetBranches) {
const branch = branchData.ref.replace("refs/heads/", "");
console.log(`Making a pull request for ${branch} from ${sourceBranch}.`);
// part of test
const { data: currentPulls } = await octokit.pulls.list({
owner: repository.owner.login,
repo: repository.name,
});
// create new branch from SOURCE_BRANCH and PR between new branch and target branch
const context = github.context;
const newBranch = `${sourceBranch}-to-${branch}`;
const currentPull = currentPulls.find((pull) => {
console.log(pull.head.ref, pull.base.ref);
return pull.head.ref === newBranch && pull.base.ref === branch;
});
if (!currentPull) {
await createBranch(octokit, context, newBranch);
const { data: pullRequest } = await octokit.pulls.create({
owner: repository.owner.login,
repo: repository.name,
head: newBranch,
base: branch,
title: `sync: ${sourceBranch} to ${branch}`,
body: `sync-branches: syncing ${branch} with ${sourceBranch}`,
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 {
// If PR exists update PR branch with sourceBranch
console.log(
`There is already a pull request (${currentPull.number}) to ${branch} from ${newBranch}.\n`
);
console.log("Updating PR branch...");
await octokit.repos.merge({
owner: repository.owner.login,
repo: repository.name,
base: newBranch,
head: context.sha,
});
console.log("PR branch updated\n");
console.log(`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();