Skip to content

Commit

Permalink
fix: Create jobs for latest valid releases on env create (#197)
Browse files Browse the repository at this point in the history
  • Loading branch information
adityachoudhari26 authored Nov 3, 2024
1 parent d8f9be3 commit b2afd96
Show file tree
Hide file tree
Showing 11 changed files with 5,301 additions and 1,060 deletions.
29 changes: 28 additions & 1 deletion apps/webservice/src/app/api/v1/environments/route.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import type { PermissionChecker } from "@ctrlplane/auth/utils";
import type { Tx } from "@ctrlplane/db";
import type { User } from "@ctrlplane/db/schema";
import { NextResponse } from "next/server";
import { isPresent } from "ts-is-present";
import { z } from "zod";

import { takeFirst } from "@ctrlplane/db";
import * as schema from "@ctrlplane/db/schema";
import { createJobsForNewEnvironment } from "@ctrlplane/job-dispatch";
import { Permission } from "@ctrlplane/validators/auth";

import { authn, authz } from "../auth";
Expand All @@ -19,6 +21,19 @@ const body = schema.createEnvironment.extend({
.optional(),
});

const createReleaseChannels = (
db: Tx,
environmentId: string,
releaseChannels: { channelId: string; deploymentId: string }[],
) =>
db.insert(schema.environmentReleaseChannel).values(
releaseChannels.map(({ channelId, deploymentId }) => ({
environmentId,
channelId,
deploymentId,
})),
);

export const POST = request()
.use(authn)
.use(parseBody(body))
Expand All @@ -41,7 +56,19 @@ export const POST = request()
})
.returning()
.then(takeFirst)
.then((environment) => NextResponse.json({ environment }))
.then(async (environment) => {
if (
isPresent(ctx.body.releaseChannels) &&
ctx.body.releaseChannels.length > 0
)
await createReleaseChannels(
ctx.db,
environment.id,
ctx.body.releaseChannels,
);
await createJobsForNewEnvironment(ctx.db, environment);
return NextResponse.json({ environment });
})
.catch(() =>
NextResponse.json(
{ error: "Failed to create environment" },
Expand Down
1 change: 1 addition & 0 deletions packages/db/drizzle/0030_easy_alex_wilder.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TYPE "release_job_trigger_type" ADD VALUE 'new_environment';
Loading

0 comments on commit b2afd96

Please sign in to comment.