Skip to content

Commit 2cc7163

Browse files
committed
Add auto deletion of 1 week+ old branches
1 parent 2ce7bc4 commit 2cc7163

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

.github/workflows/close-stale-ci-prs.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,29 @@ jobs:
3939
gh pr close $pr_number
4040
fi
4141
done <<< "$prs"
42+
43+
- name: Delete stale branches
44+
env:
45+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
46+
run: |
47+
# Get all branches with patchwork-ci- prefix
48+
branches=$(gh api repos/${{ github.repository }}/branches --jq '.[] | select(.name | startswith("patchwork-ci-")) | "\(.name) \(.commit.commit.committer.date)"')
49+
50+
while IFS= read -r line; do
51+
if [ -z "$line" ]; then
52+
continue
53+
fi
54+
55+
branch_name=$(echo "$line" | awk '{print $1}')
56+
created_at=$(echo "$line" | awk '{print $2}')
57+
58+
# Convert to Unix timestamp
59+
created_ts=$(date -d "$created_at" +%s)
60+
current_ts=$(date +%s)
61+
age_days=$(( (current_ts - created_ts) / 86400 ))
62+
63+
if [ $age_days -gt 7 ]; then
64+
echo "Deleting branch $branch_name (age: $age_days days)"
65+
gh api -X DELETE repos/${{ github.repository }}/git/refs/heads/$branch_name
66+
fi
67+
done <<< "$branches"

0 commit comments

Comments
 (0)