Skip to content

Commit 3d05902

Browse files
committed
ci(retention): fail the run when a version cannot be deleted
A refused DELETE only logged a warning, so a token without delete rights left an apply run green with nothing removed. Move the delete into hack/retention-delete.sh, which reports each refusal as an error carrying the API status, still tries the remaining versions, and exits non-zero; the workflow turns that into a failed run after the sweep. Assisted-by: LLM Signed-off-by: Aleksei Sviridkin <[email protected]>
1 parent 36537db commit 3d05902

4 files changed

Lines changed: 111 additions & 25 deletions

File tree

‎.github/workflows/retention.yaml‎

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -210,10 +210,10 @@ jobs:
210210
# what keeps a candidate safe is the protected set, which no part of
211211
# this list feeds.
212212
#
213-
# A package whose versions cannot be read is likewise reported and
214-
# skipped so the rest is still swept, then fails the run at the end.
215-
# The loop is fed by a here-string rather than a pipe so the flag
216-
# survives it.
213+
# A package whose versions cannot be read, or a version that cannot
214+
# be deleted, is likewise reported and skipped so the rest is still
215+
# swept, then fails the run at the end. The loop is fed by a
216+
# here-string rather than a pipe so the flag survives it.
217217
sweep_failed=0
218218
gh_err="$(mktemp)"
219219
tree="$(mktemp -d)"
@@ -256,16 +256,7 @@ jobs:
256256
drop_ids="$(printf '%s' "$ids" | jq -r ".[${KEEP}:][]")"
257257
echo "── ${pkg}: ${total} nightlies, keep ${KEEP}, drop $((total - KEEP))"
258258
259-
printf '%s\n' "$drop_ids" | while IFS= read -r id; do
260-
[ -n "$id" ] || continue
261-
if [ "$APPLY" = "true" ]; then
262-
echo " ✗ deleting ${pkg} version ${id}"
263-
gh api -X DELETE "/orgs/${ORG}/packages/container/${enc}/versions/${id}" >/dev/null \
264-
|| echo "::warning::failed to delete ${pkg} version ${id} (permissions?)"
265-
else
266-
echo " • would delete ${pkg} version ${id}"
267-
fi
268-
done
259+
printf '%s\n' "$drop_ids" | xargs ./hack/retention-delete.sh "$pkg" version || sweep_failed=1
269260
done <<< "$pkgs"
270261
271262
# Anything unresolved above means the protected set is incomplete, so
@@ -301,18 +292,10 @@ jobs:
301292
}
302293
promotion_total="$(printf '%s' "$promotion_ids" | jq 'length')"
303294
echo "── ${pkg}: ${promotion_total} abandoned promotion candidate(s) older than ${PROMOTION_TTL_DAYS} days"
304-
printf '%s' "$promotion_ids" | jq -r '.[]' | while IFS= read -r id; do
305-
[ -n "$id" ] || continue
306-
if [ "$APPLY" = "true" ]; then
307-
echo " ✗ deleting ${pkg} promotion candidate version ${id}"
308-
gh api -X DELETE "/orgs/${ORG}/packages/container/${enc}/versions/${id}" >/dev/null \
309-
|| echo "::warning::failed to delete ${pkg} version ${id} (permissions?)"
310-
else
311-
echo " • would delete ${pkg} promotion candidate version ${id}"
312-
fi
313-
done
295+
printf '%s' "$promotion_ids" | jq -r '.[]' \
296+
| xargs ./hack/retention-delete.sh "$pkg" "promotion candidate version" || sweep_failed=1
314297
if [ "$sweep_failed" -eq 1 ]; then
315-
echo "::error::some packages could not be read and were not swept."
298+
echo "::error::some versions could not be read or deleted; see the errors above."
316299
exit 1
317300
fi
318301
echo "Done."

‎hack/promotion-retention-contract.bats‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,3 +171,16 @@ step_block() {
171171
printf '%s\n' "$handler" | grep -qF 'sweep_failed=1'
172172
printf '%s\n' "$handler" | grep -qF "promotion_ids='[]'"
173173
}
174+
175+
@test "a refused delete flags the run instead of aborting the sweep" {
176+
# Unflagged, a failed xargs would abort the step under `set -e`: still red,
177+
# but the remaining packages and the promotion sweep would never run.
178+
block="$(step_block 'Prune' "$RETENTION")"
179+
[ -n "$block" ]
180+
calls="$(printf '%s\n' "$block" | code_lines | grep -F 'retention-delete.sh' || true)"
181+
[ "$(printf '%s\n' "$calls" | grep -c .)" -ge 2 ]
182+
if printf '%s\n' "$calls" | grep -vqE '\|\| sweep_failed=1$'; then
183+
echo "FAIL: a delete call does not flag the run: $calls"
184+
false
185+
fi
186+
}

‎hack/retention-delete.sh‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/bin/sh
2+
# Delete GHCR container-package versions for the retention workflow.
3+
#
4+
# Usage: APPLY=true|false ORG=<org> retention-delete.sh <package> <label> <version-id>...
5+
#
6+
# <label> names what is deleted in the log ("version", "promotion candidate
7+
# version"). Without APPLY=true it only reports. A refused delete is an error,
8+
# not a warning: the remaining ids are still tried, then the exit is non-zero,
9+
# so a token that cannot delete turns the run red instead of leaving it green
10+
# with nothing removed.
11+
set -eu
12+
13+
PKG="${1:?usage: retention-delete.sh <package> <label> <version-id>...}"
14+
LABEL="${2:?usage: retention-delete.sh <package> <label> <version-id>...}"
15+
shift 2
16+
: "${ORG:?ORG is required}"
17+
18+
enc="$(printf '%s' "$PKG" | sed 's|/|%2F|g')"
19+
failed=0
20+
for id in "$@"; do
21+
if [ "${APPLY:-false}" != "true" ]; then
22+
echo " • would delete ${PKG} ${LABEL} ${id}"
23+
continue
24+
fi
25+
echo " ✗ deleting ${PKG} ${LABEL} ${id}"
26+
if ! err="$(gh api -X DELETE "/orgs/${ORG}/packages/container/${enc}/versions/${id}" 2>&1 >/dev/null)"; then
27+
echo "::error::failed to delete ${PKG} ${LABEL} ${id}: ${err}"
28+
failed=1
29+
fi
30+
done
31+
exit "$failed"

‎hack/retention-delete_test.bats‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#!/usr/bin/env bats
2+
# Tests for hack/retention-delete.sh — the delete half of the GHCR retention
3+
# workflow. A refused DELETE must fail the run: a sweep that only warns stays
4+
# green while deleting nothing, which is how retention went unnoticed before.
5+
#
6+
# Run with: bats hack/retention-delete_test.bats
7+
8+
load test_helper
9+
10+
SCRIPT="${BATS_TEST_DIRNAME}/retention-delete.sh"
11+
12+
# gh stub: records every call, refuses version 2 the way the API does.
13+
_stub_gh() {
14+
mkdir -p "$1"
15+
cat > "$1/gh" <<'EOF'
16+
#!/bin/sh
17+
printf '%s\n' "$*" >> "$GH_CALLS"
18+
case "$*" in
19+
*/versions/2*) echo 'gh: Forbidden (HTTP 403)' >&2; exit 1 ;;
20+
esac
21+
EOF
22+
chmod +x "$1/gh"
23+
}
24+
25+
setup() {
26+
strict_setup
27+
_stub_gh "${BATS_TEST_TMPDIR}/bin"
28+
export PATH="${BATS_TEST_TMPDIR}/bin:$PATH"
29+
export GH_CALLS="${BATS_TEST_TMPDIR}/calls"
30+
export ORG=cozystack
31+
: > "$GH_CALLS"
32+
}
33+
34+
@test "apply: a refused delete is an error naming the HTTP status, the rest still run, exit is non-zero" {
35+
run env APPLY=true "$SCRIPT" cozystack/foo version 1 2 3
36+
[ "$status" -ne 0 ]
37+
[ "$(wc -l < "$GH_CALLS" | tr -d ' ')" -eq 3 ]
38+
grep -q 'cozystack%2Ffoo/versions/3' "$GH_CALLS"
39+
case "$output" in
40+
*"::error::"*"version 2"*"HTTP 403"*) ;;
41+
*) echo "FAIL: no error with the HTTP status: $output"; false ;;
42+
esac
43+
}
44+
45+
@test "apply: all deletes succeeding exits zero" {
46+
run env APPLY=true "$SCRIPT" cozystack/foo version 1 3
47+
[ "$status" -eq 0 ]
48+
[ "$(wc -l < "$GH_CALLS" | tr -d ' ')" -eq 2 ]
49+
}
50+
51+
@test "dry-run calls nothing and exits zero" {
52+
run env APPLY=false "$SCRIPT" cozystack/foo version 1 2 3
53+
[ "$status" -eq 0 ]
54+
[ ! -s "$GH_CALLS" ]
55+
case "$output" in
56+
*"would delete cozystack/foo version 2"*) ;;
57+
*) echo "FAIL: dry-run did not report: $output"; false ;;
58+
esac
59+
}

0 commit comments

Comments
 (0)