generated from mintlify/starter
-
Notifications
You must be signed in to change notification settings - Fork 167
41 lines (35 loc) · 1.32 KB
/
delete-merged-branches.yml
File metadata and controls
41 lines (35 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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}`);
}