Skip to content

Commit 029593e

Browse files
[chore]: add delete-outdated-pr-branches GitHub Action (#1108)
### What does this PR do? Type of change: new feature Adds a scheduled GitHub Action that automatically deletes `pull-request/<num>` branches where the corresponding PR is no longer open (i.e., closed or merged). This prevents stale branches from accumulating over time. ### Testing The workflow can be triggered on-demand via `workflow_dispatch` for manual testing before relying on the weekly schedule. ### Before your PR is "*Ready for review*" - Is this change backward compatible?: N/A - If you copied code from any other sources or added a new PIP dependency, did you follow guidance in `CONTRIBUTING.md`: N/A - Did you write any new necessary tests?: N/A - Did you update [Changelog](https://github.com/NVIDIA/Model-Optimizer/blob/main/CHANGELOG.rst)?: N/A <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Chores** * Added automated workflow to periodically clean up branches associated with closed or merged pull requests, running weekly and available for manual trigger. <!-- end of auto-generated comment: release notes by coderabbit.ai --> Signed-off-by: Keval Morabia <28916987+kevalmorabia97@users.noreply.github.com>
1 parent d0bf0be commit 029593e

1 file changed

Lines changed: 47 additions & 0 deletions

File tree

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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}"

0 commit comments

Comments
 (0)