Repository navigation
feat: daily automated branch cleanup workflow #11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| name: Branch Cleanup | ||
|
|
||
| on: | ||
| schedule: | ||
| - cron: '0 3 * * *' # daily at 03:00 UTC | ||
| workflow_dispatch: # allow manual trigger | ||
|
|
||
| jobs: | ||
| delete-merged-branches: | ||
| name: Delete merged branches | ||
| runs-on: ubuntu-latest | ||
| permissions: | ||
| contents: write | ||
|
|
||
| steps: | ||
| - name: Delete merged branches | ||
| uses: actions/github-script@v7 | ||
| with: | ||
| github-token: ${{ secrets.GITHUB_TOKEN }} | ||
| script: | | ||
| const protectedBranches = ['main', 'master', 'develop']; | ||
|
|
||
| // Resolve the repository's default branch dynamically | ||
| const { data: repo } = await github.rest.repos.get({ | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| }); | ||
| const defaultBranch = repo.default_branch; | ||
| core.info(`Default branch: ${defaultBranch}`); | ||
|
|
||
| // Paginate through all branches | ||
| const branches = await github.paginate(github.rest.repos.listBranches, { | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| per_page: 100, | ||
| }); | ||
|
|
||
| for (const branch of branches) { | ||
| if (protectedBranches.includes(branch.name)) { | ||
| core.info(`Skipping protected branch: ${branch.name}`); | ||
| continue; | ||
| } | ||
|
|
||
| // Check whether the branch has been fully merged into the default branch | ||
| let compare; | ||
| try { | ||
| ({ data: compare } = await github.rest.repos.compareCommitsWithBasehead({ | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| basehead: `${defaultBranch}...${branch.name}`, | ||
| })); | ||
| } catch (err) { | ||
| core.info(`Could not compare ${branch.name} – skipping (${err.message})`); | ||
| continue; | ||
| } | ||
|
|
||
| if (compare.ahead_by === 0) { | ||
| core.info(`Deleting merged branch: ${branch.name}`); | ||
| await github.rest.git.deleteRef({ | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| ref: `heads/${branch.name}`, | ||
| }); | ||
|
Comment on lines
+57
to
+63
|
||
| } else { | ||
| core.info(`Branch ${branch.name} has ${compare.ahead_by} unmerged commit(s) – skipping`); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The workflow can delete the repository’s default branch if it isn’t named one of
main|master|develop.repos.listBranchesincludes the default branch, and comparing${defaultBranch}...${branch.name}for the default branch yieldsahead_by === 0, triggeringdeleteRef. Add an explicit guard to always skipbranch.name === defaultBranch(or adddefaultBranchto the protected list after resolving it).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot apply changes based on this feedback