-
Notifications
You must be signed in to change notification settings - Fork 0
216 lines (181 loc) · 8 KB
/
Copy pathcleanup-stale-branches.yml
File metadata and controls
216 lines (181 loc) · 8 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
name: Cleanup Stale Branches
on:
schedule:
# Run every Monday at 00:00 UTC
- cron: '0 0 * * 1'
workflow_dispatch: # Allow manual trigger
permissions:
contents: write
jobs:
cleanup-stale-branches:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v6
with:
fetch-depth: 0
- name: Clean up stale branches
uses: actions/github-script@v7
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const { owner, repo } = context.repo;
// Get default branch
const { data: repository } = await github.rest.repos.get({
owner,
repo
});
const defaultBranch = repository.default_branch;
console.log(`Default branch: ${defaultBranch}`);
// Get all branches
const { data: branches } = await github.rest.repos.listBranches({
owner,
repo,
per_page: 100
});
console.log(`Total branches: ${branches.length}`);
const protectedBranches = [defaultBranch, 'main', 'master', 'develop', 'development', 'staging', 'production'];
let deletedCount = 0;
let skippedCount = 0;
for (const branch of branches) {
const branchName = branch.name;
// Skip protected branches
if (protectedBranches.includes(branchName)) {
console.log(`⏭️ Skipping protected branch: ${branchName}`);
skippedCount++;
continue;
}
try {
// Check if branch has an open PR
const { data: prs } = await github.rest.pulls.list({
owner,
repo,
head: `${owner}:${branchName}`,
state: 'open',
per_page: 1
});
if (prs.length > 0) {
console.log(`⏭️ Skipping branch with open PR: ${branchName}`);
skippedCount++;
continue;
}
// Check if branch has closed/merged PRs
const { data: closedPrs } = await github.rest.pulls.list({
owner,
repo,
head: `${owner}:${branchName}`,
state: 'closed',
per_page: 1
});
if (closedPrs.length > 0) {
const pr = closedPrs[0];
const daysSinceClosed = (Date.now() - new Date(pr.closed_at)) / (1000 * 60 * 60 * 24);
// Delete branches from merged/closed PRs older than 7 days
if (daysSinceClosed > 7) {
console.log(`🗑️ Deleting stale branch: ${branchName} (PR closed ${Math.floor(daysSinceClosed)} days ago)`);
await github.rest.git.deleteRef({
owner,
repo,
ref: `heads/${branchName}`
});
deletedCount++;
console.log(`✅ Deleted: ${branchName}`);
} else {
console.log(`⏭️ Branch too recent: ${branchName} (closed ${Math.floor(daysSinceClosed)} days ago)`);
skippedCount++;
}
} else {
// Check if branch is stale (no commits in last 90 days)
const { data: commits } = await github.rest.repos.listCommits({
owner,
repo,
sha: branchName,
per_page: 1
});
if (commits.length > 0) {
const lastCommitDate = new Date(commits[0].commit.committer.date);
const daysSinceLastCommit = (Date.now() - lastCommitDate) / (1000 * 60 * 60 * 24);
if (daysSinceLastCommit > 90) {
console.log(`🗑️ Deleting stale branch: ${branchName} (last commit ${Math.floor(daysSinceLastCommit)} days ago)`);
await github.rest.git.deleteRef({
owner,
repo,
ref: `heads/${branchName}`
});
deletedCount++;
console.log(`✅ Deleted: ${branchName}`);
} else {
console.log(`⏭️ Branch still active: ${branchName} (last commit ${Math.floor(daysSinceLastCommit)} days ago)`);
skippedCount++;
}
}
}
} catch (error) {
console.error(`❌ Error processing branch ${branchName}:`, error.message);
skippedCount++;
}
}
console.log('\n📊 Summary:');
console.log(`- Total branches: ${branches.length}`);
console.log(`- Deleted: ${deletedCount}`);
console.log(`- Skipped: ${skippedCount}`);
// Create issue comment with summary
if (deletedCount > 0) {
const issues = await github.rest.issues.listForRepo({
owner,
repo,
state: 'open',
labels: 'maintenance',
per_page: 1
});
if (issues.data.length > 0) {
await github.rest.issues.createComment({
owner,
repo,
issue_number: issues.data[0].number,
body: `🧹 **Stale Branch Cleanup Report**\n\n- **Deleted branches:** ${deletedCount}\n- **Skipped branches:** ${skippedCount}\n- **Total branches:** ${branches.length}\n\nCleanup completed successfully.`
});
}
}
- name: Cleanup dependabot branches specifically
uses: actions/github-script@v7
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const { owner, repo } = context.repo;
// Get all branches
const { data: branches } = await github.rest.repos.listBranches({
owner,
repo,
per_page: 100
});
const dependabotBranches = branches.filter(branch =>
branch.name.startsWith('dependabot/')
);
console.log(`Found ${dependabotBranches.length} dependabot branches`);
let deletedDependabotCount = 0;
for (const branch of dependabotBranches) {
try {
// Check if has closed PR
const { data: prs } = await github.rest.pulls.list({
owner,
repo,
head: `${owner}:${branch.name}`,
state: 'closed',
per_page: 1
});
if (prs.length > 0) {
console.log(`🗑️ Deleting dependabot branch with closed PR: ${branch.name}`);
await github.rest.git.deleteRef({
owner,
repo,
ref: `heads/${branch.name}`
});
deletedDependabotCount++;
console.log(`✅ Deleted dependabot branch: ${branch.name}`);
}
} catch (error) {
console.error(`Error processing dependabot branch ${branch.name}:`, error.message);
}
}
console.log(`\n🤖 Deleted ${deletedDependabotCount} dependabot branches`);