@@ -916,9 +916,12 @@ export class GitHubProvider implements GitProvider {
916
916
/**
917
917
* Get branches that have open PRs using HTTP requests to GitHub (no API auth needed)
918
918
*/
919
+ private httpDetectionSuccessful = false
920
+
919
921
private async getOpenPRBranchesViaGit ( ) : Promise < Set < string > > {
920
922
try {
921
923
const protectedBranches = new Set < string > ( )
924
+ this . httpDetectionSuccessful = false
922
925
923
926
console . log ( '🔍 Using HTTP requests to check actual PR status (no API auth needed)...' )
924
927
@@ -1005,33 +1008,32 @@ export class GitHubProvider implements GitProvider {
1005
1008
1006
1009
console . log ( `✅ Checked ${ checkedCount } PRs via HTTP: ${ openCount } open, ${ checkedCount - openCount } closed` )
1007
1010
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
1011
1013
}
1012
1014
catch ( error ) {
1013
1015
console . warn ( '⚠️ Could not check PR status via HTTP, applying conservative fallback:' , error )
1014
1016
1015
- // Only apply fallback protection if HTTP detection failed
1017
+ // Only apply fallback protection if HTTP detection completely failed
1016
1018
try {
1017
1019
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 )
1020
1022
1021
1023
let fallbackCount = 0
1022
1024
for ( const branch of allBuddyBranches ) {
1023
- if ( branch . lastCommitDate > threeDaysAgo && ! protectedBranches . has ( branch . name ) ) {
1025
+ if ( branch . lastCommitDate > oneDayAgo && ! protectedBranches . has ( branch . name ) ) {
1024
1026
protectedBranches . add ( branch . name )
1025
1027
fallbackCount ++
1026
1028
}
1027
1029
}
1028
1030
1029
1031
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` )
1031
1033
}
1032
1034
}
1033
1035
catch {
1034
- console . log ( '⚠️ Could not apply fallback protection' )
1036
+ console . log ( '⚠️ Could not apply emergency fallback protection' )
1035
1037
}
1036
1038
}
1037
1039
@@ -1067,37 +1069,51 @@ export class GitHubProvider implements GitProvider {
1067
1069
}
1068
1070
1069
1071
/**
1070
- * Clean up stale buddy-bot branches
1072
+ * Clean up orphaned buddy-bot branches (with optional age filter for fallback scenarios)
1071
1073
*/
1072
1074
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...` )
1077
1076
1078
1077
const orphanedBranches = await this . getOrphanedBuddyBotBranches ( )
1079
1078
console . log ( `🔍 Found ${ orphanedBranches . length } orphaned buddy-bot branches (no associated open PRs)` )
1080
1079
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
+ }
1083
1097
1084
1098
// 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 ) => {
1088
1102
const daysOld = Math . floor ( ( Date . now ( ) - branch . lastCommitDate . getTime ( ) ) / ( 1000 * 60 * 60 * 24 ) )
1089
1103
console . log ( ` - ${ branch . name } (${ daysOld } days old)` )
1090
1104
} )
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` )
1093
1107
}
1094
1108
}
1095
1109
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!' )
1098
1112
return { deleted : [ ] , failed : [ ] }
1099
1113
}
1100
1114
1115
+ const staleBranches = branchesToDelete
1116
+
1101
1117
if ( dryRun ) {
1102
1118
console . log ( '🔍 [DRY RUN] Would delete the following branches:' )
1103
1119
staleBranches . forEach ( ( branch ) => {
0 commit comments