Cleanup release-plz branches #18
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Cleanup release-plz branches | |
| on: | |
| schedule: | |
| - cron: "23 3 * * *" | |
| workflow_dispatch: | |
| permissions: | |
| contents: write | |
| pull-requests: read | |
| jobs: | |
| cleanup: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Delete stale release-plz branches | |
| uses: actions/github-script@v8 | |
| with: | |
| script: | | |
| const branches = await github.paginate( | |
| github.rest.repos.listBranches, | |
| { owner: context.repo.owner, repo: context.repo.repo, per_page: 100 } | |
| ); | |
| const openPullRequests = await github.paginate( | |
| github.rest.pulls.list, | |
| { | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| state: "open", | |
| per_page: 100 | |
| } | |
| ); | |
| const activeBranches = new Set( | |
| openPullRequests | |
| .map(pr => pr.head?.ref) | |
| .filter(ref => ref?.startsWith("release-plz-")) | |
| ); | |
| const stale = branches | |
| .filter(b => b.name.startsWith("release-plz-")) | |
| .filter(b => !activeBranches.has(b.name)) | |
| .map(b => b.name); | |
| let deletedCount = 0; | |
| for (const branch of stale) { | |
| try { | |
| await github.rest.git.deleteRef({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| ref: `heads/${branch}` | |
| }); | |
| core.info(`Deleted: ${branch}`); | |
| deletedCount += 1; | |
| } catch (e) { | |
| if (e.status === 422) { | |
| core.info(`Skip ${branch} (already deleted or protected)`); | |
| } else { | |
| core.warning(`Failed to delete ${branch}: ${e.message}`); | |
| } | |
| } | |
| } | |
| core.info(`Cleaned ${deletedCount} of ${stale.length} release-plz branches`); |