63 lines
2.0 KiB
YAML
Generated
63 lines
2.0 KiB
YAML
Generated
name: Cleanup Branches (Merged or Closed)
|
|
|
|
on:
|
|
schedule:
|
|
- cron: "0 3 * * *" # daily at 03:00 UTC
|
|
workflow_dispatch:
|
|
|
|
permissions:
|
|
contents: write
|
|
pull-requests: read
|
|
|
|
jobs:
|
|
cleanup:
|
|
runs-on: ubuntu-latest
|
|
|
|
steps:
|
|
- name: Checkout repository
|
|
uses: actions/checkout@v4
|
|
|
|
- name: Delete merged or closed branches older than 7 days
|
|
env:
|
|
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
|
run: |
|
|
set -euo pipefail
|
|
|
|
CUTOFF_DATE=$(date -u -d "7 days ago" +%s)
|
|
echo "Cutoff timestamp: $CUTOFF_DATE"
|
|
|
|
# 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 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)
|
|
|
|
# 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 \
|
|
|| echo "Failed to delete branch $branch"
|
|
fi
|
|
done
|
|
|
|
echo "Cleanup complete."
|