From e713a054eeaf5d7f3ab839e346c5bb4ebf0aab79 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sun, 12 Apr 2026 00:37:56 +0000 Subject: [PATCH 1/2] chore: add cleanup-branches workflow for merged PR branch cleanup Agent-Logs-Url: https://github.com/CodingThrust/problem-reductions/sessions/07ab69c2-2292-48ea-883c-4d32a80e80af Co-authored-by: GiggleLiu <6257240+GiggleLiu@users.noreply.github.com> --- .github/workflows/cleanup-branches.yml | 117 +++++++++++++++++++++++++ 1 file changed, 117 insertions(+) create mode 100644 .github/workflows/cleanup-branches.yml diff --git a/.github/workflows/cleanup-branches.yml b/.github/workflows/cleanup-branches.yml new file mode 100644 index 000000000..842699c3d --- /dev/null +++ b/.github/workflows/cleanup-branches.yml @@ -0,0 +1,117 @@ +name: Cleanup Merged PR Branches + +on: + # Auto-delete head branch whenever a PR is merged + pull_request: + types: [closed] + # Manual trigger to bulk-delete all existing merged-PR branches + workflow_dispatch: + +permissions: + contents: write + +jobs: + cleanup-on-merge: + if: github.event_name == 'pull_request' && github.event.pull_request.merged == true + name: Delete branch on PR merge + runs-on: ubuntu-latest + steps: + - name: Delete head branch + uses: actions/github-script@v7 + with: + github-token: ${{ secrets.GITHUB_TOKEN }} + script: | + const branch = context.payload.pull_request.head.ref; + const owner = context.repo.owner; + const repo = context.repo.repo; + + // Never delete protected branches + const protectedBranches = ['main', 'develop', 'master']; + if (protectedBranches.includes(branch)) { + console.log(`Skipping protected branch: ${branch}`); + return; + } + + try { + await github.rest.git.deleteRef({ + owner, + repo, + ref: `heads/${branch}`, + }); + console.log(`Deleted branch: ${branch}`); + } catch (err) { + if (err.status === 422) { + console.log(`Branch already deleted: ${branch}`); + } else { + throw err; + } + } + + bulk-cleanup: + if: github.event_name == 'workflow_dispatch' + name: Bulk delete all merged-PR branches + runs-on: ubuntu-latest + steps: + - name: Delete branches for merged PRs + uses: actions/github-script@v7 + with: + github-token: ${{ secrets.GITHUB_TOKEN }} + script: | + const owner = context.repo.owner; + const repo = context.repo.repo; + + const protectedBranches = new Set(['main', 'develop', 'master']); + + // Collect all head branches from merged PRs (paginate through all pages) + const mergedBranches = new Set(); + for await (const response of github.paginate.iterator( + github.rest.pulls.list, + { owner, repo, state: 'closed', per_page: 100 } + )) { + for (const pr of response.data) { + if (pr.merged_at) { + mergedBranches.add(pr.head.ref); + } + } + } + + // Fetch all current branches + const existingBranches = new Set(); + for await (const response of github.paginate.iterator( + github.rest.repos.listBranches, + { owner, repo, per_page: 100 } + )) { + for (const branch of response.data) { + existingBranches.add(branch.name); + } + } + + // Delete branches that are in merged PRs and still exist + const toDelete = [...mergedBranches].filter( + b => existingBranches.has(b) && !protectedBranches.has(b) + ); + + console.log(`Found ${toDelete.length} branches to delete.`); + let deleted = 0; + let failed = 0; + + for (const branch of toDelete) { + try { + await github.rest.git.deleteRef({ + owner, + repo, + ref: `heads/${branch}`, + }); + console.log(`Deleted: ${branch}`); + deleted++; + } catch (err) { + if (err.status === 422) { + console.log(`Already gone: ${branch}`); + } else { + console.error(`Failed to delete ${branch}: ${err.message}`); + failed++; + } + } + } + + console.log(`Done. Deleted: ${deleted}, Failed: ${failed}`); From bf49b446c4b41530a4b938d51aff7c1a0777a269 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sun, 12 Apr 2026 00:38:29 +0000 Subject: [PATCH 2/2] fix: fail workflow when branch deletions fail Agent-Logs-Url: https://github.com/CodingThrust/problem-reductions/sessions/07ab69c2-2292-48ea-883c-4d32a80e80af Co-authored-by: GiggleLiu <6257240+GiggleLiu@users.noreply.github.com> --- .github/workflows/cleanup-branches.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.github/workflows/cleanup-branches.yml b/.github/workflows/cleanup-branches.yml index 842699c3d..5d05aca1d 100644 --- a/.github/workflows/cleanup-branches.yml +++ b/.github/workflows/cleanup-branches.yml @@ -115,3 +115,6 @@ jobs: } console.log(`Done. Deleted: ${deleted}, Failed: ${failed}`); + if (failed > 0) { + throw new Error(`Failed to delete ${failed} branch(es). See logs above.`); + }