-
Notifications
You must be signed in to change notification settings - Fork 6
182 lines (157 loc) · 6.41 KB
/
dependabot-auto-merge.yml
File metadata and controls
182 lines (157 loc) · 6.41 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
name: Dependabot Auto-Merge
on:
pull_request:
types: [opened, synchronize, reopened]
permissions:
contents: write
pull-requests: write
checks: read
jobs:
auto-merge:
runs-on: ubuntu-latest
if: github.actor == 'dependabot[bot]'
steps:
- name: Checkout code
uses: actions/checkout@v6
with:
token: ${{ secrets.GITHUB_TOKEN }}
- name: Get PR details
id: pr-details
uses: actions/github-script@v8
with:
script: |
const { data: pullRequest } = await github.rest.pulls.get({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: context.issue.number,
});
console.log('PR Title:', pullRequest.title);
console.log('PR Labels:', pullRequest.labels.map(l => l.name));
return {
title: pullRequest.title,
labels: pullRequest.labels.map(l => l.name),
isDependabot: pullRequest.user.login === 'dependabot[bot]'
};
- name: Wait for status checks
uses: actions/github-script@v8
with:
script: |
const maxWaitTime = 20 * 60 * 1000; // 20 minutes
const pollInterval = 30 * 1000; // 30 seconds
const startTime = Date.now();
while (Date.now() - startTime < maxWaitTime) {
const { data: checks } = await github.rest.checks.listForRef({
owner: context.repo.owner,
repo: context.repo.repo,
ref: context.payload.pull_request.head.sha,
});
const { data: statuses } = await github.rest.repos.listCommitStatusesForRef({
owner: context.repo.owner,
repo: context.repo.repo,
ref: context.payload.pull_request.head.sha,
});
// Check if all checks are completed
const allChecks = [...checks.check_runs, ...statuses];
const pendingChecks = allChecks.filter(check =>
check.status === 'in_progress' ||
check.status === 'queued' ||
check.state === 'pending'
);
if (pendingChecks.length === 0) {
console.log('All checks completed');
break;
}
console.log(`Waiting for ${pendingChecks.length} checks to complete...`);
await new Promise(resolve => setTimeout(resolve, pollInterval));
}
- name: Check if tests passed
id: check-tests
uses: actions/github-script@v8
with:
script: |
const { data: checks } = await github.rest.checks.listForRef({
owner: context.repo.owner,
repo: context.repo.repo,
ref: context.payload.pull_request.head.sha,
});
const { data: statuses } = await github.rest.repos.listCommitStatusesForRef({
owner: context.repo.owner,
repo: context.repo.repo,
ref: context.payload.pull_request.head.sha,
});
// Check all checks and statuses
const allChecks = [...checks.check_runs, ...statuses];
const failedChecks = allChecks.filter(check =>
check.conclusion === 'failure' ||
check.state === 'failure' ||
check.conclusion === 'cancelled' ||
check.state === 'error'
);
const successfulChecks = allChecks.filter(check =>
check.conclusion === 'success' ||
check.state === 'success'
);
console.log(`Total checks: ${allChecks.length}`);
console.log(`Successful checks: ${successfulChecks.length}`);
console.log(`Failed checks: ${failedChecks.length}`);
if (failedChecks.length > 0) {
console.log('Some checks failed:', failedChecks.map(c => c.name || c.context));
core.setOutput('tests-passed', 'false');
return false;
}
if (successfulChecks.length === 0) {
console.log('No successful checks found yet');
core.setOutput('tests-passed', 'false');
return false;
}
console.log('All checks passed!');
core.setOutput('tests-passed', 'true');
return true;
- name: Enable auto-merge
if: steps.check-tests.outputs.tests-passed == 'true'
uses: actions/github-script@v8
with:
script: |
await github.rest.pulls.createReview({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: context.issue.number,
event: 'APPROVE',
body: 'Auto-approving Dependabot PR after successful tests'
});
- name: Merge PR
if: steps.check-tests.outputs.tests-passed == 'true'
uses: actions/github-script@v8
with:
script: |
try {
await github.rest.pulls.merge({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: context.issue.number,
commit_title: `Auto-merge: ${context.payload.pull_request.title}`,
commit_message: 'Automatically merged by Dependabot after successful tests',
merge_method: 'squash'
});
console.log('PR merged successfully!');
} catch (error) {
console.log('Failed to merge PR:', error.message);
// Comment on PR about merge failure
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body: `Auto-merge failed: ${error.message}\n\nPlease merge manually after reviewing.`
});
}
- name: Comment on failure
if: steps.check-tests.outputs.tests-passed == 'false'
uses: actions/github-script@v8
with:
script: |
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body: 'Auto-merge cancelled: Tests failed or are still pending. Please check the test results and merge manually if appropriate.'
});