From 167ecf02b1aa427f898a117ab22b1c9dfe4cb6f4 Mon Sep 17 00:00:00 2001 From: "CanbiZ (MickLesk)" <47820557+MickLesk@users.noreply.github.com> Date: Tue, 20 Jan 2026 08:43:21 +0100 Subject: [PATCH] cutoff --- .github/workflows/clear_old_branches.yml | 43 +++++++++++++----------- 1 file changed, 23 insertions(+), 20 deletions(-) diff --git a/.github/workflows/clear_old_branches.yml b/.github/workflows/clear_old_branches.yml index 5e68a9f26..514cd3c22 100644 --- a/.github/workflows/clear_old_branches.yml +++ b/.github/workflows/clear_old_branches.yml @@ -24,36 +24,39 @@ jobs: set -euo pipefail CUTOFF_DATE=$(date -u -d "7 days ago" +%s) - echo "Cutoff timestamp: $CUTOFF_DATE" - gh pr list \ + # Get all existing branches (except protected ones) + echo "Fetching existing branches..." + EXISTING_BRANCHES=$(gh api repos/${{ github.repository }}/git/matching-refs/heads \ + --jq '.[].ref | ltrimstr("refs/heads/")' | grep -vE '^(main|master|develop)$' || true) + + if [ -z "$EXISTING_BRANCHES" ]; then + echo "No branches to check." + exit 0 + fi + + echo "Found $(echo "$EXISTING_BRANCHES" | wc -l) branches to check." + + # Get branches from old merged/closed PRs + echo "Fetching closed/merged PRs older than 7 days..." + OLD_PR_BRANCHES=$(gh pr list \ --state all \ --base main \ - --json number,state,mergedAt,closedAt,headRefName \ + --json state,mergedAt,closedAt,headRefName \ --limit 500 | jq -r --arg cutoff "$CUTOFF_DATE" '.[] | select(.state == "MERGED" or .state == "CLOSED") | select(((.mergedAt // .closedAt) | fromdateiso8601) < ($cutoff | tonumber)) | - .headRefName' | - sort -u | - while read -r branch; do - # Schutz - case "$branch" in - main|master|develop) - echo "Skipping protected branch: $branch" - continue - ;; - esac + .headRefName' | sort -u) - echo "Checking if branch still exists: $branch" - if gh api -X GET repos/${{ github.repository }}/git/refs/heads/$branch 2>/dev/null; then + # Delete only branches that exist AND are from old PRs + for branch in $EXISTING_BRANCHES; do + if echo "$OLD_PR_BRANCHES" | grep -qx "$branch"; then echo "Deleting branch: $branch" - gh api \ - -X DELETE \ - repos/${{ github.repository }}/git/refs/heads/$branch \ + gh api -X DELETE repos/${{ github.repository }}/git/refs/heads/$branch \ || echo "Failed to delete branch $branch" - else - echo "Branch $branch does not exist (already deleted)" fi done + + echo "Cleanup complete."