-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreview-bot-resource-drift-audit.yml
More file actions
365 lines (331 loc) · 15.2 KB
/
preview-bot-resource-drift-audit.yml
File metadata and controls
365 lines (331 loc) · 15.2 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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
name: Preview bot resource drift audit
"on":
schedule:
- cron: "17 3 * * 0"
workflow_dispatch:
inputs:
mode:
description: "Audit mode"
required: true
default: audit-and-cleanup
type: choice
options:
- audit-only
- audit-and-cleanup
permissions:
contents: read
jobs:
audit:
name: Detect and remediate preview bot drift
runs-on: ${{ fromJSON(vars.ACTIONS_RUNNER_LABELS || '["ubuntu-latest"]') }}
timeout-minutes: 15
steps:
- name: Checkout
uses: actions/checkout@v6
- name: Audit preview resources for bot branches
env:
AUDIT_MODE_INPUT: ${{ inputs.mode }}
GITHUB_EVENT_NAME: ${{ github.event_name }}
NEON_API_KEY: ${{ secrets.NEON_API_KEY }}
NEON_PROJECT_ID: ${{ vars.NEON_PROJECT_ID }}
VERCEL_PROJECT_ID: ${{ secrets.VERCEL_PROJECT_ID }}
VERCEL_TEAM_ID: ${{ secrets.VERCEL_TEAM_ID }}
VERCEL_TOKEN: ${{ secrets.VERCEL_TOKEN }}
run: |
set -euo pipefail
if ! command -v jq >/dev/null 2>&1; then
echo "::error::jq is required but not available on this runner."
exit 1
fi
missing=0
for var_name in NEON_API_KEY NEON_PROJECT_ID VERCEL_PROJECT_ID VERCEL_TOKEN; do
if [ -z "${!var_name:-}" ]; then
echo "::warning::Missing ${var_name}; skipping preview bot drift audit."
missing=1
fi
done
if [ "${missing}" -eq 1 ]; then
exit 0
fi
AUDIT_MODE="audit-and-cleanup"
if [ "${GITHUB_EVENT_NAME}" = "workflow_dispatch" ] && [ -n "${AUDIT_MODE_INPUT:-}" ]; then
AUDIT_MODE="${AUDIT_MODE_INPUT}"
fi
echo "Audit mode: ${AUDIT_MODE}"
VERCEL_API_BASE="https://api.vercel.com"
NEON_API_BASE="https://console.neon.tech/api/v2"
TEAM_QUERY=""
if [ -n "${VERCEL_TEAM_ID:-}" ]; then
TEAM_QUERY="teamId=${VERCEL_TEAM_ID}"
fi
api_call() {
local method="$1"
local base_url="$2"
local token="$3"
local path="$4"
local data="${5:-}"
local CONNECT_TIMEOUT="${CONNECT_TIMEOUT:-10}"
local MAX_TIME="${MAX_TIME:-60}"
local resp=""
local stderr_file=""
local curl_status=0
stderr_file="$(mktemp)"
if [ -n "${data}" ]; then
set +e
resp="$(curl -sS -X "${method}" "${base_url}${path}" \
--connect-timeout "${CONNECT_TIMEOUT}" \
--max-time "${MAX_TIME}" \
-H "Authorization: Bearer ${token}" \
-H "Accept: application/json" \
-H "Content-Type: application/json" \
-d "${data}" \
-w '\n%{http_code}' 2>"${stderr_file}")"
curl_status=$?
set -e
else
set +e
resp="$(curl -sS -X "${method}" "${base_url}${path}" \
--connect-timeout "${CONNECT_TIMEOUT}" \
--max-time "${MAX_TIME}" \
-H "Authorization: Bearer ${token}" \
-H "Accept: application/json" \
-w '\n%{http_code}' 2>"${stderr_file}")"
curl_status=$?
set -e
fi
if [ "${curl_status}" -ne 0 ]; then
if [ -s "${stderr_file}" ]; then
cat "${stderr_file}" >&2
fi
rm -f "${stderr_file}"
printf '\n'
printf '%s\n' "000"
return 0
fi
rm -f "${stderr_file}"
# Split resp into body + http code safely even when body contains newlines.
# curl appends the HTTP code as the last line via `-w '\n%{http_code}'`.
local body=""
local http_code=""
body="$(printf '%s' "${resp}" | head -n -1 || true)"
http_code="$(printf '%s' "${resp}" | tail -n 1)"
printf '%s\n' "${body}"
printf '%s\n' "${http_code}"
}
vercel_api() {
local method="$1"
local path="$2"
local data="${3:-}"
api_call "${method}" "${VERCEL_API_BASE}" "${VERCEL_TOKEN}" "${path}" "${data}"
}
neon_api() {
local method="$1"
local path="$2"
local data="${3:-}"
api_call "${method}" "${NEON_API_BASE}" "${NEON_API_KEY}" "${path}" "${data}"
}
#
# Vercel branch-scoped preview env vars (APP_BASE_URL) for bot branches
#
ENV_PATH="/v10/projects/${VERCEL_PROJECT_ID}/env?decrypt=false"
if [ -n "${TEAM_QUERY:-}" ]; then
ENV_PATH="${ENV_PATH}&${TEAM_QUERY}"
fi
ENV_RESP="$(vercel_api GET "${ENV_PATH}")"
ENV_HTTP_CODE="$(printf '%s' "${ENV_RESP}" | tail -n 1)"
ENV_JSON="$(printf '%s' "${ENV_RESP}" | sed '$d')"
if [ "${ENV_HTTP_CODE}" != "200" ]; then
echo "::error::Failed to list Vercel env vars (HTTP ${ENV_HTTP_CODE})."
exit 1
fi
BOT_ENV_IDS="$(printf '%s' "${ENV_JSON}" | jq -r '
(.envs // [])[]
| select(.key == "APP_BASE_URL")
| select((.target // []) | index("preview"))
| select((.gitBranch // "") | test("^(dependabot/|renovate/)"))
| .id
')"
env_found=0
env_deleted=0
env_failed=0
if [ -n "${BOT_ENV_IDS}" ]; then
env_found="$(printf '%s\n' "${BOT_ENV_IDS}" | sed '/^$/d' | wc -l | tr -d ' ')"
fi
if [ "${AUDIT_MODE}" = "audit-and-cleanup" ] && [ "${env_found}" -gt 0 ]; then
while IFS= read -r env_id; do
if [ -z "${env_id}" ] || [ "${env_id}" = "null" ]; then
continue
fi
DELETE_PATH="/v9/projects/${VERCEL_PROJECT_ID}/env/${env_id}"
if [ -n "${TEAM_QUERY:-}" ]; then
DELETE_PATH="${DELETE_PATH}?${TEAM_QUERY}"
fi
DELETE_RESP="$(vercel_api DELETE "${DELETE_PATH}")"
DELETE_HTTP_CODE="$(printf '%s' "${DELETE_RESP}" | tail -n 1)"
if [ "${DELETE_HTTP_CODE}" = "200" ] || [ "${DELETE_HTTP_CODE}" = "204" ]; then
env_deleted=$((env_deleted + 1))
else
env_failed=$((env_failed + 1))
echo "::warning::Failed deleting Vercel env id ${env_id} (HTTP ${DELETE_HTTP_CODE})."
fi
done < <(printf '%s\n' "${BOT_ENV_IDS}")
fi
#
# Vercel preview deployments for bot branches/authors (audit only)
#
BOT_DEPLOYMENTS="[]"
DEPLOY_UNTIL=""
NEXT_DEPLOY_UNTIL=""
MAX_DEPLOY_PAGES=50
deploy_page=1
while :; do
if [ "${deploy_page}" -gt "${MAX_DEPLOY_PAGES}" ]; then
echo "::warning::Vercel deployments pagination hit page cap (deploy_page=${deploy_page} >= MAX_DEPLOY_PAGES=${MAX_DEPLOY_PAGES}). Stopping pagination with DEPLOY_UNTIL='${DEPLOY_UNTIL}' NEXT_DEPLOY_UNTIL='${NEXT_DEPLOY_UNTIL}' and BOT_DEPLOYMENTS length $(printf '%s' "${BOT_DEPLOYMENTS}" | jq 'length')."
break
fi
DEPLOY_PATH="/v6/deployments?projectId=${VERCEL_PROJECT_ID}&target=preview&limit=100"
if [ -n "${DEPLOY_UNTIL}" ]; then
ENCODED_DEPLOY_UNTIL="$(printf '%s' "${DEPLOY_UNTIL}" | jq -sRr @uri)"
DEPLOY_PATH="${DEPLOY_PATH}&until=${ENCODED_DEPLOY_UNTIL}"
fi
if [ -n "${TEAM_QUERY:-}" ]; then
DEPLOY_PATH="${DEPLOY_PATH}&${TEAM_QUERY}"
fi
DEPLOY_RESP="$(vercel_api GET "${DEPLOY_PATH}")"
DEPLOY_HTTP_CODE="$(printf '%s' "${DEPLOY_RESP}" | tail -n 1)"
DEPLOY_JSON="$(printf '%s' "${DEPLOY_RESP}" | sed '$d')"
if [ "${DEPLOY_HTTP_CODE}" != "200" ]; then
echo "::error::Failed to list Vercel preview deployments (page ${deploy_page}, HTTP ${DEPLOY_HTTP_CODE})."
exit 1
fi
PAGE_BOT_DEPLOYMENTS="$(printf '%s' "${DEPLOY_JSON}" | jq -r '
[(.deployments // [])[] as $d
| ($d.meta.githubCommitRef // "") as $ref
| ($d.meta.githubCommitAuthorLogin // "") as $author
| ($d.creator.username // "") as $creator
| select(
($ref | test("^(refs/heads/)?(dependabot/|renovate/)"))
or ($author == "dependabot[bot]" or $author == "renovate[bot]")
or ($creator == "dependabot[bot]" or $creator == "renovate[bot]")
)
| {
id: ($d.uid // $d.id // ""),
ref: $ref,
author: $author,
creator: $creator,
url: ($d.url // "")
}
]
')"
BOT_DEPLOYMENTS="$(jq -cn \
--argjson all "${BOT_DEPLOYMENTS}" \
--argjson page "${PAGE_BOT_DEPLOYMENTS}" \
'$all + $page')"
NEXT_DEPLOY_UNTIL="$(printf '%s' "${DEPLOY_JSON}" | jq -r '.pagination.next // empty')"
if [ -z "${NEXT_DEPLOY_UNTIL}" ] || [ "${NEXT_DEPLOY_UNTIL}" = "null" ]; then
break
fi
if [ "${NEXT_DEPLOY_UNTIL}" = "${DEPLOY_UNTIL}" ]; then
echo "::warning::Vercel deployments pagination cursor did not advance (deploy_page=${deploy_page}, MAX_DEPLOY_PAGES=${MAX_DEPLOY_PAGES}, DEPLOY_UNTIL='${DEPLOY_UNTIL}', NEXT_DEPLOY_UNTIL='${NEXT_DEPLOY_UNTIL}'). Stopping pagination with BOT_DEPLOYMENTS length $(printf '%s' "${BOT_DEPLOYMENTS}" | jq 'length')."
break
fi
DEPLOY_UNTIL="${NEXT_DEPLOY_UNTIL}"
deploy_page=$((deploy_page + 1))
done
BOT_DEPLOYMENTS="$(printf '%s' "${BOT_DEPLOYMENTS}" | jq '
unique_by((.id // "") + "|" + (.ref // "") + "|" + (.url // ""))
')"
deploy_found="$(printf '%s' "${BOT_DEPLOYMENTS}" | jq 'length')"
if [ "${deploy_found}" -gt 0 ]; then
echo "::warning::Detected bot preview deployments. Manual cleanup may be required."
printf '%s' "${BOT_DEPLOYMENTS}" | jq -r '.[] | "- id=\(.id) ref=\(.ref) author=\(.author) creator=\(.creator) url=\(.url)"'
fi
#
# Neon preview branches for bot branches
#
BOT_NEON_BRANCHES=""
NEON_CURSOR=""
MAX_NEON_BRANCH_PAGES=50
neon_page=1
while :; do
if [ "${neon_page}" -gt "${MAX_NEON_BRANCH_PAGES}" ]; then
echo "::warning::Neon branches pagination hit page cap (neon_page=${neon_page} >= MAX_NEON_BRANCH_PAGES=${MAX_NEON_BRANCH_PAGES}). Stopping pagination with NEON_CURSOR='${NEON_CURSOR}'."
break
fi
BRANCHES_PATH="/projects/${NEON_PROJECT_ID}/branches?limit=100"
if [ -n "${NEON_CURSOR}" ]; then
ENCODED_NEON_CURSOR="$(printf '%s' "${NEON_CURSOR}" | jq -sRr @uri)"
BRANCHES_PATH="${BRANCHES_PATH}&cursor=${ENCODED_NEON_CURSOR}"
fi
BRANCHES_RESP="$(neon_api GET "${BRANCHES_PATH}")"
BRANCHES_HTTP_CODE="$(printf '%s' "${BRANCHES_RESP}" | tail -n 1)"
BRANCHES_JSON="$(printf '%s' "${BRANCHES_RESP}" | sed '$d')"
if [ "${BRANCHES_HTTP_CODE}" != "200" ]; then
echo "::error::Failed to list Neon branches (page ${neon_page}, HTTP ${BRANCHES_HTTP_CODE})."
exit 1
fi
PAGE_BOT_NEON_BRANCHES="$(printf '%s' "${BRANCHES_JSON}" | jq -r '
(.branches // [])[]
| select((.name // "") | test("^preview/(dependabot/|renovate/)"))
| "\(.id)\t\(.name)"
')"
if [ -n "${PAGE_BOT_NEON_BRANCHES}" ]; then
if [ -n "${BOT_NEON_BRANCHES}" ]; then
BOT_NEON_BRANCHES="${BOT_NEON_BRANCHES}"$'\n'"${PAGE_BOT_NEON_BRANCHES}"
else
BOT_NEON_BRANCHES="${PAGE_BOT_NEON_BRANCHES}"
fi
fi
NEXT_NEON_CURSOR="$(printf '%s' "${BRANCHES_JSON}" | jq -r '.pagination.next // empty')"
if [ -z "${NEXT_NEON_CURSOR}" ] || [ "${NEXT_NEON_CURSOR}" = "null" ]; then
break
fi
if [ "${NEXT_NEON_CURSOR}" = "${NEON_CURSOR}" ]; then
echo "::warning::Neon branches pagination cursor did not advance (neon_page=${neon_page}, MAX_NEON_BRANCH_PAGES=${MAX_NEON_BRANCH_PAGES}, NEON_CURSOR='${NEON_CURSOR}', NEXT_NEON_CURSOR='${NEXT_NEON_CURSOR}'); stopping pagination."
break
fi
NEON_CURSOR="${NEXT_NEON_CURSOR}"
neon_page=$((neon_page + 1))
done
if [ -n "${BOT_NEON_BRANCHES}" ]; then
BOT_NEON_BRANCHES="$(printf '%s\n' "${BOT_NEON_BRANCHES}" | sed '/^$/d' | sort -u)"
fi
neon_found=0
neon_deleted=0
neon_failed=0
if [ -n "${BOT_NEON_BRANCHES}" ]; then
neon_found="$(printf '%s\n' "${BOT_NEON_BRANCHES}" | sed '/^$/d' | wc -l | tr -d ' ')"
fi
if [ "${AUDIT_MODE}" = "audit-and-cleanup" ] && [ "${neon_found}" -gt 0 ]; then
while IFS=$'\t' read -r branch_id branch_name; do
if [ -z "${branch_id}" ] || [ "${branch_id}" = "null" ]; then
continue
fi
DELETE_RESP="$(neon_api DELETE "/projects/${NEON_PROJECT_ID}/branches/${branch_id}")"
DELETE_HTTP_CODE="$(printf '%s' "${DELETE_RESP}" | tail -n 1)"
if [ "${DELETE_HTTP_CODE}" = "200" ] || [ "${DELETE_HTTP_CODE}" = "202" ] || [ "${DELETE_HTTP_CODE}" = "204" ]; then
neon_deleted=$((neon_deleted + 1))
else
neon_failed=$((neon_failed + 1))
echo "::warning::Failed deleting Neon branch ${branch_name} (${branch_id}) (HTTP ${DELETE_HTTP_CODE})."
fi
done < <(printf '%s\n' "${BOT_NEON_BRANCHES}")
fi
unresolved_env="${env_found}"
unresolved_neon="${neon_found}"
if [ "${AUDIT_MODE}" = "audit-and-cleanup" ]; then
unresolved_env="${env_failed}"
unresolved_neon="${neon_failed}"
fi
# Deployments are audit-only (no reliable deletion API), so they should not
# force a permanent failure once any bot deployment has existed.
unresolved_total=$((unresolved_env + unresolved_neon))
echo "Summary:"
echo " Vercel bot APP_BASE_URL env vars: found=${env_found}, deleted=${env_deleted}, failed=${env_failed}"
echo " Vercel bot preview deployments: found=${deploy_found} (audit-only)"
echo " Neon bot preview branches: found=${neon_found}, deleted=${neon_deleted}, failed=${neon_failed}"
if [ "${unresolved_total}" -gt 0 ]; then
echo "::error::Preview resource drift remains (unresolved=${unresolved_total})."
exit 1
fi
echo "No unresolved bot preview resource drift detected."