@@ -733,8 +733,33 @@ export class GitHubProvider implements GitProvider {
733
733
*/
734
734
async getBuddyBotBranches ( ) : Promise < Array < { name : string , sha : string , lastCommitDate : Date } > > {
735
735
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` )
738
763
739
764
// Get detailed info for each branch including last commit date
740
765
const branchDetails = await Promise . all (
@@ -795,19 +820,36 @@ export class GitHubProvider implements GitProvider {
795
820
const cutoffDate = new Date ( )
796
821
cutoffDate . setDate ( cutoffDate . getDate ( ) - olderThanDays )
797
822
823
+ console . log ( `🔍 Looking for buddy-bot branches older than ${ olderThanDays } days (before ${ cutoffDate . toISOString ( ) } )` )
824
+
798
825
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)` )
800
827
828
+ const staleBranches = orphanedBranches . filter ( branch => branch . lastCommitDate < cutoffDate )
801
829
console . log ( `🔍 Found ${ staleBranches . length } stale buddy-bot branches (older than ${ olderThanDays } days)` )
802
830
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
+
803
843
if ( staleBranches . length === 0 ) {
844
+ console . log ( '✅ No stale branches to clean up!' )
804
845
return { deleted : [ ] , failed : [ ] }
805
846
}
806
847
807
848
if ( dryRun ) {
808
849
console . log ( '🔍 [DRY RUN] Would delete the following branches:' )
809
850
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 ( ) } )` )
811
853
} )
812
854
return { deleted : staleBranches . map ( b => b . name ) , failed : [ ] }
813
855
}
@@ -817,18 +859,45 @@ export class GitHubProvider implements GitProvider {
817
859
818
860
console . log ( `🧹 Cleaning up ${ staleBranches . length } stale branches...` )
819
861
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 ) )
829
889
}
830
890
}
831
891
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
+
832
901
return { deleted, failed }
833
902
}
834
903
0 commit comments