From 030d3b5be632207ed0b9db535b481067a331c00e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Greg=20Berg=C3=A9?= Date: Tue, 8 Oct 2024 06:05:42 +0200 Subject: [PATCH] fix: only use approved builds as baseline --- .../src/build/strategy/strategies/ci/query.ts | 40 +++++++++++++++---- 1 file changed, 32 insertions(+), 8 deletions(-) diff --git a/apps/backend/src/build/strategy/strategies/ci/query.ts b/apps/backend/src/build/strategy/strategies/ci/query.ts index 2e04af252..bbc4cf490 100644 --- a/apps/backend/src/build/strategy/strategies/ci/query.ts +++ b/apps/backend/src/build/strategy/strategies/ci/query.ts @@ -1,4 +1,8 @@ -import { Build, ScreenshotBucket } from "@/database/models/index.js"; +import { + Build, + ScreenshotBucket, + ScreenshotDiff, +} from "@/database/models/index.js"; /** * Get the base bucket for a build and a commit. @@ -30,11 +34,31 @@ export async function getBaseBucketForBuildAndCommit( * Query the base bucket from a build. */ export function queryBaseBucket(build: Build) { - return ScreenshotBucket.query().where({ - projectId: build.projectId, - name: build.name, - complete: true, - valid: true, - mode: build.mode, - }); + const approvedBuilds = Build.query() + .where("projectId", build.projectId) + .where("name", build.name) + .where("mode", build.mode) + .where("jobStatus", "complete") + .whereNot("id", build.id) + .where((qb) => { + // Reference build or check build with accepted diffs + qb.where("type", "reference").orWhere((qb) => { + qb.where("type", "check").whereExists( + ScreenshotDiff.query() + .select(1) + .whereRaw('"buildId" = builds.id') + .where("validationStatus", "accepted"), + ); + }); + }); + + return ScreenshotBucket.query() + .where({ + projectId: build.projectId, + name: build.name, + complete: true, + valid: true, + mode: build.mode, + }) + .whereIn("id", approvedBuilds); }