File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ name : Delete Outdated PR Branches
2+
3+ on :
4+ schedule :
5+ - cron : " 0 9 * * 1" # Every Monday at 9:00 UTC
6+ workflow_dispatch : # On-demand
7+
8+ permissions :
9+ contents : write
10+ pull-requests : read
11+
12+ jobs :
13+ delete-outdated-pr-branches :
14+ runs-on : ubuntu-latest
15+ timeout-minutes : 15
16+ steps :
17+ - uses : actions/checkout@v6
18+ with :
19+ fetch-depth : 0
20+ - name : Delete branches for closed/merged PRs
21+ env :
22+ GH_TOKEN : ${{ secrets.GITHUB_TOKEN }}
23+ run : |
24+ REPO="${{ github.repository }}"
25+ DELETED=0
26+ SKIPPED=0
27+
28+ # List all remote branches matching pull-request/<num>
29+ git fetch --prune origin
30+ for branch in $(git branch -r | grep -oP 'origin/pull-request/\K[0-9]+' | sort -un); do
31+ FULL_BRANCH="pull-request/${branch}"
32+ STATE=$(gh pr view "$branch" --repo "$REPO" --json state --jq '.state' 2>/dev/null || echo "")
33+
34+ if [ "$STATE" = "CLOSED" ] || [ "$STATE" = "MERGED" ]; then
35+ echo "Deleting branch '${FULL_BRANCH}' (PR #${branch} is ${STATE})"
36+ git push origin --delete "$FULL_BRANCH" && DELETED=$((DELETED + 1)) || true
37+ elif [ "$STATE" = "OPEN" ]; then
38+ echo "Skipping branch '${FULL_BRANCH}' (PR #${branch} is still OPEN)"
39+ SKIPPED=$((SKIPPED + 1))
40+ else
41+ echo "Skipping branch '${FULL_BRANCH}' (could not determine PR #${branch} state)"
42+ SKIPPED=$((SKIPPED + 1))
43+ fi
44+ done
45+
46+ echo ""
47+ echo "Done. Deleted: ${DELETED}, Skipped: ${SKIPPED}"
You can’t perform that action at this time.
0 commit comments