This repository was archived by the owner on May 19, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
273 lines (221 loc) · 6.77 KB
/
Copy pathdependabot-automerge.yml
File metadata and controls
273 lines (221 loc) · 6.77 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
name: Dependabot Auto Merge
on:
workflow_dispatch:
pull_request:
types:
- opened
- reopened
- synchronize
- ready_for_review
issue_comment:
types:
- created
workflow_run:
workflows:
- CI
types:
- completed
schedule:
- cron: '0 10 */1 * *'
permissions:
contents: write
pull-requests: write
issues: write
concurrency:
group: dependabot-automerge
cancel-in-progress: false
jobs:
automerge:
runs-on: blacksmith-4vcpu-ubuntu-2404
steps:
- name: Process Dependabot PRs
uses: actions/github-script@v8
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const owner = context.repo.owner;
const repo = context.repo.repo;
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
async function getOpenDependabotPrs() {
const prs = await github.paginate(
github.rest.pulls.list,
{
owner,
repo,
state: 'open',
per_page: 100,
}
);
return prs.filter(
pr => pr.user.login === 'dependabot[bot]'
);
}
async function maybeComment(issue_number, body) {
const comments = await github.paginate(
github.rest.issues.listComments,
{
owner,
repo,
issue_number,
per_page: 100,
}
);
const alreadyPosted = comments.some(
c =>
c.user.login === 'github-actions[bot]' &&
c.body.trim() === body.trim()
);
if (alreadyPosted) {
core.info(
`Skipping duplicate comment on #${issue_number}: ${body}`
);
return false;
}
await github.rest.issues.createComment({
owner,
repo,
issue_number,
body,
});
core.info(`Posted comment on #${issue_number}: ${body}`);
return true;
}
async function waitForStableMergeability(prNumber) {
for (let i = 0; i < 12; i++) {
const pr = await github.rest.pulls.get({
owner,
repo,
pull_number: prNumber,
});
const state = pr.data.mergeable_state;
core.info(
`#${prNumber} mergeable_state=${state}`
);
if (
state &&
state !== 'unknown' &&
state !== 'unstable'
) {
return pr.data;
}
await sleep(5000);
}
return null;
}
async function processPr(prNumber) {
const pr = await waitForStableMergeability(prNumber);
if (!pr) {
core.warning(
`Could not determine mergeability for #${prNumber}`
);
return;
}
if (pr.user.login !== 'dependabot[bot]') {
return;
}
if (pr.state !== 'open') {
return;
}
if (pr.draft) {
return;
}
core.info(`Processing #${pr.number}: ${pr.title}`);
// Already merged/queued
if (pr.auto_merge) {
core.info(`#${pr.number} already has auto-merge`);
return;
}
const state = pr.mergeable_state;
// Merge immediately if clean
if (state === 'clean' || state === 'has_hooks') {
try {
await github.rest.pulls.merge({
owner,
repo,
pull_number: pr.number,
merge_method: 'squash',
});
core.info(`Merged #${pr.number}`);
} catch (error) {
core.warning(
`Merge failed for #${pr.number}: ${error.message}`
);
}
return;
}
// Needs rebase
if (state === 'behind') {
await maybeComment(
pr.number,
'@dependabot rebase'
);
return;
}
// Hard conflicts / broken branch
if (
state === 'dirty' ||
state === 'blocked'
) {
const comments = await github.paginate(
github.rest.issues.listComments,
{
owner,
repo,
issue_number: pr.number,
per_page: 100,
}
);
const alreadyRebased = comments.some(c =>
c.body.includes('@dependabot rebase')
);
if (!alreadyRebased) {
await maybeComment(
pr.number,
'@dependabot rebase'
);
} else {
await maybeComment(
pr.number,
'@dependabot recreate'
);
}
return;
}
core.info(
`#${pr.number} not mergeable yet (${state})`
);
}
//
// EVENT ROUTING
//
// pull_request event
if (context.payload.pull_request) {
await processPr(
context.payload.pull_request.number
);
return;
}
// workflow_run event
if (context.payload.workflow_run) {
const prs =
context.payload.workflow_run.pull_requests || [];
for (const pr of prs) {
await processPr(pr.number);
}
return;
}
// issue_comment event
if (context.payload.issue) {
const issue = context.payload.issue;
if (issue.pull_request) {
await processPr(issue.number);
}
return;
}
// scheduled sweep
const prs = await getOpenDependabotPrs();
for (const pr of prs) {
await processPr(pr.number);
}