-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
In order to avoid using a build if some tests have failed.
- Loading branch information
Showing
24 changed files
with
548 additions
and
57 deletions.
There are no files selected for viewing
55 changes: 55 additions & 0 deletions
55
apps/backend/db/migrations/20240901150444_build-metadata.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/** | ||
* @param {import('knex').Knex} knex | ||
*/ | ||
export const up = async (knex) => { | ||
await knex.schema.alterTable("builds", async (table) => { | ||
table.jsonb("metadata"); | ||
}); | ||
|
||
await knex.schema.alterTable("build_shards", async (table) => { | ||
table.jsonb("metadata"); | ||
}); | ||
|
||
await knex.schema.alterTable("screenshot_buckets", (table) => { | ||
table.boolean("valid"); | ||
}); | ||
|
||
await knex.raw( | ||
`UPDATE screenshot_buckets SET valid = true WHERE valid IS NULL`, | ||
); | ||
|
||
await knex.raw(` | ||
ALTER TABLE screenshot_buckets ADD CONSTRAINT screenshot_buckets_valid_not_null_constraint CHECK (valid IS NOT NULL) NOT VALID; | ||
`); | ||
|
||
await knex.raw(` | ||
ALTER TABLE screenshot_buckets VALIDATE CONSTRAINT screenshot_buckets_valid_not_null_constraint; | ||
`); | ||
|
||
await knex.raw(` | ||
ALTER TABLE screenshot_buckets ALTER column valid SET NOT NULL; | ||
`); | ||
|
||
await knex.raw(` | ||
ALTER TABLE screenshot_buckets DROP CONSTRAINT screenshot_buckets_valid_not_null_constraint; | ||
`); | ||
}; | ||
|
||
/** | ||
* @param {import('knex').Knex} knex | ||
*/ | ||
export const down = async (knex) => { | ||
await knex.schema.alterTable("builds", async (table) => { | ||
table.dropColumn("metadata"); | ||
}); | ||
|
||
await knex.schema.alterTable("build_shards", async (table) => { | ||
table.dropColumn("metadata"); | ||
}); | ||
|
||
await knex.schema.alterTable("screenshot_buckets", async (table) => { | ||
table.dropColumn("valid"); | ||
}); | ||
}; | ||
|
||
export const config = { transaction: false }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,122 @@ | ||
import { TransactionOrKnex } from "objection"; | ||
|
||
import { Build, Screenshot } from "@/database/models/index.js"; | ||
import { Build, BuildShard, Screenshot } from "@/database/models/index.js"; | ||
import { BuildMetadata } from "@/database/schemas"; | ||
|
||
/** | ||
* Check if the bucket is valid from the metadata. | ||
*/ | ||
export function checkIsBucketValidFromMetadata(metadata: BuildMetadata | null) { | ||
return !metadata?.testReport || metadata.testReport.status === "passed"; | ||
} | ||
|
||
function add(a: number | undefined, b: number | undefined) { | ||
if (!a) { | ||
return b; | ||
} | ||
if (!b) { | ||
return a; | ||
} | ||
return a + b; | ||
} | ||
|
||
/** | ||
* Aggregate metadata from multiple shards. | ||
*/ | ||
function aggregateMetadata(allMetatada: (BuildMetadata | null)[]) { | ||
return allMetatada.reduce<BuildMetadata | null>((acc, item) => { | ||
if (!item?.testReport) { | ||
return acc; | ||
} | ||
|
||
if (!acc) { | ||
return item; | ||
} | ||
|
||
const status = (() => { | ||
if (acc.testReport?.status === "failed") { | ||
return "failed"; | ||
} | ||
if (item.testReport.status === "passed") { | ||
return "passed"; | ||
} | ||
return "failed"; | ||
})(); | ||
|
||
const startTime = (() => { | ||
if (!acc.testReport?.stats?.startTime) { | ||
return item.testReport?.stats?.startTime; | ||
} | ||
if (!item.testReport?.stats?.startTime) { | ||
return acc.testReport.stats.startTime; | ||
} | ||
return item.testReport.stats.startTime < acc.testReport.stats.startTime | ||
? item.testReport.stats.startTime | ||
: acc.testReport.stats.startTime; | ||
})(); | ||
|
||
return { | ||
testReport: { | ||
status, | ||
stats: { | ||
startTime, | ||
duration: add( | ||
acc.testReport?.stats?.duration, | ||
item.testReport?.stats?.duration, | ||
), | ||
tests: add( | ||
acc.testReport?.stats?.tests, | ||
item.testReport?.stats?.tests, | ||
), | ||
expected: add( | ||
acc.testReport?.stats?.expected, | ||
item.testReport?.stats?.expected, | ||
), | ||
unexpected: add( | ||
acc.testReport?.stats?.unexpected, | ||
item.testReport?.stats?.unexpected, | ||
), | ||
}, | ||
}, | ||
}; | ||
}, null); | ||
} | ||
|
||
/** | ||
* Finalize a build. | ||
* - Count the number of screenshots in the compare bucket. | ||
* - Check if the build is considered valid. | ||
* - Update the compare bucket with the screenshot count and validity. | ||
*/ | ||
export async function finalizeBuild(input: { | ||
trx?: TransactionOrKnex; | ||
build: Build; | ||
screenshotCount?: number; | ||
}) { | ||
const screenshotCount = await Screenshot.query(input.trx) | ||
.where("screenshotBucketId", input.build.compareScreenshotBucketId) | ||
.resultSize(); | ||
await input.build | ||
.$relatedQuery("compareScreenshotBucket", input.trx) | ||
.patch({ complete: true, screenshotCount }); | ||
const { trx, build } = input; | ||
const [screenshotCount, shards] = await Promise.all([ | ||
Screenshot.query(trx) | ||
.where("screenshotBucketId", build.compareScreenshotBucketId) | ||
.resultSize(), | ||
BuildShard.query(trx).select("metadata").where("buildId", build.id), | ||
]); | ||
|
||
const valid = | ||
shards.length > 0 | ||
? shards.every((shard) => checkIsBucketValidFromMetadata(shard.metadata)) | ||
: checkIsBucketValidFromMetadata(build.metadata); | ||
|
||
const metadata = | ||
shards.length > 0 | ||
? aggregateMetadata(shards.map((shard) => shard.metadata)) | ||
: build.metadata; | ||
|
||
await Promise.all([ | ||
metadata !== build.metadata | ||
? build.$clone().$query(trx).patch({ metadata }) | ||
: null, | ||
build | ||
.$relatedQuery("compareScreenshotBucket", trx) | ||
.patch({ complete: true, screenshotCount, valid }), | ||
]); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.