Skip to content

Commit 01587b4

Browse files
committed
chore: also handle stale branches from today
1 parent 10c4c41 commit 01587b4

File tree

3 files changed

+39
-49
lines changed

3 files changed

+39
-49
lines changed

.github/workflows/buddy-bot.yml

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -207,19 +207,6 @@ jobs:
207207
bunx buddy-bot update-check --verbose
208208
fi
209209
210-
- name: Clean up stale branches
211-
if: ${{ github.event.inputs.dry_run != 'true' }}
212-
run: |
213-
echo "🧹 Cleaning up stale buddy-bot branches..."
214-
echo "This will remove branches older than 2 days that don't have associated open PRs"
215-
216-
# Run cleanup automatically (with confirmation disabled in CI)
217-
bunx buddy-bot cleanup --days 2 --force --verbose
218-
219-
env:
220-
GITHUB_TOKEN: ${{ secrets.BUDDY_BOT_TOKEN || secrets.GITHUB_TOKEN }}
221-
BUDDY_BOT_TOKEN: ${{ secrets.BUDDY_BOT_TOKEN }}
222-
223210
- name: Create check summary
224211
if: always()
225212
run: |

src/git/github-provider.ts

Lines changed: 39 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -916,9 +916,12 @@ export class GitHubProvider implements GitProvider {
916916
/**
917917
* Get branches that have open PRs using HTTP requests to GitHub (no API auth needed)
918918
*/
919+
private httpDetectionSuccessful = false
920+
919921
private async getOpenPRBranchesViaGit(): Promise<Set<string>> {
920922
try {
921923
const protectedBranches = new Set<string>()
924+
this.httpDetectionSuccessful = false
922925

923926
console.log('🔍 Using HTTP requests to check actual PR status (no API auth needed)...')
924927

@@ -1005,33 +1008,32 @@ export class GitHubProvider implements GitProvider {
10051008

10061009
console.log(`✅ Checked ${checkedCount} PRs via HTTP: ${openCount} open, ${checkedCount - openCount} closed`)
10071010
console.log(`🛡️ Protected ${protectedBranches.size} branches with confirmed open PRs`)
1008-
1009-
// Since HTTP detection was successful and is 100% accurate, we don't need fallback protection
1010-
console.log(`🎯 HTTP detection successful`)
1011+
console.log(`🎯 HTTP detection successful - no age-based protection needed`)
1012+
this.httpDetectionSuccessful = true
10111013
}
10121014
catch (error) {
10131015
console.warn('⚠️ Could not check PR status via HTTP, applying conservative fallback:', error)
10141016

1015-
// Only apply fallback protection if HTTP detection failed
1017+
// Only apply fallback protection if HTTP detection completely failed
10161018
try {
10171019
const allBuddyBranches = await this.getBuddyBotBranches()
1018-
const threeDaysAgo = new Date()
1019-
threeDaysAgo.setDate(threeDaysAgo.getDate() - 3)
1020+
const oneDayAgo = new Date()
1021+
oneDayAgo.setDate(oneDayAgo.getDate() - 1)
10201022

10211023
let fallbackCount = 0
10221024
for (const branch of allBuddyBranches) {
1023-
if (branch.lastCommitDate > threeDaysAgo && !protectedBranches.has(branch.name)) {
1025+
if (branch.lastCommitDate > oneDayAgo && !protectedBranches.has(branch.name)) {
10241026
protectedBranches.add(branch.name)
10251027
fallbackCount++
10261028
}
10271029
}
10281030

10291031
if (fallbackCount > 0) {
1030-
console.log(`🛡️ Fallback protection: ${fallbackCount} recent branches (< 3 days) protected due to HTTP failure`)
1032+
console.log(`🛡️ Emergency fallback: ${fallbackCount} very recent branches (< 1 day) protected due to HTTP failure`)
10311033
}
10321034
}
10331035
catch {
1034-
console.log('⚠️ Could not apply fallback protection')
1036+
console.log('⚠️ Could not apply emergency fallback protection')
10351037
}
10361038
}
10371039

@@ -1067,37 +1069,51 @@ export class GitHubProvider implements GitProvider {
10671069
}
10681070

10691071
/**
1070-
* Clean up stale buddy-bot branches
1072+
* Clean up orphaned buddy-bot branches (with optional age filter for fallback scenarios)
10711073
*/
10721074
async cleanupStaleBranches(olderThanDays = 7, dryRun = false): Promise<{ deleted: string[], failed: string[] }> {
1073-
const cutoffDate = new Date()
1074-
cutoffDate.setDate(cutoffDate.getDate() - olderThanDays)
1075-
1076-
console.log(`🔍 Looking for buddy-bot branches older than ${olderThanDays} days (before ${cutoffDate.toISOString()})`)
1075+
console.log(`🔍 Looking for buddy-bot branches without open PRs...`)
10771076

10781077
const orphanedBranches = await this.getOrphanedBuddyBotBranches()
10791078
console.log(`🔍 Found ${orphanedBranches.length} orphaned buddy-bot branches (no associated open PRs)`)
10801079

1081-
const staleBranches = orphanedBranches.filter(branch => branch.lastCommitDate < cutoffDate)
1082-
console.log(`🔍 Found ${staleBranches.length} stale buddy-bot branches (older than ${olderThanDays} days)`)
1080+
// Since we have 100% accurate HTTP-based PR detection, we can clean up ALL orphaned branches
1081+
// Only apply age filter if HTTP detection failed (indicated by very conservative protection)
1082+
let branchesToDelete = orphanedBranches
1083+
1084+
// Use the HTTP detection success flag to determine cleanup strategy
1085+
if (this.httpDetectionSuccessful) {
1086+
// HTTP detection worked perfectly - clean up ALL orphaned branches regardless of age
1087+
console.log(`🎯 HTTP detection successful - cleaning up ALL ${branchesToDelete.length} orphaned branches (any age)`)
1088+
}
1089+
else {
1090+
// HTTP detection failed - apply conservative age filter
1091+
const cutoffDate = new Date()
1092+
cutoffDate.setDate(cutoffDate.getDate() - olderThanDays)
1093+
branchesToDelete = orphanedBranches.filter(branch => branch.lastCommitDate < cutoffDate)
1094+
console.log(`⚠️ HTTP detection failed - only deleting branches older than ${olderThanDays} days`)
1095+
console.log(`🔍 Found ${branchesToDelete.length} stale buddy-bot branches (older than ${olderThanDays} days)`)
1096+
}
10831097

10841098
// Show some examples of what we found
1085-
if (staleBranches.length > 0) {
1086-
console.log('📋 Sample of stale branches:')
1087-
staleBranches.slice(0, 5).forEach((branch) => {
1099+
if (branchesToDelete.length > 0) {
1100+
console.log('📋 Sample of branches to delete:')
1101+
branchesToDelete.slice(0, 5).forEach((branch) => {
10881102
const daysOld = Math.floor((Date.now() - branch.lastCommitDate.getTime()) / (1000 * 60 * 60 * 24))
10891103
console.log(` - ${branch.name} (${daysOld} days old)`)
10901104
})
1091-
if (staleBranches.length > 5) {
1092-
console.log(` ... and ${staleBranches.length - 5} more`)
1105+
if (branchesToDelete.length > 5) {
1106+
console.log(` ... and ${branchesToDelete.length - 5} more`)
10931107
}
10941108
}
10951109

1096-
if (staleBranches.length === 0) {
1097-
console.log('✅ No stale branches to clean up!')
1110+
if (branchesToDelete.length === 0) {
1111+
console.log('✅ No branches to clean up!')
10981112
return { deleted: [], failed: [] }
10991113
}
11001114

1115+
const staleBranches = branchesToDelete
1116+
11011117
if (dryRun) {
11021118
console.log('🔍 [DRY RUN] Would delete the following branches:')
11031119
staleBranches.forEach((branch) => {

src/setup.ts

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1307,19 +1307,6 @@ ${generateComposerSetupSteps()}
13071307
GITHUB_TOKEN: ${tokenEnv}
13081308
BUDDY_BOT_TOKEN: \${{ secrets.BUDDY_BOT_TOKEN }}
13091309
1310-
- name: Clean up stale branches
1311-
if: \${{ github.event.inputs.dry_run != 'true' }}
1312-
run: |
1313-
echo "🧹 Cleaning up stale buddy-bot branches..."
1314-
echo "This will remove branches older than 2 days that don't have associated open PRs"
1315-
1316-
# Run cleanup automatically (with confirmation disabled in CI)
1317-
bunx buddy-bot cleanup --days 2 --force --verbose
1318-
1319-
env:
1320-
GITHUB_TOKEN: ${tokenEnv}
1321-
BUDDY_BOT_TOKEN: \${{ secrets.BUDDY_BOT_TOKEN }}
1322-
13231310
- name: Create check summary
13241311
if: always()
13251312
run: |

0 commit comments

Comments
 (0)