-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
142 lines (111 loc) · 4.27 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
const core = require('@actions/core');
const { GitHub, context } = require('@actions/github');
function getPullRequestFromContext() {
if (!context.payload) {
return undefined;
}
if (context.payload.pull_request && context.payload.pull_request.number) {
// pull_request event
return context.payload.pull_request.number;
}
if (!context.payload.check_suite || !context.payload.check_suite.pull_requests) {
return undefined;
}
// check_suite event
for (const pullRequest of context.payload.check_suite.pull_requests) {
if (pullRequest.number) {
return pullRequest.number;
}
}
return undefined;
}
function pullRequestHasLabel(pullRequestResponseData, labelName) {
if (!pullRequestResponseData.labels) {
return false;
}
const res = pullRequestResponseData.labels.filter((label) => label.name === labelName);
return res.length > 0;
}
const run = async () => {
const { owner, repo } = context.repo;
core.debug(`repository: ${owner}/${repo}`);
const pull_number = getPullRequestFromContext();
if (!pull_number) {
core.warning('Could not get pull request information from context');
return;
}
core.info(`pull request number: ${pull_number}`);
const token = core.getInput('GITHUB_TOKEN', { require: true });
const automergeLabel = core.getInput('AUTOMERGE') != ''?core.getInput('AUTOMERGE'): 'automerge';
const blockLabel = core.getInput('LABEL') != ''?core.getInput('LABEL'): '';
const mergeMethod = core.getInput('MERGE_METHOD') != ''? core.getInput('MERGE_METHOD'):'squash';
const github = new GitHub(token);
core.info(`auto merge label is ${automergeLabel}`);
core.info(`auto merge label is ${blockLabel}`);
core.info(`auto merge label is ${mergeMethod}`);
const pullRequestResponse = await github.pulls.get({
owner,
repo,
pull_number,
});
core.info(JSON.stringify(pullRequestResponse));
const pullRequestResponseStatus = pullRequestResponse.status || undefined;
const pullRequestResponseData = pullRequestResponse.data || {};
core.info(JSON.stringify(pullRequestResponseStatus));
core.info(JSON.stringify(pullRequestResponseData));
if (pullRequestResponseStatus !== 200 || Object.entries(pullRequestResponseData).length === 0) {
throw new Error('Could not get pull request information from API');
}
core.info(`retrieved data for pull request #${pull_number}`);
if (!pullRequestHasLabel(pullRequestResponseData, automergeLabel)) {
core.info(`label found ${pullRequestResponseData}`);
core.warning('Pull request does not have the automerge label');
return;
}
if (pullRequestHasLabel(pullRequestResponseData, blockLabel)) {
core.warning('Pull request has the work-in-progress label');
return;
}
core.info(`pull request mergeable: ${pullRequestResponseData.mergeable}`);
core.info(`pull request merged: ${pullRequestResponseData.merged}`);
core.info(`pull request state: ${pullRequestResponseData.state}`);
if (
pullRequestResponseData.state !== 'open'
|| pullRequestResponseData.mergeable !== true
|| pullRequestResponseData.merged !== false
) {
core.warning('Pull Request is not in a mergeable state');
return;
}
try {
core.info(`Trying to merge #${pull_number}`);
const commit_title = `${pullRequestResponseData.title} (#${pull_number})`;
const commit_message = pullRequestResponseData.body.replace(/(## PR Checklist[\w\W\s\S]*)/gm, '').trim();
core.info(`commit_title is ${commit_title}`);
core.info(`commit_message is ${commit_message}`);
core.info(`owner is ${owner}`);
core.info(`repo is ${repo}`);
core.info(`pull_number is ${pull_number}`);
core.info(`mergeMethod is ${mergeMethod}`);
const pullRequestMergeResponse = await github.pulls.merge({
owner,
repo,
pull_number,
commit_title,
commit_message,
merge_method: mergeMethod,
});
if (pullRequestMergeResponse.status !== 200) {
core.info(`failed with status`);
core.info(pullRequestMergeResponse.status);
core.warning(pullRequestMergeResponse.data.message);
return;
}
core.info(pullRequestMergeResponse.data.message);
} catch (e) {
core.warning(e.message);
}
};
run().catch((error) => {
core.setFailed(`An unexpected error occurred: ${error}, ${error.stack}.`);
});