Skip to content

Commit

Permalink
backend working
Browse files Browse the repository at this point in the history
  • Loading branch information
adityachoudhari26 committed Nov 3, 2024
1 parent 00f5ab3 commit b3411b4
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 49 deletions.
15 changes: 11 additions & 4 deletions apps/event-worker/src/job-dispatch/github.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import type { Job } from "@ctrlplane/db/schema";

import { and, eq, takeFirstOrNull } from "@ctrlplane/db";
import { and, eq, or, takeFirstOrNull } from "@ctrlplane/db";
import { db } from "@ctrlplane/db/client";
import {
environment,
githubOrganization,
job,
releaseJobTrigger,
runbook,
runbookJobTrigger,
system,
workspace,
} from "@ctrlplane/db/schema";
Expand Down Expand Up @@ -37,16 +39,21 @@ export const dispatchGithubJob = async (je: Job) => {
.from(githubOrganization)
.innerJoin(workspace, eq(githubOrganization.workspaceId, workspace.id))
.innerJoin(system, eq(system.workspaceId, workspace.id))
.innerJoin(environment, eq(environment.systemId, system.id))
.innerJoin(
.leftJoin(environment, eq(environment.systemId, system.id))
.leftJoin(
releaseJobTrigger,
eq(releaseJobTrigger.environmentId, environment.id),
)
.leftJoin(runbook, eq(runbook.systemId, system.id))
.leftJoin(runbookJobTrigger, eq(runbookJobTrigger.runbookId, runbook.id))
.where(
and(
eq(githubOrganization.installationId, parsed.data.installationId),
eq(githubOrganization.organizationName, parsed.data.owner),
eq(releaseJobTrigger.jobId, je.id),
or(
eq(releaseJobTrigger.jobId, je.id),
eq(runbookJobTrigger.jobId, je.id),
),
),
)
.then(takeFirstOrNull);
Expand Down
18 changes: 10 additions & 8 deletions packages/api/src/router/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ import {
targetMatchesMetadata,
updateEnvironment,
} from "@ctrlplane/db/schema";
import { dispatchJobsForNewTargets } from "@ctrlplane/job-dispatch";
import {
dispatchJobsForNewTargets,
handleTargetsFromEnvironmentToBeDeleted,
} from "@ctrlplane/job-dispatch";
import { Permission } from "@ctrlplane/validators/auth";

import { createTRPCRouter, protectedProcedure } from "../trpc";
Expand Down Expand Up @@ -322,12 +325,11 @@ export const environmentRouter = createTRPCRouter({
})
.input(z.string().uuid())
.mutation(({ ctx, input }) =>
ctx.db.transaction((db) =>
db
.delete(environment)
.where(eq(environment.id, input))
.returning()
.then(takeFirst),
),
ctx.db
.delete(environment)
.where(eq(environment.id, input))
.returning()
.then(takeFirst)
.then((env) => handleTargetsFromEnvironmentToBeDeleted(ctx.db, env)),
),
});
12 changes: 1 addition & 11 deletions packages/job-dispatch/src/job-dispatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,16 +141,6 @@ export const dispatchRunbook = async (
.where(eq(schema.runbook.id, runbookId))
.then(takeFirst);
const job = await createTriggeredRunbookJob(db, runbook, values);
await createReleaseVariables(db, job.id)
.then(() => dispatchJobsQueue.add(job.id, { jobId: job.id }))
.catch((error) =>
db
.update(schema.job)
.set({
status: JobStatus.Failure,
message: `Failed to create release variables: ${error.message}`,
})
.where(eq(schema.job.id, job.id)),
);
await dispatchJobsQueue.add(job.id, { jobId: job.id });
return job;
};
40 changes: 14 additions & 26 deletions packages/job-dispatch/src/lifecycle-hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,20 @@ export const handleTargetsFromEnvironmentToBeDeleted = async (
.select()
.from(SCHEMA.target)
.where(SCHEMA.targetMatchesMetadata(db, env.targetFilter));

if (targets.length === 0) return;

const system = await db
.select()
.from(SCHEMA.system)
.leftJoin(
SCHEMA.environment,
eq(SCHEMA.environment.systemId, SCHEMA.system.id),
)
.where(
and(
eq(SCHEMA.system.id, env.systemId),
isNotNull(SCHEMA.environment.targetFilter),
ne(SCHEMA.environment.id, env.id),
),
)
.then((rows) => ({
...rows[0]!,
environments: rows.map((r) => r.environment).filter(isPresent),
}));
const system = await db.query.system.findFirst({
where: eq(SCHEMA.system.id, env.systemId),
with: {
environments: {
where: and(
isNotNull(SCHEMA.environment.targetFilter),
ne(SCHEMA.environment.id, env.id),
),
},
},
});
if (system == null) return;

const deploymentLifecycleHooks = await db
.select()
Expand All @@ -49,9 +42,8 @@ export const handleTargetsFromEnvironmentToBeDeleted = async (
SCHEMA.deployment,
eq(SCHEMA.deploymentLifecycleHook.deploymentId, SCHEMA.deployment.id),
)
.where(eq(SCHEMA.deployment.systemId, system.system.id))
.where(eq(SCHEMA.deployment.systemId, system.id))
.then((rows) => rows.map((r) => r.deployment_lifecycle_hook));

if (deploymentLifecycleHooks.length === 0) return;

const envFilters = system.environments
Expand Down Expand Up @@ -80,10 +72,6 @@ export const handleTargetsFromEnvironmentToBeDeleted = async (
),
)
: targets;

console.log("removedFromSystemTargets", removedFromSystemTargets);
console.log("deploymentLifecycleHooks", deploymentLifecycleHooks);

if (removedFromSystemTargets.length === 0) return;

const handleLifecycleHooksForTargets = removedFromSystemTargets.flatMap((t) =>
Expand All @@ -92,7 +80,7 @@ export const handleTargetsFromEnvironmentToBeDeleted = async (
targetId: t.id,
deploymentId: dlh.deploymentId,
environmentId: env.id,
systemId: system.system.id,
systemId: system.id,
};

return dispatchRunbook(db, dlh.runbookId, values);
Expand Down

0 comments on commit b3411b4

Please sign in to comment.