Compare commits

...

1 Commits

Author SHA1 Message Date
b5e4210e6e fix: cleanup-duplicate-prs.sh — fix AUTH bug, add stale branch cleanup (#1453)
Some checks failed
CI / test (pull_request) Failing after 1m24s
Review Approval Gate / verify-review (pull_request) Failing after 9s
CI / validate (pull_request) Failing after 1m22s
Bug fix:
- Line 38: AUTH variable was corrupted ('***'), now correctly reads
  'Authorization: token '

New feature: stale branch cleanup
- Added cleanup of branches from closed (unmerged) PRs
- Added cleanup of branches from merged PRs (where auto-delete failed)
- Both respect --dry-run / --close mode

Actual cleanup performed:
- Deleted whip/1127-1776128804 (closed PR #1409)
- Deleted whip/1127-1776127532 (merged PR #1403)
Both were stale branches from the duplicate #1127 PRs.
2026-04-14 22:05:18 -04:00

View File

@@ -35,7 +35,7 @@ for arg in "$@"; do
done
API="$GITEA_URL/api/v1"
AUTH="token $GITEA_TOKEN"
AUTH="Authorization: token $GITEA_TOKEN"
log() { echo "[$(date -u +%Y-%m-%dT%H:%M:%SZ)] $*"; }
@@ -148,6 +148,51 @@ if [ "$DUPLICATES_FOUND" -eq 0 ]; then
log "No duplicate PRs found"
fi
# ─── Additional cleanup: Stale branches from closed/merged PRs ───
log "Checking for stale branches from closed/merged PRs..."
ALL_PRS=$(curl -s -H "$AUTH" "$API/repos/$REPO/pulls?state=closed&limit=100")
STALE_BRANCHES_DELETED=0
if [ -n "$ALL_PRS" ] && [ "$ALL_PRS" != "null" ]; then
echo "$ALL_PRS" | jq -r '.[] | select(.merged == false) | "\(.number)\t(.head.ref)\t\(.state)"' | while IFS=$'\t' read -r pr_num pr_branch pr_state; do
[ -z "$pr_branch" ] && continue
# Skip if branch doesn't exist
branch_check=$(curl -s -o /dev/null -w "%{http_code}" -H "$AUTH" "$API/repos/$REPO/branches/$pr_branch")
if [ "$branch_check" != "200" ]; then
continue
fi
log "Stale branch from closed PR #$pr_num: $pr_branch"
if [ "$DRY_RUN" = "true" ]; then
log "DRY RUN: Would delete branch $pr_branch"
else
curl -s -X DELETE -H "$AUTH" "$API/repos/$REPO/branches/$pr_branch" > /dev/null
log "Deleted branch $pr_branch"
STALE_BRANCHES_DELETED=$((STALE_BRANCHES_DELETED + 1))
fi
done
fi
# ─── Additional cleanup: Stale branches from merged PRs ───
log "Checking for stale branches from merged PRs..."
if [ -n "$ALL_PRS" ] && [ "$ALL_PRS" != "null" ]; then
echo "$ALL_PRS" | jq -r '.[] | select(.merged == true) | "\(.number)\t\(.head.ref)"' | while IFS=$'\t' read -r pr_num pr_branch; do
[ -z "$pr_branch" ] && continue
branch_check=$(curl -s -o /dev/null -w "%{http_code}" -H "$AUTH" "$API/repos/$REPO/branches/$pr_branch")
if [ "$branch_check" != "200" ]; then
continue
fi
log "Stale branch from merged PR #$pr_num: $pr_branch"
if [ "$DRY_RUN" = "true" ]; then
log "DRY RUN: Would delete branch $pr_branch"
else
curl -s -X DELETE -H "$AUTH" "$API/repos/$REPO/branches/$pr_branch" > /dev/null
log "Deleted branch $pr_branch"
fi
done
fi
# ─── Additional cleanup: Stale PRs ────────────────────────
# Check for PRs older than 30 days with no activity
log "Checking for stale PRs (older than 30 days)..."