forked from msgflux/msgspec-ext
-
Notifications
You must be signed in to change notification settings - Fork 0
289 lines (248 loc) · 10.3 KB
/
merge-bot.yml
File metadata and controls
289 lines (248 loc) · 10.3 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
name: Merge Bot
on:
issue_comment:
types: [created]
permissions:
contents: write
pull-requests: write
issues: write
jobs:
merge-command:
name: Handle Merge Command
runs-on: ubuntu-latest
# Only run on PR comments
if: github.event.issue.pull_request
steps:
- name: Parse command
id: command
uses: actions/github-script@v8
with:
script: |
const comment = context.payload.comment.body.toLowerCase().trim();
const user = context.payload.comment.user.login;
core.info(`Comment from ${user}: ${comment}`);
// Check for merge command (case insensitive)
// Supported formats:
// - @mergebot merge
// - @merge-bot merge
// - /merge
// - merge (if alone)
const mergePatterns = [
/@merge-?bot\s+merge/i,
/^\/merge$/i,
/^merge$/i,
];
const isMergeCommand = mergePatterns.some(pattern => pattern.test(comment));
if (!isMergeCommand) {
core.info('Not a merge command, skipping');
return;
}
core.setOutput('should_merge', 'true');
core.info('✅ Merge command detected');
- name: React to comment
if: steps.command.outputs.should_merge == 'true'
uses: actions/github-script@v8
with:
script: |
// Add rocket emoji to show bot is processing
await github.rest.reactions.createForIssueComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: context.payload.comment.id,
content: 'rocket'
});
- name: Check permissions
if: steps.command.outputs.should_merge == 'true'
id: check_perms
uses: actions/github-script@v8
with:
script: |
const user = context.payload.comment.user.login;
// Check if user has write access
try {
const { data: permission } = await github.rest.repos.getCollaboratorPermissionLevel({
owner: context.repo.owner,
repo: context.repo.repo,
username: user
});
const hasPermission = ['admin', 'write'].includes(permission.permission);
if (!hasPermission) {
core.setFailed(`❌ @${user} does not have permission to merge (permission: ${permission.permission})`);
// Comment on PR
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.payload.issue.number,
body: `❌ @${user} you don't have permission to merge PRs. Only collaborators with write access can use merge commands.`
});
return;
}
core.info(`✅ User ${user} has ${permission.permission} access`);
core.setOutput('has_permission', 'true');
} catch (error) {
core.setFailed(`Error checking permissions: ${error.message}`);
}
- name: Get PR info
if: steps.check_perms.outputs.has_permission == 'true'
id: pr_info
uses: actions/github-script@v8
with:
script: |
const { data: pr } = await github.rest.pulls.get({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: context.payload.issue.number
});
core.setOutput('mergeable', pr.mergeable);
core.setOutput('draft', pr.draft);
core.setOutput('sha', pr.head.sha);
core.setOutput('title', pr.title);
core.info(`PR #${pr.number}: ${pr.title}`);
core.info(`- Mergeable: ${pr.mergeable}`);
core.info(`- Draft: ${pr.draft}`);
core.info(`- SHA: ${pr.head.sha}`);
- name: Check if PR is ready
if: steps.check_perms.outputs.has_permission == 'true'
id: ready
uses: actions/github-script@v8
with:
script: |
const mergeable = '${{ steps.pr_info.outputs.mergeable }}';
const draft = '${{ steps.pr_info.outputs.draft }}';
if (draft === 'true') {
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.payload.issue.number,
body: '❌ Cannot merge: PR is still a draft. Please mark it as ready for review first.'
});
core.setFailed('PR is draft');
return;
}
if (mergeable === 'false') {
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.payload.issue.number,
body: '❌ Cannot merge: PR has conflicts or is not mergeable. Please resolve conflicts first.'
});
core.setFailed('PR not mergeable');
return;
}
core.setOutput('ready', 'true');
- name: Wait for checks
if: steps.ready.outputs.ready == 'true'
id: wait_checks
uses: actions/github-script@v8
with:
script: |
const sha = '${{ steps.pr_info.outputs.sha }}';
const maxWaitMinutes = 10;
const pollInterval = 10000; // 10 seconds
const maxAttempts = (maxWaitMinutes * 60 * 1000) / pollInterval;
let attempt = 0;
while (attempt < maxAttempts) {
const { data: checks } = await github.rest.checks.listForRef({
owner: context.repo.owner,
repo: context.repo.repo,
ref: sha
});
const pendingChecks = checks.check_runs.filter(
check => check.status !== 'completed'
);
const failedChecks = checks.check_runs.filter(
check => check.conclusion === 'failure' || check.conclusion === 'cancelled'
);
if (failedChecks.length > 0) {
const failedNames = failedChecks.map(c => c.name).join(', ');
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.payload.issue.number,
body: `❌ Cannot merge: The following checks failed:\n\n${failedChecks.map(c => `- ❌ ${c.name}`).join('\n')}\n\nPlease fix the issues and try again.`
});
core.setFailed(`Failed checks: ${failedNames}`);
return;
}
if (pendingChecks.length === 0) {
core.info('✅ All checks passed');
core.setOutput('checks_passed', 'true');
return;
}
const pendingNames = pendingChecks.map(c => c.name).join(', ');
core.info(`Waiting for checks: ${pendingNames} (attempt ${attempt + 1}/${maxAttempts})`);
await new Promise(resolve => setTimeout(resolve, pollInterval));
attempt++;
}
// Timeout
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.payload.issue.number,
body: `⏱️ Timeout waiting for checks to complete (waited ${maxWaitMinutes} minutes).\n\nYou can try the merge command again once checks complete.`
});
core.setFailed('Timeout waiting for checks');
- name: Merge PR
if: steps.wait_checks.outputs.checks_passed == 'true'
uses: actions/github-script@v8
with:
script: |
const user = context.payload.comment.user.login;
const title = '${{ steps.pr_info.outputs.title }}';
try {
await github.rest.pulls.merge({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: context.payload.issue.number,
merge_method: 'squash',
commit_title: title,
commit_message: ''
});
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.payload.issue.number,
body: `✅ PR merged successfully by @${user}!`
});
// Add +1 reaction to original comment
await github.rest.reactions.createForIssueComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: context.payload.comment.id,
content: '+1'
});
core.info('✅ PR merged successfully');
} catch (error) {
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.payload.issue.number,
body: `❌ Failed to merge PR: ${error.message}`
});
core.setFailed(`Merge failed: ${error.message}`);
}
- name: Delete branch
if: steps.wait_checks.outputs.checks_passed == 'true'
uses: actions/github-script@v8
with:
script: |
const { data: pr } = await github.rest.pulls.get({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: context.payload.issue.number
});
// Don't delete main or protected branches
if (pr.head.ref === 'main' || pr.head.ref.startsWith('release/')) {
core.info('Skipping branch deletion for protected branch');
return;
}
try {
await github.rest.git.deleteRef({
owner: context.repo.owner,
repo: context.repo.repo,
ref: `heads/${pr.head.ref}`
});
core.info(`🗑️ Deleted branch: ${pr.head.ref}`);
} catch (error) {
core.warning(`Failed to delete branch: ${error.message}`);
}