-
Notifications
You must be signed in to change notification settings - Fork 18
chore: include dependencies on version post endpoint #1087
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -3,7 +3,9 @@ import { ApiError, asyncHandler } from "@/types/api.js"; | |||||||||||||||||||||||||||||||
| import { evaluate } from "cel-js"; | ||||||||||||||||||||||||||||||||
| import { Router } from "express"; | ||||||||||||||||||||||||||||||||
| import { v4 as uuidv4 } from "uuid"; | ||||||||||||||||||||||||||||||||
| import { z } from "zod"; | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| import type { Tx } from "@ctrlplane/db"; | ||||||||||||||||||||||||||||||||
| import { and, asc, count, desc, eq, inArray, takeFirst } from "@ctrlplane/db"; | ||||||||||||||||||||||||||||||||
| import { db } from "@ctrlplane/db/client"; | ||||||||||||||||||||||||||||||||
| import { | ||||||||||||||||||||||||||||||||
|
|
@@ -14,8 +16,6 @@ import { | |||||||||||||||||||||||||||||||
| import * as schema from "@ctrlplane/db/schema"; | ||||||||||||||||||||||||||||||||
| import { getClientFor } from "@ctrlplane/workspace-engine-sdk"; | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| // 1 hour | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| import { validResourceSelector } from "../valid-selector.js"; | ||||||||||||||||||||||||||||||||
| import { listDeploymentVariablesByDeploymentRouter } from "./deployment-variables.js"; | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
|
|
@@ -377,33 +377,134 @@ const listDeploymentVersions: AsyncTypedHandler< | |||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| const assertDeploymentExistsInWorkspace = async ( | ||||||||||||||||||||||||||||||||
| workspaceId: string, | ||||||||||||||||||||||||||||||||
| deploymentId: string, | ||||||||||||||||||||||||||||||||
| ) => { | ||||||||||||||||||||||||||||||||
| const found = await db.query.deployment.findFirst({ | ||||||||||||||||||||||||||||||||
| where: and( | ||||||||||||||||||||||||||||||||
| eq(schema.deployment.id, deploymentId), | ||||||||||||||||||||||||||||||||
| eq(schema.deployment.workspaceId, workspaceId), | ||||||||||||||||||||||||||||||||
| ), | ||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||
| if (found == null) throw new ApiError("Deployment not found", 404); | ||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| const validateDependencyEntries = ( | ||||||||||||||||||||||||||||||||
| entries: [string, { versionSelector: string }][], | ||||||||||||||||||||||||||||||||
| parentDeploymentId: string, | ||||||||||||||||||||||||||||||||
| ) => { | ||||||||||||||||||||||||||||||||
| for (const [depDeploymentId, edge] of entries) { | ||||||||||||||||||||||||||||||||
| if (!z.string().uuid().safeParse(depDeploymentId).success) | ||||||||||||||||||||||||||||||||
| throw new ApiError( | ||||||||||||||||||||||||||||||||
| `Invalid dependency deployment id "${depDeploymentId}"`, | ||||||||||||||||||||||||||||||||
| 400, | ||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||
| if (depDeploymentId === parentDeploymentId) | ||||||||||||||||||||||||||||||||
| throw new ApiError( | ||||||||||||||||||||||||||||||||
| "A deployment version cannot depend on its own deployment", | ||||||||||||||||||||||||||||||||
| 400, | ||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||
| const { versionSelector: selector } = edge; | ||||||||||||||||||||||||||||||||
| if (selector.trim() === "") | ||||||||||||||||||||||||||||||||
| throw new ApiError( | ||||||||||||||||||||||||||||||||
| `Missing versionSelector for dependency ${depDeploymentId}`, | ||||||||||||||||||||||||||||||||
| 400, | ||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||
| if (!validResourceSelector(selector)) | ||||||||||||||||||||||||||||||||
| throw new ApiError( | ||||||||||||||||||||||||||||||||
| `Invalid versionSelector CEL expression for dependency ${depDeploymentId}`, | ||||||||||||||||||||||||||||||||
| 400, | ||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| const assertDependencyDeploymentsExistInWorkspace = async ( | ||||||||||||||||||||||||||||||||
| tx: Tx, | ||||||||||||||||||||||||||||||||
| workspaceId: string, | ||||||||||||||||||||||||||||||||
| dependencyDeploymentIds: string[], | ||||||||||||||||||||||||||||||||
| ) => { | ||||||||||||||||||||||||||||||||
| if (dependencyDeploymentIds.length === 0) return; | ||||||||||||||||||||||||||||||||
| const found = await tx | ||||||||||||||||||||||||||||||||
| .select({ id: schema.deployment.id }) | ||||||||||||||||||||||||||||||||
| .from(schema.deployment) | ||||||||||||||||||||||||||||||||
| .where( | ||||||||||||||||||||||||||||||||
| and( | ||||||||||||||||||||||||||||||||
| eq(schema.deployment.workspaceId, workspaceId), | ||||||||||||||||||||||||||||||||
| inArray(schema.deployment.id, dependencyDeploymentIds), | ||||||||||||||||||||||||||||||||
| ), | ||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||
| const foundIds = new Set(found.map((d) => d.id)); | ||||||||||||||||||||||||||||||||
| for (const id of dependencyDeploymentIds) { | ||||||||||||||||||||||||||||||||
| if (!foundIds.has(id)) | ||||||||||||||||||||||||||||||||
| throw new ApiError(`Dependency deployment ${id} not found`, 404); | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| const insertDeploymentVersionOrThrowConflict = async ( | ||||||||||||||||||||||||||||||||
| tx: Tx, | ||||||||||||||||||||||||||||||||
| data: typeof schema.deploymentVersion.$inferInsert, | ||||||||||||||||||||||||||||||||
| ) => { | ||||||||||||||||||||||||||||||||
| const insertedRows = await tx | ||||||||||||||||||||||||||||||||
| .insert(schema.deploymentVersion) | ||||||||||||||||||||||||||||||||
| .values(data) | ||||||||||||||||||||||||||||||||
| .onConflictDoNothing() | ||||||||||||||||||||||||||||||||
| .returning(); | ||||||||||||||||||||||||||||||||
| const inserted = insertedRows[0]; | ||||||||||||||||||||||||||||||||
| if (inserted == null) | ||||||||||||||||||||||||||||||||
| throw new ApiError( | ||||||||||||||||||||||||||||||||
| `Deployment version with tag "${data.tag}" already exists for this deployment`, | ||||||||||||||||||||||||||||||||
| 409, | ||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||
| return inserted; | ||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| const createDeploymentVersion: AsyncTypedHandler< | ||||||||||||||||||||||||||||||||
| "/v1/workspaces/{workspaceId}/deployments/{deploymentId}/versions", | ||||||||||||||||||||||||||||||||
| "post" | ||||||||||||||||||||||||||||||||
| > = async (req, res) => { | ||||||||||||||||||||||||||||||||
| const { workspaceId, deploymentId } = req.params; | ||||||||||||||||||||||||||||||||
| const { body } = req; | ||||||||||||||||||||||||||||||||
| const { dependencies, ...versionFields } = body; | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
| const deployment = await db | |
| .select({ id: schema.deployment.id }) | |
| .from(schema.deployment) | |
| .where( | |
| and( | |
| eq(schema.deployment.workspaceId, workspaceId), | |
| eq(schema.deployment.id, deploymentId), | |
| ), | |
| ) | |
| .then(takeFirst); | |
| if (!deployment) | |
| throw new ApiError(`Deployment ${deploymentId} not found`, 404); |
Uh oh!
There was an error while loading. Please reload this page.