@@ -2161,33 +2161,69 @@ export const make = Effect.gen(function* () {
21612161 if ( input . refs . length === 0 ) return { stats : [ ] } ;
21622162 const scoped = input . refs . filter ( ( ref ) => ref . workspace !== undefined ) ;
21632163 if ( scoped . length > 0 ) {
2164+ const byScope = new Map < string , typeof scoped > ( ) ;
2165+ for ( const ref of scoped ) {
2166+ const key = [
2167+ ref . projectId ,
2168+ ref . workspace ?. threadId ,
2169+ ref . workspace ?. repositoryPath ,
2170+ ref . host ?? "" ,
2171+ ref . repository . trim ( ) . toLowerCase ( ) ,
2172+ ] . join ( "\0" ) ;
2173+ const group = byScope . get ( key ) ;
2174+ if ( group ) group . push ( ref ) ;
2175+ else byScope . set ( key , [ ref ] ) ;
2176+ }
2177+ const resolved = yield * Effect . forEach (
2178+ [ ...byScope . values ( ) ] ,
2179+ Effect . fn ( function * ( refs ) {
2180+ const project = yield * requireProject ( refs [ 0 ] ! ) . pipe (
2181+ Effect . orElseSucceed ( ( ) => undefined ) ,
2182+ ) ;
2183+ return project ?. api . listChangeRequestStats ? [ { project, refs } ] : [ ] ;
2184+ } ) ,
2185+ { concurrency : REPOSITORY_CONCURRENCY } ,
2186+ ) ;
2187+ const byCheckout = new Map < string , ( typeof resolved ) [ number ] > ( ) ;
2188+ for ( const entry of resolved . flat ( ) ) {
2189+ const key = `${ entry . project . host } \0${ entry . project . project . workspaceRoot } ` ;
2190+ const group = byCheckout . get ( key ) ;
2191+ if ( group ) group . push ( entry ) ;
2192+ else byCheckout . set ( key , [ entry ] ) ;
2193+ }
21642194 const scopedStats = yield * Effect . forEach (
2165- scoped ,
2166- Effect . fn ( function * ( ref ) {
2167- const project = yield * requireProject ( ref ) . pipe ( Effect . orElseSucceed ( ( ) => undefined ) ) ;
2168- if ( ! project ?. api . listChangeRequestStats ) return [ ] ;
2169- return yield * project . api
2170- . listChangeRequestStats ( {
2171- cwd : project . project . workspaceRoot ,
2172- host : project . host ,
2173- changeRequests : [ { repository : project . repository , number : ref . number } ] ,
2174- } )
2175- . pipe (
2176- Effect . map ( ( stats ) =>
2177- stats
2178- . filter (
2179- ( stat ) =>
2180- stat . number === ref . number &&
2181- stat . repository . toLowerCase ( ) === project . repository . toLowerCase ( ) ,
2182- )
2183- . map ( ( stat ) => ( {
2184- ...stat ,
2185- projectId : ref . projectId ,
2186- workspace : ref . workspace ,
2187- } ) ) ,
2195+ [ ...byCheckout . values ( ) ] ,
2196+ Effect . fn ( function * ( entries ) {
2197+ const first = entries [ 0 ] ! ;
2198+ const readStats = first . project . api . listChangeRequestStats ;
2199+ if ( ! readStats ) return [ ] ;
2200+ return yield * readStats ( {
2201+ cwd : first . project . project . workspaceRoot ,
2202+ host : first . project . host ,
2203+ changeRequests : entries . flatMap ( ( { project, refs } ) =>
2204+ refs . map ( ( ref ) => ( {
2205+ repository : project . repository ,
2206+ number : ref . number ,
2207+ } ) ) ,
2208+ ) ,
2209+ } ) . pipe (
2210+ Effect . map ( ( stats ) =>
2211+ stats . flatMap ( ( stat ) =>
2212+ entries . flatMap ( ( { project, refs } ) =>
2213+ stat . repository . toLowerCase ( ) === project . repository . toLowerCase ( )
2214+ ? refs
2215+ . filter ( ( ref ) => ref . number === stat . number )
2216+ . map ( ( ref ) => ( {
2217+ ...stat ,
2218+ projectId : ref . projectId ,
2219+ workspace : ref . workspace ,
2220+ } ) )
2221+ : [ ] ,
2222+ ) ,
21882223 ) ,
2189- Effect . orElseSucceed ( ( ) => [ ] ) ,
2190- ) ;
2224+ ) ,
2225+ Effect . orElseSucceed ( ( ) => [ ] ) ,
2226+ ) ;
21912227 } ) ,
21922228 { concurrency : REPOSITORY_CONCURRENCY } ,
21932229 ) ;
0 commit comments