Repository navigation
334 lines (311 loc) · 14.5 KB
/
Copy pathmerge-queue-dequeue-report.yml
File metadata and controls
334 lines (311 loc) · 14.5 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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
# Copyright 2026 Collate
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# The queue ref is deleted on dequeue; its commit is not, so check runs are
# read from the commit rather than the branch.
# Must stay pull_request_target — pull_request gets a read-only token on fork
# PRs and cannot comment. Safe only while nothing checks out PR code.
#
# Conflict labels. GitHub builds each entry on main + every entry ahead and
# removes it with MERGE_CONFLICT if that fails, even when it applies cleanly to
# main and the entry it hit is about to fail. So a MERGE_CONFLICT dequeue is
# labelled by what it conflicts with: `merge conflict` (latest main — rebase)
# or `queue conflict` (only an entry ahead — re-queue once that one leaves).
# GitHub only builds the first few entries, so a QUEUED entry further back that
# starts conflicting with main would wait its turn and burn a full CI run
# before failing; evict-main-conflicts removes it on the merge that caused it.
# Both labels clear when the PR is pushed to or re-queued.
# ADR:2026-10-13-merge-queue-conflicts-are-labelled-and-main-conflicts-evicted
name: Merge Queue Dequeue Report
on:
pull_request_target:
types: [dequeued, enqueued, synchronize]
push:
branches: [main]
workflow_dispatch:
inputs:
pr:
description: PR number to report on
required: true
type: number
dequeue:
description: Which dequeue to report on, counted back from the latest (1)
type: number
default: 1
dry_run:
description: Print to the job summary instead of commenting on the PR
type: boolean
default: true
permissions:
actions: read
checks: read
contents: read
issues: write
pull-requests: write
jobs:
report:
name: Report dequeue cause
if: ${{ github.event_name == 'workflow_dispatch' || (github.event.action == 'dequeued' && !github.event.pull_request.merged) }}
runs-on: ubuntu-latest
env:
GH_TOKEN: ${{ github.token }}
GH_REPO: ${{ github.repository }}
OWNER: ${{ github.repository_owner }}
REPO: ${{ github.event.repository.name }}
PR: ${{ github.event.number || inputs.pr }}
DEQUEUE: ${{ inputs.dequeue || 1 }}
BASE_REF: ${{ github.event.repository.default_branch }}
DRY_RUN: ${{ inputs.dry_run == true || vars.MQ_REPORT_DRY_RUN == 'true' }}
SLACK_CHANNEL: C0AC5T013V1 # #ci-cleanup
steps:
# pull_request_target checks out the trusted base, never the PR head.
- uses: actions/checkout@v7
with:
persist-credentials: false
- name: Resolve the dequeue event
id: event
shell: bash
run: |
set -euo pipefail
# Only a first-ever dequeue can return an empty list; a repeat dequeue
# matches at once, and its createdAt in the comment exposes a stale read.
# shellcheck disable=SC2016 # the GraphQL query and jq filter must not expand
for _ in 1 2 3; do
gh api graphql -f owner="$OWNER" -f repo="$REPO" -F pr="$PR" -F last="$DEQUEUE" -f query='
query($owner: String!, $repo: String!, $pr: Int!, $last: Int!) {
repository(owner: $owner, name: $repo) {
pullRequest(number: $pr) {
timelineItems(last: $last, itemTypes: [REMOVED_FROM_MERGE_QUEUE_EVENT]) {
nodes {
... on RemovedFromMergeQueueEvent {
reason
createdAt
beforeCommit { oid }
}
}
}
}
}
}' --jq '.data.repository.pullRequest.timelineItems.nodes
| select(length == ($ENV.DEQUEUE | tonumber)) | .[0]
| select(.reason != null)
| "\(.reason)\t\(.beforeCommit.oid // "")\t\(.createdAt)"' > event.tsv || true
if [ -s event.tsv ]; then
case "$(cut -f1 event.tsv)" in
manual|MANUAL)
echo "Manual removal; skipping notifications."
exit 0
;;
esac
{
echo "reason=$(cut -f1 event.tsv)"
echo "sha=$(cut -f2 event.tsv)"
echo "created=$(cut -f3 event.tsv)"
} >> "$GITHUB_OUTPUT"
exit 0
fi
sleep 5
done
# A dispatch replaying an older dequeue must find it, never report another.
if [ "$DEQUEUE" != 1 ]; then
echo "::error::PR #$PR has no dequeue $DEQUEUE back from the latest (1-100)."
exit 1
fi
echo "No RemovedFromMergeQueueEvent found for PR #$PR — nothing to report."
- name: Label what the entry conflicts with
if: ${{ steps.event.outputs.reason == 'MERGE_CONFLICT' }}
# Labelling is extra; a failure here must not skip the comment and
# the #ci-cleanup alert that follow.
continue-on-error: true
shell: bash
run: |
set -euo pipefail
# GitHub computes mergeability against the base lazily: the first
# read can be UNKNOWN while it works it out in the background.
state=UNKNOWN
for _ in 1 2 3 4 5 6; do
# shellcheck disable=SC2016 # the GraphQL query must not expand
state=$(gh api graphql -f owner="$OWNER" -f repo="$REPO" -F pr="$PR" -f query='
query($owner: String!, $repo: String!, $pr: Int!) {
repository(owner: $owner, name: $repo) { pullRequest(number: $pr) { mergeable } }
}' --jq '.data.repository.pullRequest.mergeable')
[ "$state" != "UNKNOWN" ] && break
sleep 10
done
case "$state" in
CONFLICTING)
label="merge conflict"
echo "Conflicts with the latest \`$BASE_REF\` — rebase, then re-queue." > conflict.txt ;;
MERGEABLE)
label="queue conflict"
echo "Applies cleanly to the latest \`$BASE_REF\`; it conflicted only with an entry ahead in the queue. Re-queue once that entry has merged or left the queue." > conflict.txt ;;
*)
echo "Mergeability against $BASE_REF still UNKNOWN; leaving the PR unlabelled."
exit 0 ;;
esac
if [ "$DRY_RUN" = "true" ]; then
echo "Would label PR #$PR: $label" >> "$GITHUB_STEP_SUMMARY"
else
gh label create "merge conflict" --color B60205 --force \
--description "Conflicts with the latest main; removed from the merge queue"
gh label create "queue conflict" --color FBCA04 --force \
--description "Conflicted only with an entry ahead in the merge queue"
gh pr edit "$PR" --add-label "$label"
fi
- name: Comment the checks that caused the dequeue
if: ${{ steps.event.outputs.reason != '' }}
shell: bash
env:
REASON: ${{ steps.event.outputs.reason }}
SHA: ${{ steps.event.outputs.sha }}
CREATED: ${{ steps.event.outputs.created }}
run: |
set -euo pipefail
: > tests.txt
# The Playwright summary leaves its failed tests in one annotation rather
# than alerting itself (a failed run does not always dequeue its PR), so
# this report is the queue's only #ci-cleanup alert.
if [ -n "$SHA" ]; then
# shellcheck disable=SC2016 # the jq filter must not expand
gh api "repos/$GH_REPO/commits/$SHA/check-runs?check_name=playwright-summary" \
--jq '.check_runs[].id' | while read -r id; do
gh api "repos/$GH_REPO/check-runs/$id/annotations" \
--jq '.[] | select(.title == "Merge queue failed tests") | .message'
done | sed '/^$/d' | sort -u > tests.txt || true
fi
# Only reads GitHub; writes the PR comment and the #ci-cleanup payload.
python3 .github/scripts/merge_queue_dequeue.py \
--repo "$GH_REPO" --pr "$PR" --branch "$BASE_REF" \
--queue "merge queue" --reason "$REASON" \
--sha "$SHA" --created "$CREATED" --failed-tests tests.txt \
--channel "$SLACK_CHANNEL" --body body.md --slack slack.json
# PR comment only: the #ci-cleanup message stays as it was.
if [ -s conflict.txt ]; then
{ echo; cat conflict.txt; } >> body.md
fi
if [ "$DRY_RUN" = "true" ]; then
cat body.md >> "$GITHUB_STEP_SUMMARY"
else
gh pr comment "$PR" --body-file body.md
fi
# A dequeue holds up everyone behind it in the queue, so the channel gets
# the same report the PR does. Slack must never fail the report, hence
# continue-on-error; `errors: true` still surfaces the API error in the log.
- name: "Ping #ci-cleanup"
if: ${{ steps.event.outputs.reason != '' && env.DRY_RUN != 'true' }}
continue-on-error: true
uses: slackapi/slack-github-action@dcb1066f776dd043e64d0e8ba94ca15cc7e1875d # v4.0.0
with:
method: chat.postMessage
token: ${{ secrets.COLLATE_BOT_SLACK_TOKEN }}
# Keep on — the action discards Slack API errors by default.
errors: true
payload-file-path: slack.json
# Only reacts to PRs that carry a conflict label, so the frequent
# synchronize events skip without taking a runner.
clear-conflict-labels:
if: >-
${{ (github.event.action == 'enqueued' || github.event.action == 'synchronize')
&& (contains(github.event.pull_request.labels.*.name, 'merge conflict')
|| contains(github.event.pull_request.labels.*.name, 'queue conflict')) }}
runs-on: ubuntu-latest
timeout-minutes: 5
env:
GH_TOKEN: ${{ github.token }}
GH_REPO: ${{ github.repository }}
PR: ${{ github.event.number }}
steps:
- name: Clear conflict labels
shell: bash
run: |
set -euo pipefail
gh pr edit "$PR" --remove-label "merge conflict" --remove-label "queue conflict"
evict-main-conflicts:
if: ${{ github.event_name == 'push' }}
# A newer merge re-checks every entry anyway.
concurrency:
group: merge-queue-evict-main-conflicts
cancel-in-progress: true
runs-on: ubuntu-latest
timeout-minutes: 5
permissions:
contents: write
issues: write
pull-requests: write
env:
GH_TOKEN: ${{ github.token }}
GH_REPO: ${{ github.repository }}
OWNER: ${{ github.repository_owner }}
REPO: ${{ github.event.repository.name }}
BASE_REF: ${{ github.event.repository.default_branch }}
DRY_RUN: ${{ vars.MQ_REPORT_DRY_RUN == 'true' }}
MERGED_SHA: ${{ github.sha }}
steps:
- name: Remove queue entries that conflict with main
shell: bash
run: |
set -euo pipefail
# Only QUEUED entries: GitHub builds the rest on main + entries ahead
# and removes them itself on a conflict (it also reports them
# UNKNOWN while building). Mergeability is computed lazily after main
# moves; re-read until none is UNKNOWN, leave any still UNKNOWN for
# the next merge.
for _ in 1 2 3 4 5 6; do
# shellcheck disable=SC2016 # the GraphQL query must not expand
gh api graphql -f owner="$OWNER" -f repo="$REPO" -f branch="$BASE_REF" -f query='
query($owner: String!, $repo: String!, $branch: String!) {
repository(owner: $owner, name: $repo) {
mergeQueue(branch: $branch) {
entries(first: 100) {
nodes { position state pullRequest { id number mergeable } }
}
}
}
}' --jq '.data.repository.mergeQueue.entries.nodes[]
| select(.state == "QUEUED")
| [.pullRequest.id, .pullRequest.number, .pullRequest.mergeable, .position]
| @tsv' > entries.tsv
grep -q $'\tUNKNOWN\t' entries.tsv || break
sleep 10
done
echo "queued entries (id, pr, mergeable, position):"
cat entries.tsv
evicted=0
while IFS=$'\t' read -r id pr state position; do
[ "$state" = "CONFLICTING" ] || continue
evicted=$((evicted + 1))
echo "evict #$pr (position $position)"
if [ "$DRY_RUN" = "true" ]; then
continue
fi
# shellcheck disable=SC2016 # the GraphQL mutation must not expand
gh api graphql -f id="$id" -f query='
mutation($id: ID!) { dequeuePullRequest(input: {id: $id}) { clientMutationId } }' \
--silent || { echo "::warning::could not dequeue #$pr"; continue; }
# The dequeue already happened; a label or comment failure must not
# stop the loop before the remaining entries.
gh label create "merge conflict" --color B60205 --force \
--description "Conflicts with the latest main; removed from the merge queue" \
|| echo "::warning::could not create the merge conflict label"
gh pr edit "$pr" --add-label "merge conflict" \
|| echo "::warning::could not label #$pr"
gh pr comment "$pr" --body "### 🚦 Removed from the merge queue — conflicts with \`$BASE_REF\`
This PR (queue position $position) stopped merging cleanly into \`$BASE_REF\` at ${MERGED_SHA:0:7}, so it was removed before it was built. Rebase on \`$BASE_REF\`, then re-queue." \
|| echo "::warning::could not comment on #$pr"
done < entries.tsv
{
echo "### Merge-queue entries conflicting with \`$BASE_REF\`"
echo
echo "| | |"
echo "|---|---|"
echo "| dry run | \`${DRY_RUN}\` |"
echo "| queued entries checked | $(wc -l < entries.tsv | tr -d ' ') |"
echo "| evicted | ${evicted} |"
} >> "$GITHUB_STEP_SUMMARY"