@@ -476,3 +476,69 @@ pub async fn local_notes(ids: &[i64]) -> laravel::Result<HashMap<i64, String>> {
476476 }
477477 Ok ( out)
478478}
479+
480+ /// Where a snapshot's new version was already approved: the same name and platform, the same content (its
481+ /// fingerprint), approved in a review on another branch. A hint for the reviewer: it approves nothing.
482+ #[ derive( Clone , Debug , PartialEq , serde:: Serialize ) ]
483+ pub struct ApprovedElsewhere {
484+ pub branch : String ,
485+ pub pr_number : Option < i32 > ,
486+ /// The build it was approved in, and the snapshot there.
487+ pub build : i32 ,
488+ pub snapshot_id : laravel:: str:: Uuid ,
489+ /// Who approved it ("Claude Code for Ada Lovelace").
490+ pub approved_by : Option < String > ,
491+ pub approved_at : String ,
492+ }
493+
494+ /// For a build's changed and added snapshots, and those that passed by matching an approval elsewhere, the newest
495+ /// matching approval on another branch, by snapshot id. Changes are always the build's own rows.
496+ ///
497+ /// # Errors
498+ /// On a database error.
499+ pub async fn approved_elsewhere ( build_id : i64 ) -> laravel:: Result < HashMap < i64 , ApprovedElsewhere > > {
500+ let rows = db:: query!(
501+ "SELECT DISTINCT ON (s.id) s.id AS snapshot_id, bl.branch, b2.pr_number, b2.number, s2.pid AS other, \
502+ u.name AS approver, r.source, r.client_name, r.agent, r.token_name, bl.created_at \
503+ FROM snapshots s \
504+ JOIN builds b ON b.id = s.build_id \
505+ JOIN baselines bl ON bl.project_id = b.project_id AND bl.name = s.name AND bl.platform = s.platform \
506+ AND bl.approval = 'review' AND bl.branch <> b.branch AND bl.snapshot_id IS NOT NULL \
507+ JOIN snapshots s2 ON s2.id = bl.snapshot_id AND s2.source_fingerprint = s.source_fingerprint \
508+ JOIN builds b2 ON b2.id = bl.build_id \
509+ LEFT JOIN users u ON u.id = bl.approved_by \
510+ LEFT JOIN reviews r ON r.id = bl.review_id \
511+ WHERE s.build_id = {build_id} AND (s.state IN ('changed', 'added') \
512+ OR EXISTS (SELECT 1 FROM snapshot_pages p WHERE p.snapshot_id = s.id AND p.state = 'variant')) \
513+ ORDER BY s.id, bl.created_at DESC"
514+ )
515+ . select ( )
516+ . await ?;
517+ let mut out = HashMap :: new ( ) ;
518+ for r in rows {
519+ let at: laravel:: time:: Timestamp = r. try_get ( "created_at" ) ?;
520+ let person: Option < String > = r. try_get ( "approver" ) ?;
521+ let approved_by = match r. try_get :: < Option < String > > ( "source" ) ? {
522+ Some ( source) => Some ( super :: reviews:: by (
523+ & source,
524+ r. try_get :: < Option < String > > ( "client_name" ) ?. as_deref ( ) ,
525+ r. try_get :: < Option < String > > ( "agent" ) ?. as_deref ( ) ,
526+ r. try_get :: < Option < String > > ( "token_name" ) ?. as_deref ( ) ,
527+ person. as_deref ( ) ,
528+ ) ) ,
529+ None => person,
530+ } ;
531+ out. insert (
532+ r. try_get :: < i64 > ( "snapshot_id" ) ?,
533+ ApprovedElsewhere {
534+ branch : r. try_get ( "branch" ) ?,
535+ pr_number : r. try_get ( "pr_number" ) ?,
536+ build : r. try_get ( "number" ) ?,
537+ snapshot_id : r. try_get ( "other" ) ?,
538+ approved_by,
539+ approved_at : at. to_string ( ) ,
540+ } ,
541+ ) ;
542+ }
543+ Ok ( out)
544+ }
0 commit comments