diff --git a/.github/workflows/delete-merged-branches.yml b/.github/workflows/delete-merged-branches.yml new file mode 100644 index 000000000..779a62fdd --- /dev/null +++ b/.github/workflows/delete-merged-branches.yml @@ -0,0 +1,41 @@ +name: Delete Merged Branches + +on: + pull_request: + types: [closed] + +jobs: + delete-merged-branch: + if: github.event.pull_request.merged == true + runs-on: ubuntu-latest + + steps: + - name: Delete merged branch + uses: actions/github-script@v7 + with: + script: | + const { owner, repo } = context.repo; + const branchName = context.payload.pull_request.head.ref; + + // Skip if it's the default branch + if (branchName === 'main' || branchName === 'master') { + console.log(`Skipping deletion of protected branch: ${branchName}`); + return; + } + + // Skip if it's from a fork + if (context.payload.pull_request.head.repo.full_name !== context.payload.pull_request.base.repo.full_name) { + console.log(`Skipping deletion of branch from fork: ${branchName}`); + return; + } + + try { + await github.rest.git.deleteRef({ + owner, + repo, + ref: `heads/${branchName}` + }); + console.log(`Successfully deleted branch: ${branchName}`); + } catch (error) { + console.log(`Failed to delete branch ${branchName}: ${error.message}`); + } \ No newline at end of file