-
-
Notifications
You must be signed in to change notification settings - Fork 1
178 lines (163 loc) · 7.95 KB
/
auto-merge-on-approval.yml
File metadata and controls
178 lines (163 loc) · 7.95 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
# ------------------------------------------------------------------------------
# Auto Merge on Approval Workflow
#
# Purpose: Automatically merge PRs once all approval and CI conditions pass.
#
# Triggers: Pull‑request events, review submissions, and completed checks.
#
# Maintainer: @mrz1836
#
# Rules for Auto‑Merge:
# • ≥1 approval review
# • No requested reviewers remaining
# • No "Changes Requested" reviews
# • All required status checks pass:
# - test (1.18.x, ubuntu-latest) (min version)
# - test (1.23.x, ubuntu-latest) (-1 version)
# - test (1.24.x, ubuntu-latest) (latest version)
# - Analyze (go)
# • Title must not contain "WIP"
# • PR must not have the "work-in-progress" label
# • PR must not be a draft
# ------------------------------------------------------------------------------
name: auto-merge-on-approval
on:
pull_request:
types:
[opened, synchronize, reopened, ready_for_review, labeled, unlabeled, edited]
pull_request_review:
types: [submitted]
check_suite:
types: [completed]
status: {}
# Cancel older runs of the same PR if a new commit is pushed
concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
cancel-in-progress: true
permissions:
contents: read
pull-requests: read
jobs:
automerge:
permissions:
pull-requests: write
runs-on: ubuntu-latest
steps:
- name: Attempt auto‑merge
uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
// core and github are already available in github-script
// ------------------------------------------------------------------
// Locate the pull request (handles pull_request, check_suite, status)
// ------------------------------------------------------------------
const owner = context.payload.repository.owner.login;
const repo = context.payload.repository.name;
let pr = context.payload.pull_request;
if (!pr) {
// check_suite/status → find PR by HEAD SHA
const sha = context.payload.check_suite?.head_sha || context.payload.sha;
if (sha) {
const { data: prs } = await github.rest.pulls.list({
owner,
repo,
head: `${owner}:${sha}`,
state: 'open',
});
pr = prs[0];
}
}
if (!pr) {
core.info('PR not found (may be closed), skipping.');
return;
}
// ------------------------------------------------------------------
// Skip PRs authored by bots (Dependabot / Codecov / etc.)
// ------------------------------------------------------------------
//const botLogins = [
// 'dependabot[bot]',
// 'dependabot-preview[bot]',
// 'codecov[bot]',
//];
//if (botLogins.includes(pr.user.login)) {
// core.info(`Skipping auto‑merge for bot‑authored PR (#${pr.number})`);
// return;
//}
// ------------------------------------------------------------------
// Gather PR metadata
// ------------------------------------------------------------------
const prNumber = pr.number;
const title = pr.title || '';
const labels = pr.labels.map(l => l.name);
const isDraft = pr.draft;
// Reviews
const { data: reviews } = await github.rest.pulls.listReviews({
owner,
repo,
pull_number: prNumber,
});
const approvals = reviews.filter(r => r.state === 'APPROVED').length;
const changesRequested = reviews.filter(r => r.state === 'CHANGES_REQUESTED').length;
// Requested reviewers
const requestedReviewers = pr.requested_reviewers || [];
// Required checks
const requiredChecks = [
'test (1.18.x, ubuntu-latest)',
'test (1.23.x, ubuntu-latest)',
'test (1.24.x, ubuntu-latest)',
'Analyze (go)',
];
const sha = pr.head.sha;
const { data: checks } = await github.rest.checks.listForRef({
owner,
repo,
ref: sha,
});
const checkRuns = checks.check_runs || [];
const checksPass = requiredChecks.every(name => {
const run = checkRuns.find(c => c.name === name);
return run && run.conclusion === 'success';
});
// WIP indicators
const titleHasWip = /wip/i.test(title);
const hasWipLabel = labels.includes('work-in-progress');
// ------------------------------------------------------------------
// Merge or explain why not
// ------------------------------------------------------------------
if (
approvals >= 1 &&
requestedReviewers.length === 0 &&
changesRequested === 0 &&
checksPass &&
!titleHasWip &&
!hasWipLabel &&
!isDraft
) {
try {
await github.rest.pulls.merge({
owner,
repo,
pull_number: prNumber,
merge_method: 'merge',
});
console.log(`✅ Pull request #${prNumber} merged.`);
} catch (error) {
core.setFailed(`❌ Failed to merge PR #${prNumber}: ${error.message}`);
}
} else {
if (approvals < 1) console.log('⏭ Less than 1 approval.');
if (requestedReviewers.length)
console.log('⏭ Still has requested reviewers.');
if (changesRequested) console.log('⏭ "Changes Requested" reviews present.');
if (!checksPass) {
const failed = requiredChecks.filter(name => {
const run = checkRuns.find(c => c.name === name);
return !(run && run.conclusion === 'success');
});
console.log(`⏭ Required checks failed: ${failed.join(', ')}`);
}
if (titleHasWip) console.log('⏭ Title contains "WIP".');
if (hasWipLabel) console.log('⏭ Has "work-in-progress" label.');
if (isDraft) console.log('⏭ PR is a draft.');
}