From dbc42f9d24a6bb52df841d1335c4f4a7e89a17e3 Mon Sep 17 00:00:00 2001 From: Nathan Broadbent Date: Fri, 4 Dec 2026 17:34:14 +0000 Subject: [PATCH] Builds: the pull_request run of a commit the push run already sent shows that build's result instead of comparing again (case studies 9b) --- app/src/domain/reruns.rs | 84 +++++++++++++++++++++++++++++++++++----- app/tests/reruns.rs | 54 +++++++++++++++++++++++++- 2 files changed, 128 insertions(+), 10 deletions(-) diff --git a/app/src/domain/reruns.rs b/app/src/domain/reruns.rs index 77904424..3d9fc4b6 100644 --- a/app/src/domain/reruns.rs +++ b/app/src/domain/reruns.rs @@ -8,6 +8,13 @@ //! A build follows only when it's a CI build that was sent no snapshots (`--allow-empty` or not), and an earlier CI build //! of the same project, suite (`name`), environment, commit and branch has been finalized and wasn't itself empty. While //! that build is still processing, the empty one waits, processing, and concludes when it does. +//! +//! A build that was sent exactly what such an earlier build was sent follows it too (case studies, 9b): a workflow run +//! for both `push` and `pull_request` sends the same snapshots of the same commit twice, and comparing them again would +//! only charge for the same result. Exactly: the same snapshots (name, platform, environment, kind, contents, settings +//! and errors), the same visualci.yml, the same base branch and the same subset and shards, one build with a pull +//! request and the other without, within the hour, with no settings change or other approval in between +//! ([`twin`]). Anything else, and it's compared as usual. use laravel::db; use laravel::model::update; @@ -15,7 +22,7 @@ use laravel::model::update; use super::builds::EMPTY_BUILD; use crate::models::Build; -/// The warning on a build that follows an earlier one. +/// The warning on a build that was sent no snapshots and follows an earlier one. #[must_use] pub fn covered_warning(number: i32) -> String { format!( @@ -24,6 +31,15 @@ pub fn covered_warning(number: i32) -> String { ) } +/// The warning on a build that was sent the same snapshots as an earlier one and follows it. +#[must_use] +pub fn twin_warning(number: i32) -> String { + format!( + "This run sent the same snapshots of this commit as build #{number} (a workflow that runs on both push and \ + pull_request?), so this build shows its result instead of comparing them again." + ) +} + /// How many snapshots the build was sent itself (not carried forward, not counted as removed, not the visualci.yml /// item). async fn sent(build_id: i64) -> laravel::Result { @@ -68,6 +84,55 @@ pub async fn covering(build: &Build) -> laravel::Result> { Ok(found.into_iter().next()) } +/// The earlier build this CI build is the other half of, when a workflow runs on both `push` and `pull_request`: the +/// newest earlier CI build of the same project, suite, environment, commit, branch, visualci.yml, base branch, subset +/// and shards that was finalized, wasn't empty and follows no build itself, whose sent snapshots are this build's, +/// field for field. One of the two has a pull request and the other doesn't, so a re-run of the same workflow is +/// compared afresh. And nothing that decides a comparison has moved since: it was made within the hour, the project's +/// settings haven't changed since, and no other build of the suite has been approved since. +/// +/// # Errors +/// On a database error. +pub async fn twin(build: &Build) -> laravel::Result> { + if build.mode != "ci" { + return Ok(None); + } + let id = build.id; + let found: Vec = db::query!( + "SELECT b.* FROM builds me JOIN projects p ON p.id = me.project_id JOIN builds b \ + ON b.project_id = me.project_id AND b.name = me.name AND b.environment = me.environment \ + AND b.commit_sha = me.commit_sha AND b.branch = me.branch AND b.mode = 'ci' AND b.id < me.id \ + WHERE me.id = {id} AND b.covered_by_build_id IS NULL AND b.state IN ('processing', 'completed') \ + AND b.error IS DISTINCT FROM {EMPTY_BUILD} AND b.config IS NOT DISTINCT FROM me.config \ + AND COALESCE(b.base_branch, p.default_branch) = COALESCE(me.base_branch, p.default_branch) \ + AND b.subset = me.subset AND b.shard_total = me.shard_total \ + AND (b.pr_number IS NULL) <> (me.pr_number IS NULL) AND b.created_at > me.created_at - interval '1 hour' \ + AND p.updated_at <= b.created_at \ + AND NOT EXISTS (SELECT 1 FROM baselines bl WHERE bl.project_id = me.project_id \ + AND bl.build_name = me.name AND bl.created_at > b.created_at AND bl.build_id <> b.id) \ + AND NOT EXISTS ( \ + (SELECT name, platform, environment, kind, source_fingerprint, source_blob, threshold, \ + ignore_anti_aliasing, meta, format, ignore_rules, error FROM snapshots \ + WHERE build_id = b.id AND NOT carried_forward AND state <> 'removed' AND platform <> 'visualci' \ + EXCEPT ALL \ + SELECT name, platform, environment, kind, source_fingerprint, source_blob, threshold, \ + ignore_anti_aliasing, meta, format, ignore_rules, error FROM snapshots \ + WHERE build_id = me.id AND NOT carried_forward AND state <> 'removed' AND platform <> 'visualci') \ + UNION ALL \ + (SELECT name, platform, environment, kind, source_fingerprint, source_blob, threshold, \ + ignore_anti_aliasing, meta, format, ignore_rules, error FROM snapshots \ + WHERE build_id = me.id AND NOT carried_forward AND state <> 'removed' AND platform <> 'visualci' \ + EXCEPT ALL \ + SELECT name, platform, environment, kind, source_fingerprint, source_blob, threshold, \ + ignore_anti_aliasing, meta, format, ignore_rules, error FROM snapshots \ + WHERE build_id = b.id AND NOT carried_forward AND state <> 'removed' AND platform <> 'visualci')) \ + ORDER BY b.id DESC LIMIT 1" + ) + .select_as() + .await?; + Ok(found.into_iter().next()) +} + /// Processing's first step for a CI build: when it was sent no snapshots and an earlier build covers its commit, it /// follows that build. `None`: it doesn't, so it's processed as usual. `Some(false)`: the earlier build is still /// processing, and this one waits for it. `Some(true)`: it concluded with the earlier build's result. @@ -78,16 +143,17 @@ pub async fn follow(build: &Build) -> laravel::Result> { if build.mode != "ci" { return Ok(None); } + let empty = sent(build.id).await? == 0; let earlier = match build.covered_by_build_id { Some(id) => Build::find(id).await?, - None if sent(build.id).await? == 0 => covering(build).await?, - None => None, + None if empty => covering(build).await?, + None => twin(build).await?, }; let Some(earlier) = earlier else { return Ok(None) }; if !matches!(earlier.state.as_str(), "completed" | "failed") { if build.covered_by_build_id.is_none() { let mut build = build.clone(); - let warnings = with_warning(&build, earlier.number); + let warnings = with_warning(&build, earlier.number, empty); update!(build { covered_by_build_id: Some(earlier.id), warnings }).await?; } return Ok(Some(false)); @@ -96,9 +162,9 @@ pub async fn follow(build: &Build) -> laravel::Result> { Ok(Some(true)) } -fn with_warning(build: &Build, number: i32) -> serde_json::Value { +fn with_warning(build: &Build, number: i32, empty: bool) -> serde_json::Value { let mut warnings: Vec = serde_json::from_value(build.warnings.clone()).unwrap_or_default(); - let warning = covered_warning(number); + let warning = if empty { covered_warning(number) } else { twin_warning(number) }; if !warnings.contains(&warning) { warnings.push(warning); } @@ -128,7 +194,7 @@ pub async fn mirror(build_id: i64, earlier: &Build) -> laravel::Result { }); let superseded_by_build_id = earlier.superseded_by_build_id; let covered_by_build_id = Some(earlier.id); - let warnings = with_warning(&build, earlier.number); + let warnings = with_warning(&build, earlier.number, sent(build.id).await? == 0); let unchanged = build.state == "completed" && build.conclusion == conclusion && build.review_state == review_state @@ -186,7 +252,7 @@ pub async fn update_followers(build_id: i64) -> laravel::Result { #[must_use] pub fn not_reviewable(number: i32) -> String { format!( - "This build was sent no snapshots: it shows the result of build #{number}, which covers the same commit. Review \ - build #{number} instead." + "This build shows the result of build #{number}, which already compared the same commit (this run sent no \ + snapshots, or the same ones). Review build #{number} instead." ) } diff --git a/app/tests/reruns.rs b/app/tests/reruns.rs index 343dff69..28122c65 100644 --- a/app/tests/reruns.rs +++ b/app/tests/reruns.rs @@ -2,7 +2,7 @@ //! build of that suite already compared, shows that build's result instead of failing as empty. use app::domain::builds::EMPTY_BUILD; -use app::domain::reruns::covered_warning; +use app::domain::reruns::{covered_warning, twin_warning}; use app::models::{Build, Organization, Project}; use app::testing::fixtures; use laravel::db; @@ -124,6 +124,7 @@ fn warnings(build: &Value) -> Vec { } const RED: [u8; 3] = [255, 0, 0]; +const BLUE: [u8; 3] = [0, 0, 255]; #[laravel::test] async fn an_empty_rerun_shows_the_result_of_the_build_that_covers_its_commit(app: Application) -> laravel::Result<()> { @@ -271,3 +272,54 @@ async fn an_empty_build_with_no_earlier_build_of_its_suite_and_commit_still_fail assert_eq!(empty["error"], json!(EMPTY_BUILD), "{empty}"); Ok(()) } + +/// A workflow that runs on both push and pull_request sends a PR's commit twice. The second build was sent exactly what +/// the first was, so it follows it instead of comparing (and charging) again; a review of the first reaches it. A build +/// sent anything different is compared as usual (case studies, 9b). +#[laravel::test] +async fn a_second_run_that_sent_the_same_snapshots_follows_the_first(app: Application) -> laravel::Result<()> { + fixtures::blob_store().await?; + let token = project_token().await?; + let pdf = |commit, nonce| Run { name: "pdf", commit, branch: "feature", nonce }; + + // #1: the push run adds a screenshot, which waits for review. #2: the pull_request run sends the same. + let push = run(&app, &token, &pdf("f1", "push-run"), &[("invoice.png", RED)]).await; + assert_eq!((&push["conclusion"], &push["review_state"]), (&json!("changes"), &json!("pending")), "{push}"); + let id = send(&app, &token, &pdf("f1", "pull-request-run"), &[("invoice.png", RED)]).await; + db::query!("UPDATE builds SET pr_number = 7 WHERE number = 2").update().await?; + let pull = finalize(&app, &token, &id, false).await; + assert_eq!( + (&pull["state"], &pull["conclusion"], &pull["review_state"]), + (&json!("completed"), &json!("changes"), &json!("pending")), + "{pull}" + ); + assert!(warnings(&pull).contains(&twin_warning(1)), "{pull}"); + let (one, two) = (build(1).await?, build(2).await?); + assert_eq!(two.covered_by_build_id, Some(one.id)); + // Its snapshots were checked as they came in, as any build's are; nothing was compared again. + assert_eq!( + (&two.charged["image_diffs"], &two.charged["rendered_pages"]), + (&json!(0), &json!(0)), + "{}", + two.charged + ); + + // Approving #1 approves #2. + let via = app::domain::reviews::Via::default(); + let approved = app::domain::reviews::review(one, None, &via, "approve", None, None).await?.expect("approved"); + app::events::BuildChanged { build_id: approved.id }.dispatch().await?; + let pull = fetch(&app, &token, &pull["id"]).await; + assert_eq!((&pull["review_state"], &pull["passed"]), (&json!("approved"), &json!(true)), "{pull}"); + + // A pull_request run that sent something else (a flaky screenshot) is compared on its own. + let id = send(&app, &token, &pdf("f1", "third-run"), &[("invoice.png", BLUE)]).await; + db::query!("UPDATE builds SET pr_number = 7 WHERE number = 3").update().await?; + let other = finalize(&app, &token, &id, false).await; + assert!(!warnings(&other).contains(&twin_warning(1)), "{other}"); + assert_eq!(build(3).await?.covered_by_build_id, None); + + // So is a re-run of the push run (#1 has no pull request either): it may follow other approvals or settings. + run(&app, &token, &pdf("f1", "push-run-again"), &[("invoice.png", RED)]).await; + assert_eq!(build(4).await?.covered_by_build_id, None); + Ok(()) +}