diff --git a/packages/api/src/router/release.ts b/packages/api/src/router/release.ts index ff00cd2e..5676938b 100644 --- a/packages/api/src/router/release.ts +++ b/packages/api/src/router/release.ts @@ -108,6 +108,7 @@ export const releaseRouter = createTRPCRouter({ .select() .from(releaseJobTrigger) .innerJoin(job, eq(releaseJobTrigger.jobId, job.id)) + .innerJoin(release, eq(releaseJobTrigger.releaseId, release.id)) .innerJoin( resource, and( diff --git a/packages/api/src/router/runbook.ts b/packages/api/src/router/runbook.ts index 1a70e8cc..7369a3c3 100644 --- a/packages/api/src/router/runbook.ts +++ b/packages/api/src/router/runbook.ts @@ -179,7 +179,7 @@ export const runbookRouter = createTRPCRouter({ .where( and( eq(SCHEMA.runbookJobTrigger.runbookId, input.runbookId), - SCHEMA.jobMatchesCondition(ctx.db, input.filter), + SCHEMA.runbookJobMatchesCondition(ctx.db, input.filter), ), ) .orderBy(desc(SCHEMA.job.createdAt)) @@ -214,7 +214,7 @@ export const runbookRouter = createTRPCRouter({ .where( and( eq(SCHEMA.runbookJobTrigger.runbookId, input.runbookId), - SCHEMA.jobMatchesCondition(ctx.db, input.filter), + SCHEMA.runbookJobMatchesCondition(ctx.db, input.filter), ), ) .then(takeFirst) diff --git a/packages/db/src/schema/job.ts b/packages/db/src/schema/job.ts index 38ac4e17..8d7f08f2 100644 --- a/packages/db/src/schema/job.ts +++ b/packages/db/src/schema/job.ts @@ -233,71 +233,33 @@ const buildCondition = (tx: Tx, cond: JobCondition): SQL => { if (cond.type === FilterType.CreatedAt) return buildCreatedAtCondition(cond); if (cond.type === JobFilterType.Status) return eq(job.status, cond.value); if (cond.type === JobFilterType.Deployment) - return exists( - tx - .select({ value: sql`1` }) - .from(releaseJobTrigger) - .innerJoin(release, eq(releaseJobTrigger.releaseId, release.id)) - .where( - and( - eq(release.deploymentId, cond.value), - eq(releaseJobTrigger.jobId, job.id), - ), - ) - .limit(1), - ); + return eq(release.deploymentId, cond.value); if (cond.type === JobFilterType.Environment) - return exists( - tx - .select({ value: sql`1` }) - .from(releaseJobTrigger) - .where( - and( - eq(releaseJobTrigger.environmentId, cond.value), - eq(releaseJobTrigger.jobId, job.id), - ), - ) - .limit(1), - ); - if (cond.type === FilterType.Version) - return exists( - tx - .select({ value: sql`1` }) - .from(releaseJobTrigger) - .innerJoin(release, eq(releaseJobTrigger.releaseId, release.id)) - .where( - and(eq(releaseJobTrigger.jobId, job.id), buildVersionCondition(cond)), - ) - .limit(1), - ); + return eq(releaseJobTrigger.environmentId, cond.value); + if (cond.type === FilterType.Version) return buildVersionCondition(cond); if (cond.type === JobFilterType.JobTarget) - return exists( - tx - .select({ value: sql`1` }) - .from(releaseJobTrigger) - .innerJoin(resource, eq(releaseJobTrigger.resourceId, resource.id)) - .where( - and( - eq(releaseJobTrigger.jobId, job.id), - eq(releaseJobTrigger.resourceId, cond.value), - isNull(resource.deletedAt), - ), - ) - .limit(1), - ); - if (cond.type === JobFilterType.Release) - return exists( - tx - .select({ value: sql`1` }) - .from(releaseJobTrigger) - .where( - and( - eq(releaseJobTrigger.jobId, job.id), - eq(releaseJobTrigger.releaseId, cond.value), - ), - ) - .limit(1), - ); + return and(eq(resource.id, cond.value), isNull(resource.deletedAt))!; + if (cond.type === JobFilterType.Release) return eq(release.id, cond.value); + + const subCon = cond.conditions.map((c) => buildCondition(tx, c)); + const con = + cond.operator === ComparisonOperator.And ? and(...subCon)! : or(...subCon)!; + return cond.not ? not(con) : con; +}; + +const buildRunbookCondition = (tx: Tx, cond: JobCondition): SQL | undefined => { + if ( + cond.type !== FilterType.Metadata && + cond.type !== FilterType.CreatedAt && + cond.type !== JobFilterType.Status && + cond.type !== FilterType.Comparison + ) + return undefined; + + if (cond.type === FilterType.Metadata) + return buildMetadataCondition(tx, cond); + if (cond.type === FilterType.CreatedAt) return buildCreatedAtCondition(cond); + if (cond.type === JobFilterType.Status) return eq(job.status, cond.value); const subCon = cond.conditions.map((c) => buildCondition(tx, c)); const con = @@ -313,3 +275,12 @@ export function jobMatchesCondition( ? undefined : buildCondition(tx, condition); } + +export function runbookJobMatchesCondition( + tx: Tx, + condition?: JobCondition, +): SQL | undefined { + return condition == null || Object.keys(condition).length === 0 + ? undefined + : buildRunbookCondition(tx, condition); +}