forked from dotnet/try
-
Notifications
You must be signed in to change notification settings - Fork 1
269 lines (243 loc) · 13.1 KB
/
dependabot-major-merge.yml
File metadata and controls
269 lines (243 loc) · 13.1 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
# Dependabot major-version merge executor
#
# This is a companion to the AI review workflow. It is triggered in two ways:
# 1. workflow_run: when the AI reviewer completes — labels added by GITHUB_TOKEN
# do NOT fire pull_request:labeled events (GitHub security restriction), so
# we listen for the reviewer workflow completing and scan for labeled PRs.
# 2. workflow_dispatch: for manual one-off triggering by a maintainer.
# 3. pull_request:labeled: kept for future cases where a PAT is used for labeling.
#
# DEFENSE-IN-DEPTH: This workflow does NOT blindly trust the label. Before
# enabling auto-merge it re-verifies every safety condition:
# 1. The PR was opened by dependabot[bot].
# 2. The PR has the "ai-approved-major-update" label.
# 3. ALL CI workflow runs on the PR branch have passed (latest per workflow).
# If any condition fails, the workflow skips/exits without merging.
#
# NOTE: The existing dependabot-auto-merge.yml already unconditionally
# approves all dependabot PRs, so by the time this workflow runs the PR
# already has an approval. This workflow only needs to enable auto-merge.
name: Dependabot major-version auto-merge
on:
workflow_run:
workflows: ["Dependabot Major Version Reviewer"]
types: [completed]
pull_request:
types: [labeled]
workflow_dispatch:
inputs:
pr_number:
description: 'PR number(s) to enable auto-merge on (comma-separated, must have ai-approved-major-update label)'
required: true
type: string
permissions:
contents: write
pull-requests: write
jobs:
auto-merge-major:
runs-on: ubuntu-latest
if: >-
(github.event_name == 'workflow_run' && github.event.workflow_run.conclusion == 'success')
|| github.event_name == 'workflow_dispatch'
|| (github.event.label.name == 'ai-approved-major-update'
&& github.event.pull_request.user.login == 'dependabot[bot]'
&& github.repository == 'IntelliTect/try')
steps:
# ── workflow_run: loop over all labeled Dependabot PRs ──────────
# Labels added by GITHUB_TOKEN never fire pull_request:labeled events.
# Instead we trigger on reviewer completion and scan for labeled PRs.
- name: Process all labeled PRs (workflow_run)
if: github.event_name == 'workflow_run'
env:
GH_TOKEN: ${{ secrets.GH_PAT_WORKFLOW }}
run: |
echo "Fetching open Dependabot PRs with ai-approved-major-update label..."
PR_NUMBERS=$(gh api "/repos/${{ github.repository }}/pulls?state=open&per_page=100" \
--jq '[.[] | select(.user.login == "dependabot[bot]") | select(any(.labels[]; .name == "ai-approved-major-update")) | .number] | .[]')
if [ -z "$PR_NUMBERS" ]; then
echo "No labeled Dependabot PRs found — nothing to do."
exit 0
fi
for PR_NUMBER in $PR_NUMBERS; do
echo ""
echo "════════════════════════════════════════"
echo "Processing PR #$PR_NUMBER..."
PR_DATA=$(gh api "/repos/${{ github.repository }}/pulls/$PR_NUMBER")
PR_URL=$(echo "$PR_DATA" | jq -r '.html_url')
PR_BRANCH=$(echo "$PR_DATA" | jq -r '.head.ref')
PR_AUTHOR=$(echo "$PR_DATA" | jq -r '.user.login')
if [ "$PR_AUTHOR" != "dependabot[bot]" ]; then
echo "::warning::PR #$PR_NUMBER author is '$PR_AUTHOR' — skipping."
continue
fi
echo "Checking CI for branch: $PR_BRANCH"
ALL_RUNS=$(gh api "/repos/${{ github.repository }}/actions/runs?branch=$(python3 -c "import urllib.parse,sys; print(urllib.parse.quote(sys.argv[1]))" "$PR_BRANCH")&per_page=100" \
--jq '[.workflow_runs[] | select(.name != "Dependabot major-version auto-merge")]')
TOTAL=$(echo "$ALL_RUNS" | jq 'length')
if [ "$TOTAL" -eq 0 ]; then
echo "::warning::PR #$PR_NUMBER has no CI workflow runs — skipping."
continue
fi
# Latest run per workflow (highest run_number)
LATEST_RUNS=$(echo "$ALL_RUNS" | jq '[group_by(.name)[] | sort_by(.run_number) | last]')
PENDING=$(echo "$LATEST_RUNS" | jq '[.[] | select(.status != "completed")] | length')
FAILED=$(echo "$LATEST_RUNS" | jq '[.[] | select(.status == "completed" and .conclusion != "success" and .conclusion != "skipped")] | length')
if [ "$PENDING" -gt 0 ]; then
echo "::warning::PR #$PR_NUMBER has $PENDING pending CI run(s) — skipping."
continue
fi
if [ "$FAILED" -gt 0 ]; then
echo "::warning::PR #$PR_NUMBER has $FAILED failed CI run(s) — skipping."
echo "$LATEST_RUNS" | jq '[.[] | select(.conclusion != "success" and .conclusion != "skipped")] | .[] | {name, conclusion}'
continue
fi
echo "✅ CI passed for PR #$PR_NUMBER — enabling auto-merge..."
MERGE_OUTPUT=$(gh pr merge --auto --squash "$PR_URL" 2>&1) && \
echo "✅ Auto-merge enabled for PR #$PR_NUMBER." || \
{
if echo "$MERGE_OUTPUT" | grep -q "clean status"; then
echo "Branch protection has no required checks — merging directly..."
gh pr merge --squash --delete-branch "$PR_URL"
echo "✅ PR #$PR_NUMBER merged directly."
else
echo "::error::Failed to merge PR #$PR_NUMBER: $MERGE_OUTPUT"
exit 1
fi
}
done
# ── pull_request / workflow_dispatch: single or multiple PRs ──────
- name: Resolve PR metadata
id: resolve
if: github.event_name != 'workflow_run'
env:
GH_TOKEN: ${{ secrets.GH_PAT_WORKFLOW }}
run: |
if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then
# Loop over comma-separated PR numbers, handling each fully inline.
IFS=',' read -ra PR_LIST <<< "${{ inputs.pr_number }}"
for PR_NUMBER in "${PR_LIST[@]}"; do
PR_NUMBER=$(echo "$PR_NUMBER" | tr -d ' ')
echo ""
echo "════════════════════════════════════════"
echo "Processing PR #$PR_NUMBER..."
PR_DATA=$(gh api "/repos/${{ github.repository }}/pulls/$PR_NUMBER")
PR_URL=$(echo "$PR_DATA" | jq -r '.html_url')
PR_AUTHOR=$(echo "$PR_DATA" | jq -r '.user.login')
PR_BRANCH=$(echo "$PR_DATA" | jq -r '.head.ref')
HAS_LABEL=$(echo "$PR_DATA" | jq '[.labels[].name] | contains(["ai-approved-major-update"])')
if [ "$PR_AUTHOR" != "dependabot[bot]" ]; then
echo "::error::PR #$PR_NUMBER author is '$PR_AUTHOR', not dependabot[bot] — skipping."
continue
fi
if [ "$HAS_LABEL" != "true" ]; then
echo "::error::PR #$PR_NUMBER does not have the 'ai-approved-major-update' label — skipping."
continue
fi
echo "Checking CI for branch: $PR_BRANCH"
ENCODED_BRANCH=$(python3 -c "import urllib.parse,sys; print(urllib.parse.quote(sys.argv[1]))" "$PR_BRANCH")
ALL_RUNS=$(gh api "/repos/${{ github.repository }}/actions/runs?branch=$ENCODED_BRANCH&per_page=100" \
--jq '[.workflow_runs[] | select(.name != "Dependabot major-version auto-merge")]')
TOTAL=$(echo "$ALL_RUNS" | jq 'length')
if [ "$TOTAL" -eq 0 ]; then
echo "::warning::PR #$PR_NUMBER has no CI workflow runs — skipping."
continue
fi
LATEST_RUNS=$(echo "$ALL_RUNS" | jq '[group_by(.name)[] | sort_by(.run_number) | last]')
PENDING=$(echo "$LATEST_RUNS" | jq '[.[] | select(.status != "completed")] | length')
FAILED=$(echo "$LATEST_RUNS" | jq '[.[] | select(.status == "completed" and .conclusion != "success" and .conclusion != "skipped")] | length')
if [ "$PENDING" -gt 0 ]; then
echo "::warning::PR #$PR_NUMBER has $PENDING pending CI run(s) — skipping."
continue
fi
if [ "$FAILED" -gt 0 ]; then
echo "::warning::PR #$PR_NUMBER has $FAILED failed CI run(s) — skipping."
echo "$LATEST_RUNS" | jq '[.[] | select(.conclusion != "success" and .conclusion != "skipped")] | .[] | {name, conclusion}'
continue
fi
echo "✅ CI passed for PR #$PR_NUMBER — merging..."
MERGE_OUTPUT=$(gh pr merge --auto --squash "$PR_URL" 2>&1) && \
echo "✅ Auto-merge enabled for PR #$PR_NUMBER." || \
{
if echo "$MERGE_OUTPUT" | grep -q "clean status"; then
echo "Branch protection has no required checks — merging directly..."
gh pr merge --squash --delete-branch "$PR_URL"
echo "✅ PR #$PR_NUMBER merged directly."
else
echo "::error::Failed to merge PR #$PR_NUMBER: $MERGE_OUTPUT"
exit 1
fi
}
done
else
PR_URL="${{ github.event.pull_request.html_url }}"
PR_BRANCH="${{ github.event.pull_request.head.ref }}"
echo "pr_url=$PR_URL" >> "$GITHUB_OUTPUT"
echo "pr_branch=$PR_BRANCH" >> "$GITHUB_OUTPUT"
fi
# ── 1. Fetch dependabot PR metadata (label trigger only) ────────
- name: Dependabot metadata
id: metadata
if: github.event_name == 'pull_request'
uses: dependabot/fetch-metadata@v2
with:
github-token: "${{ secrets.GITHUB_TOKEN }}"
# ── 2. Verify semver-major or multi-package update type ─────────
- name: Verify update type
if: github.event_name == 'pull_request'
run: |
UPDATE_TYPE="${{ steps.metadata.outputs.update-type }}"
if [ "$UPDATE_TYPE" != "version-update:semver-major" ] && [ -n "$UPDATE_TYPE" ]; then
echo "::error::PR update-type is '$UPDATE_TYPE' (not semver-major or multi-package) — refusing to merge."
exit 1
fi
echo "✅ Update type accepted: '${UPDATE_TYPE:-multi-package/unknown}'"
# ── 3. Verify CI passed (latest workflow run per workflow) ───────
- name: Verify all CI passed
if: github.event_name == 'pull_request'
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
PR_BRANCH: ${{ steps.resolve.outputs.pr_branch }}
run: |
ENCODED_BRANCH=$(python3 -c "import urllib.parse,sys; print(urllib.parse.quote(sys.argv[1]))" "$PR_BRANCH")
echo "Fetching workflow runs for branch: $PR_BRANCH"
ALL_RUNS=$(gh api \
"/repos/${{ github.repository }}/actions/runs?branch=$ENCODED_BRANCH&per_page=100" \
--jq '[.workflow_runs[] | select(.name != "Dependabot major-version auto-merge")]')
TOTAL=$(echo "$ALL_RUNS" | jq 'length')
echo "Found $TOTAL workflow run(s) for branch."
if [ "$TOTAL" -eq 0 ]; then
echo "::error::No CI workflow runs found for branch '$PR_BRANCH' — refusing to merge."
exit 1
fi
LATEST_RUNS=$(echo "$ALL_RUNS" | jq '[group_by(.name)[] | sort_by(.run_number) | last]')
PENDING=$(echo "$LATEST_RUNS" | jq '[.[] | select(.status != "completed")] | length')
FAILED=$(echo "$LATEST_RUNS" | jq '[.[] | select(.status == "completed" and .conclusion != "success" and .conclusion != "skipped")] | length')
if [ "$PENDING" -gt 0 ]; then
echo "::error::$PENDING CI run(s) still pending — refusing to merge."
exit 1
fi
if [ "$FAILED" -gt 0 ]; then
echo "::error::$FAILED CI run(s) failed — refusing to merge."
echo "$LATEST_RUNS" | jq '[.[] | select(.conclusion != "success" and .conclusion != "skipped")] | .[] | {name, conclusion}'
exit 1
fi
echo "✅ All CI runs passed."
# ── 4. Enable auto-merge ───────────────────────────────────────
- name: Enable auto-merge
if: github.event_name == 'pull_request'
env:
PR_URL: ${{ steps.resolve.outputs.pr_url }}
GH_TOKEN: ${{ secrets.GH_PAT_WORKFLOW }}
run: |
MERGE_OUTPUT=$(gh pr merge --auto --squash "$PR_URL" 2>&1) && \
echo "✅ Auto-merge enabled." || \
{
if echo "$MERGE_OUTPUT" | grep -q "clean status"; then
echo "Branch protection has no required checks — merging directly..."
gh pr merge --squash --delete-branch "$PR_URL"
echo "✅ PR merged directly."
else
echo "::error::Failed to merge PR: $MERGE_OUTPUT"
exit 1
fi
}