forked from TreTuna/sync-branches
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
152 lines (137 loc) · 5.42 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
const core = require("@actions/core");
const github = require("@actions/github");
const [owner, repo] = process.env.GITHUB_REPOSITORY.split("/");
async function run() {
try {
const fromBranch = core.getInput("FROM_BRANCH", { required: true });
const toBranch = core.getInput("TO_BRANCH", { required: true });
const githubToken = core.getInput("GITHUB_TOKEN", { required: true });
const pullRequestTitle = core.getInput("PULL_REQUEST_TITLE");
const pullRequestBody = core.getInput("PULL_REQUEST_BODY");
const pullRequestAutoMergeMethod = core.getInput("PULL_REQUEST_AUTO_MERGE_METHOD");
const pullRequestIsDraft =
core.getInput("PULL_REQUEST_IS_DRAFT").toLowerCase() === "true";
const contentComparison =
core.getInput("CONTENT_COMPARISON").toLowerCase() === "true";
const reviewers = JSON.parse(core.getInput("REVIEWERS"));
const team_reviewers = JSON.parse(core.getInput("TEAM_REVIEWERS"));
const labels = JSON.parse(core.getInput("LABELS"));
let isMerged = false;
console.log(
`Determining if ${fromBranch} should be synced to ${toBranch}...`
);
const octokit = new github.getOctokit(githubToken);
// Find all current open PR's that are from the source branch to the target branch
const { data: currentPulls } = await octokit.rest.pulls.list({
owner,
repo,
state: "open",
head: `${owner}:${fromBranch}`,
base: toBranch,
});
// Check if there's a current pull request for syncing our branches.
// First check if the title matches a custom provided pullRequestTitle,
// otherwise check if the title matches the default title.
const currentPull = currentPulls.find((pull) => {
return pull.title == pullRequestTitle ? pullRequestTitle : `sync: ${fromBranch} to ${toBranch}`;
});
// If there's no current PR open, then go through the steps of creating one
if (!currentPull) {
// Run content comparison (if enabled) to determine if we should create a PR
let shouldCreatePullRequest = true;
if (contentComparison) {
shouldCreatePullRequest = await hasContentDifference(
octokit,
fromBranch,
toBranch
);
}
if (shouldCreatePullRequest) {
const { data: pullRequest } = await octokit.rest.pulls.create({
owner,
repo,
head: fromBranch,
base: toBranch,
title: pullRequestTitle
? pullRequestTitle
: `sync: ${fromBranch} to ${toBranch}`,
body: pullRequestBody
? pullRequestBody
: `sync-branches: New code has just landed in ${fromBranch}, so let's bring ${toBranch} up to speed!`,
draft: pullRequestIsDraft,
});
// Add reviewers/team reviewers if they're set in the inputs
if (reviewers.length > 0 || team_reviewers.length > 0) {
octokit.rest.pulls.requestReviewers({
owner,
repo,
pull_number: pullRequest.number,
reviewers,
team_reviewers,
});
}
// Add labels if they're set in the inputs
if (labels.length > 0) {
octokit.rest.issues.addLabels({
owner,
repo,
issue_number: pullRequest.number,
labels
})
}
// Auto-merge the PR if it's enabled
if (pullRequestAutoMergeMethod) {
try {
await octokit.rest.pulls.merge({
owner,
repo,
pull_number: pullRequest.number,
merge_method: pullRequestAutoMergeMethod
});
isMerged = true;
} catch (err) {
isMerged = false;
}
}
console.log(
`Pull request (${pullRequest.number}) successfully created${isMerged ? ' and merged' : ''}! You can view it here: ${pullRequest.html_url}`
);
core.setOutput("PULL_REQUEST_URL", pullRequest.html_url.toString());
core.setOutput("PULL_REQUEST_NUMBER", pullRequest.number.toString());
core.summary.addRaw('Pull Request successfully created :rocket:', true)
core.summary.addLink(`PR: #${pullRequest.number}`, pullRequest.html_url.toString());
} else {
console.log(
`There is no content difference between ${fromBranch} and ${toBranch}.`
);
core.summary.addRaw(`There is no content difference between ${fromBranch} and ${toBranch}. Not creating a PR.`, true)
}
} else {
// PR already exists, nothing to do. Just output a message.
console.log(
`There is already a pull request (${currentPull.number}) to ${toBranch} from ${fromBranch}.`,
`You can view it here: ${currentPull.html_url}`
);
core.setOutput("PULL_REQUEST_URL", currentPull.html_url.toString());
core.setOutput("PULL_REQUEST_NUMBER", currentPull.number.toString());
core.summary.addRaw('Pull Request already exists', true)
core.summary.addLink(`PR: #${currentPull.number}`, currentPull.html_url.toString());
}
// Write the summary buffer
core.summary.write({overwrite: true})
} catch (error) {
core.setFailed(error.message);
}
}
async function hasContentDifference(octokit, fromBranch, toBranch) {
const { data: response } = await octokit.rest.repos.compareCommits({
owner,
repo,
base: toBranch,
head: fromBranch,
page: 1,
per_page: 1,
});
return response.files.length > 0;
}
run();