Skip to content

Commit

Permalink
fix: Redeploy on var value change (#211)
Browse files Browse the repository at this point in the history
  • Loading branch information
adityachoudhari26 authored Nov 11, 2024
1 parent 115a193 commit 894ed53
Show file tree
Hide file tree
Showing 6 changed files with 4,161 additions and 3 deletions.
41 changes: 38 additions & 3 deletions packages/api/src/router/deployment-variable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ import {
updateDeploymentVariable,
updateDeploymentVariableValue,
} from "@ctrlplane/db/schema";
import {
cancelOldReleaseJobTriggersOnJobDispatch,
createReleaseJobTriggers,
dispatchReleaseJobTriggers,
isPassingAllPolicies,
isPassingNoPendingJobsPolicy,
isPassingReleaseStringCheckPolicy,
} from "@ctrlplane/job-dispatch";
import { Permission } from "@ctrlplane/validators/auth";

import { createTRPCRouter, protectedProcedure } from "../trpc";
Expand Down Expand Up @@ -75,7 +83,7 @@ const valueRouter = createTRPCRouter({
z.object({ id: z.string().uuid(), data: updateDeploymentVariableValue }),
)
.mutation(async ({ ctx, input }) => {
const defaultValueId = await ctx.db
const { deploymentId, defaultValueId } = await ctx.db
.select()
.from(deploymentVariableValue)
.innerJoin(
Expand All @@ -84,9 +92,12 @@ const valueRouter = createTRPCRouter({
)
.where(eq(deploymentVariableValue.id, input.id))
.then(takeFirst)
.then((v) => v.deployment_variable.defaultValueId);
.then((v) => ({
deploymentId: v.deployment_variable.deploymentId,
defaultValueId: v.deployment_variable.defaultValueId,
}));

return ctx.db.transaction((tx) =>
const updatedValue = await ctx.db.transaction((tx) =>
tx
.update(deploymentVariableValue)
.set(input.data)
Expand All @@ -112,6 +123,30 @@ const valueRouter = createTRPCRouter({
return updatedValue;
}),
);

if (input.data.value != null)
await ctx.db.query.target
.findMany({
where: targetMatchesMetadata(ctx.db, updatedValue.targetFilter),
})
.then((targets) =>
createReleaseJobTriggers(ctx.db, "variable_changed")
.causedById(ctx.session.user.id)
.targets(targets.map((t) => t.id))
.deployments([deploymentId])
.filter(isPassingNoPendingJobsPolicy)
.filter(isPassingReleaseStringCheckPolicy)
.insert()
.then((triggers) =>
dispatchReleaseJobTriggers(ctx.db)
.releaseTriggers(triggers)
.filter(isPassingAllPolicies)
.then(cancelOldReleaseJobTriggersOnJobDispatch)
.dispatch(),
),
);

return updatedValue;
}),

delete: protectedProcedure
Expand Down
1 change: 1 addition & 0 deletions packages/db/drizzle/0034_brave_rogue.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TYPE "release_job_trigger_type" ADD VALUE 'variable_changed';
Loading

0 comments on commit 894ed53

Please sign in to comment.