Skip to content

Commit 515eef9

Browse files
committed
chore: improve cleanup
1 parent bf50ed5 commit 515eef9

File tree

1 file changed

+82
-13
lines changed

1 file changed

+82
-13
lines changed

src/git/github-provider.ts

Lines changed: 82 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -733,8 +733,33 @@ export class GitHubProvider implements GitProvider {
733733
*/
734734
async getBuddyBotBranches(): Promise<Array<{ name: string, sha: string, lastCommitDate: Date }>> {
735735
try {
736-
const branches = await this.apiRequest(`GET /repos/${this.owner}/${this.repo}/branches?per_page=100`)
737-
const buddyBranches = branches.filter((branch: any) => branch.name.startsWith('buddy-bot/'))
736+
// Fetch all branches with pagination
737+
let allBranches: any[] = []
738+
let page = 1
739+
const perPage = 100
740+
741+
while (true) {
742+
const branches = await this.apiRequest(`GET /repos/${this.owner}/${this.repo}/branches?per_page=${perPage}&page=${page}`)
743+
744+
if (!branches || branches.length === 0) {
745+
break
746+
}
747+
748+
allBranches = allBranches.concat(branches)
749+
750+
// If we got less than perPage results, we've reached the end
751+
if (branches.length < perPage) {
752+
break
753+
}
754+
755+
page++
756+
}
757+
758+
console.log(`🔍 Found ${allBranches.length} total branches in repository`)
759+
760+
// Filter for buddy-bot branches
761+
const buddyBranches = allBranches.filter((branch: any) => branch.name.startsWith('buddy-bot/'))
762+
console.log(`🤖 Found ${buddyBranches.length} buddy-bot branches`)
738763

739764
// Get detailed info for each branch including last commit date
740765
const branchDetails = await Promise.all(
@@ -795,19 +820,36 @@ export class GitHubProvider implements GitProvider {
795820
const cutoffDate = new Date()
796821
cutoffDate.setDate(cutoffDate.getDate() - olderThanDays)
797822

823+
console.log(`🔍 Looking for buddy-bot branches older than ${olderThanDays} days (before ${cutoffDate.toISOString()})`)
824+
798825
const orphanedBranches = await this.getOrphanedBuddyBotBranches()
799-
const staleBranches = orphanedBranches.filter(branch => branch.lastCommitDate < cutoffDate)
826+
console.log(`🔍 Found ${orphanedBranches.length} orphaned buddy-bot branches (no associated open PRs)`)
800827

828+
const staleBranches = orphanedBranches.filter(branch => branch.lastCommitDate < cutoffDate)
801829
console.log(`🔍 Found ${staleBranches.length} stale buddy-bot branches (older than ${olderThanDays} days)`)
802830

831+
// Show some examples of what we found
832+
if (staleBranches.length > 0) {
833+
console.log('📋 Sample of stale branches:')
834+
staleBranches.slice(0, 5).forEach((branch) => {
835+
const daysOld = Math.floor((Date.now() - branch.lastCommitDate.getTime()) / (1000 * 60 * 60 * 24))
836+
console.log(` - ${branch.name} (${daysOld} days old)`)
837+
})
838+
if (staleBranches.length > 5) {
839+
console.log(` ... and ${staleBranches.length - 5} more`)
840+
}
841+
}
842+
803843
if (staleBranches.length === 0) {
844+
console.log('✅ No stale branches to clean up!')
804845
return { deleted: [], failed: [] }
805846
}
806847

807848
if (dryRun) {
808849
console.log('🔍 [DRY RUN] Would delete the following branches:')
809850
staleBranches.forEach((branch) => {
810-
console.log(` - ${branch.name} (last commit: ${branch.lastCommitDate.toISOString()})`)
851+
const daysOld = Math.floor((Date.now() - branch.lastCommitDate.getTime()) / (1000 * 60 * 60 * 24))
852+
console.log(` - ${branch.name} (${daysOld} days old, last commit: ${branch.lastCommitDate.toISOString()})`)
811853
})
812854
return { deleted: staleBranches.map(b => b.name), failed: [] }
813855
}
@@ -817,18 +859,45 @@ export class GitHubProvider implements GitProvider {
817859

818860
console.log(`🧹 Cleaning up ${staleBranches.length} stale branches...`)
819861

820-
for (const branch of staleBranches) {
821-
try {
822-
await this.deleteBranch(branch.name)
823-
deleted.push(branch.name)
824-
console.log(`✅ Deleted stale branch: ${branch.name}`)
825-
}
826-
catch (error) {
827-
failed.push(branch.name)
828-
console.warn(`❌ Failed to delete branch ${branch.name}:`, error)
862+
// Delete branches in batches to avoid rate limiting
863+
const batchSize = 10
864+
for (let i = 0; i < staleBranches.length; i += batchSize) {
865+
const batch = staleBranches.slice(i, i + batchSize)
866+
const batchNumber = Math.floor(i / batchSize) + 1
867+
const totalBatches = Math.ceil(staleBranches.length / batchSize)
868+
869+
console.log(`🔄 Processing batch ${batchNumber}/${totalBatches} (${batch.length} branches)`)
870+
871+
await Promise.all(
872+
batch.map(async (branch) => {
873+
try {
874+
await this.deleteBranch(branch.name)
875+
deleted.push(branch.name)
876+
console.log(`✅ Deleted: ${branch.name}`)
877+
}
878+
catch (error) {
879+
failed.push(branch.name)
880+
console.warn(`❌ Failed to delete ${branch.name}:`, error)
881+
}
882+
}),
883+
)
884+
885+
// Small delay between batches to be nice to the API
886+
if (i + batchSize < staleBranches.length) {
887+
console.log('⏳ Waiting 1 second before next batch...')
888+
await new Promise(resolve => setTimeout(resolve, 1000))
829889
}
830890
}
831891

892+
console.log(`🎉 Cleanup complete!`)
893+
console.log(` ✅ Successfully deleted: ${deleted.length} branches`)
894+
console.log(` ❌ Failed to delete: ${failed.length} branches`)
895+
896+
if (failed.length > 0) {
897+
console.log('❌ Failed branches:')
898+
failed.forEach(branch => console.log(` - ${branch}`))
899+
}
900+
832901
return { deleted, failed }
833902
}
834903

0 commit comments

Comments
 (0)