@@ -309,8 +309,7 @@ impl VisualCi {
309309 . map_err ( internal) ?
310310 . ok_or_else ( || format ! ( "no snapshot {name} in build {}" , build. number) ) ) ;
311311 }
312- let all: Vec < Snapshot > =
313- snaps:: of_build ( build. id ) . await . map_err ( internal) ?. into_iter ( ) . filter ( |s| s. name == name) . collect ( ) ;
312+ let all = snaps:: named_in_build ( build. id , name) . await . map_err ( internal) ?;
314313 if let Some ( s) = all. iter ( ) . find ( |s| s. platform == platform) {
315314 return Ok ( Ok ( s. clone ( ) ) ) ;
316315 }
@@ -445,11 +444,11 @@ impl VisualCi {
445444 let member = Self :: role_in ( & rc, & project) . await ?. is_some ( ) ;
446445 let limit = usize:: try_from ( args. limit . unwrap_or ( 20 ) . clamp ( 1 , 100 ) ) . unwrap_or ( 20 ) ;
447446 let mut list = Vec :: new ( ) ;
448- for b in domain :: builds :: recent ( project . id , 1000 ) . await . map_err ( internal ) ? {
449- if args . branch . as_ref ( ) . is_some_and ( |br| * br != b . branch ) {
450- continue ;
451- }
452- if args . waiting_for_review == Some ( true ) && !domain:: builds:: waits_for ( & b, user_id ( & rc) ) {
447+ let waiting = args . waiting_for_review == Some ( true ) ;
448+ // Waiting builds include local ones that wait only for their own person: read a few more than asked for.
449+ let scan = if waiting { 1000 } else { limit as u64 } ;
450+ for b in domain :: builds :: recent ( project . id , args . branch . as_deref ( ) , waiting , scan ) . await . map_err ( internal ) ? {
451+ if waiting && !domain:: builds:: waits_for ( & b, user_id ( & rc) ) {
453452 continue ;
454453 }
455454 list. push ( Self :: build_json ( & project, & b, member) . await ?) ;
@@ -900,13 +899,8 @@ impl VisualCi {
900899 ) -> Result < CallToolResult , ErrorData > {
901900 let project = need ! ( Self :: project( & rc, args. project. as_deref( ) ) ) ;
902901 let limit = usize:: try_from ( args. limit . unwrap_or ( 20 ) . clamp ( 1 , 200 ) ) . unwrap_or ( 20 ) ;
903- let recent: Vec < Build > = domain:: builds:: recent ( project. id , 2000 )
904- . await
905- . map_err ( internal) ?
906- . into_iter ( )
907- . filter ( |b| args. branch . as_ref ( ) . is_none_or ( |br| * br == b. branch ) )
908- . take ( limit)
909- . collect ( ) ;
902+ let recent: Vec < Build > =
903+ domain:: builds:: recent ( project. id , args. branch . as_deref ( ) , false , limit as u64 ) . await . map_err ( internal) ?;
910904 let ids: Vec < i64 > = recent. iter ( ) . map ( |b| b. id ) . collect ( ) ;
911905 let ( name, platform) = ( args. name . as_str ( ) , args. platform . as_str ( ) ) ;
912906 let found: Vec < Snapshot > = if ids. is_empty ( ) {
@@ -954,17 +948,16 @@ impl VisualCi {
954948 } ;
955949 // Straight after a run the build may still be comparing: wait for it rather than refusing.
956950 let build = refuse ! ( domain:: builds:: wait_until_reviewable( build, std:: time:: Duration :: from_secs( 60 ) ) ) ;
951+ let all = snaps:: of_build ( build. id ) . await . map_err ( internal) ?;
957952 let ids = match ( & args. snapshots , args. only . as_deref ( ) ) {
958953 ( Some ( _) , Some ( _) ) => return Ok ( tool_error ( "pass `snapshots` or `only`, not both" ) ) ,
959- ( Some ( names) , None ) => Some (
960- snaps:: of_build ( build. id )
961- . await
962- . map_err ( internal) ?
963- . into_iter ( )
964- . filter ( |s| names. contains ( & s. name ) )
965- . map ( |s| s. id )
966- . collect ( ) ,
967- ) ,
954+ ( Some ( names) , None ) => {
955+ // A name that isn't in the build is a mistake to say, never an empty review.
956+ if let Some ( missing) = names. iter ( ) . find ( |n| !all. iter ( ) . any ( |s| & s. name == * n) ) {
957+ return Ok ( tool_error ( format ! ( "no snapshot {missing:?} in build {}" , build. number) ) ) ;
958+ }
959+ Some ( all. iter ( ) . filter ( |s| names. contains ( & s. name ) ) . map ( |s| s. id ) . collect ( ) )
960+ }
968961 ( None , Some ( only) ) => Some ( refuse ! ( snaps:: picked_by( build. id, only) ) ) ,
969962 ( None , None ) => None ,
970963 } ;
@@ -974,18 +967,23 @@ impl VisualCi {
974967 // A review with a project token is recorded without a user.
975968 let via = Self :: via ( & rc) ;
976969 let rejected_ids = ids. clone ( ) ;
977- let build = refuse ! ( domain:: reviews:: review(
978- build. clone( ) ,
979- user_id( & rc) ,
980- & via,
981- & args. action,
982- ids,
983- args. note. as_deref( )
984- ) ) ;
970+ // Under the build's lock, so two approvals at once can't both record the same baselines.
971+ let ( user, action, note) = ( user_id ( & rc) , args. action . clone ( ) , args. note . clone ( ) ) ;
972+ let reviewed = laravel:: db:: transaction ( async |_| {
973+ domain:: processing:: lock ( build. id ) . await ?;
974+ let fresh = Build :: find_or_fail ( build. id ) . await ?;
975+ domain:: reviews:: review ( fresh, user, & via, & action, ids. clone ( ) , note. as_deref ( ) ) . await
976+ } )
977+ . await
978+ . map_err ( internal) ?;
979+ let build = match reviewed {
980+ Ok ( b) => b,
981+ Err ( message) => return Ok ( tool_error ( message) ) ,
982+ } ;
985983 if let Some ( body) = & args. comment {
986984 let author = Self :: author ( & rc) ;
987985 let place = comments:: Place { page : None , side : "head" . into ( ) , anchor : comments:: Anchor :: Image } ;
988- for s in snaps :: of_build ( build . id ) . await . map_err ( internal ) ? {
986+ for s in all {
989987 let named = rejected_ids. as_ref ( ) . is_none_or ( |ids| ids. contains ( & s. id ) ) ;
990988 if named && snaps:: is_change ( & s) && !s. sourced {
991989 refuse ! ( comments:: start( & build, & s, & place, & author, body) ) ;
0 commit comments