Skip to content
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

fix: Convert endpoints to reference deployment version instead of release #392

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,22 @@ export const openapi: Swagger.SwaggerV3 = {
version: "1.0.0",
},
paths: {
"/v1/release-channels": {
"/v1/channels": {
post: {
summary: "Create a release channel",
operationId: "createReleaseChannel",
summary: "Create a channel",
operationId: "createChannel",
requestBody: {
required: true,
content: {
"application/json": {
schema: {
type: "object",
required: ["deploymentId", "name", "releaseFilter"],
required: ["deploymentId", "name", "versionSelector"],
properties: {
deploymentId: { type: "string" },
name: { type: "string" },
description: { type: "string", nullable: true },
releaseFilter: {
versionSelector: {
type: "object",
additionalProperties: true,
},
Expand All @@ -33,7 +33,7 @@ export const openapi: Swagger.SwaggerV3 = {
},
responses: {
"200": {
description: "Release channel created successfully",
description: "Channel created successfully",
content: {
"application/json": {
schema: {
Expand All @@ -44,7 +44,7 @@ export const openapi: Swagger.SwaggerV3 = {
name: { type: "string" },
description: { type: "string", nullable: true },
createdAt: { type: "string", format: "date-time" },
releaseFilter: {
versionSelector: {
type: "object",
additionalProperties: true,
},
Expand All @@ -55,7 +55,7 @@ export const openapi: Swagger.SwaggerV3 = {
},
},
"409": {
description: "Release channel already exists",
description: "Channel already exists",
content: {
"application/json": {
schema: {
Expand All @@ -70,7 +70,7 @@ export const openapi: Swagger.SwaggerV3 = {
},
},
"500": {
description: "Failed to create release channel",
description: "Failed to create channel",
content: {
"application/json": {
schema: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export const POST = request()
if (deploymentVersionChannel)
return NextResponse.json(
{
error: "Release channel already exists",
error: "Channel already exists",
id: deploymentVersionChannel.id,
},
{ status: 409 },
Expand Down
4 changes: 2 additions & 2 deletions apps/webservice/src/app/api/v1/config/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,10 +188,10 @@ const upsertVersions = async (db: Tx, config: CacV1, userId: string) => {
d.deployment.slug === deploymentSlug && d.system.slug === systemSlug,
);
if (deployment == null) return false;
const existingRelease = deployment.versions.find(
const existingVersion = deployment.versions.find(
(r) => r.tag === version.tag,
);
return existingRelease == null;
return existingVersion == null;
},
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ export const openapi: Swagger.SwaggerV3 = {
version: "1.0.0",
},
paths: {
"/v1/deployments/{deploymentId}/release-channels/name/{name}": {
"/v1/deployments/{deploymentId}/channels/name/{name}": {
delete: {
summary: "Delete a release channel",
operationId: "deleteReleaseChannel",
summary: "Delete a channel",
operationId: "deleteChannel",
parameters: [
{
name: "deploymentId",
Expand All @@ -27,7 +27,7 @@ export const openapi: Swagger.SwaggerV3 = {
],
responses: {
"200": {
description: "Release channel deleted",
description: "Channel deleted",
content: {
"application/json": {
schema: {
Expand All @@ -51,7 +51,7 @@ export const openapi: Swagger.SwaggerV3 = {
},
},
"404": {
description: "Release channel not found",
description: "Channel not found",
content: {
"application/json": {
schema: {
Expand All @@ -63,7 +63,7 @@ export const openapi: Swagger.SwaggerV3 = {
},
},
"500": {
description: "Failed to delete release channel",
description: "Failed to delete channel",
content: {
"application/json": {
schema: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ export const DELETE = request()
);

return NextResponse.json(
{ message: "Release channel deleted" },
{ message: "Channel deleted" },
{ status: 200 },
);
} catch {
return NextResponse.json(
{ error: "Failed to delete release channel" },
{ error: "Failed to delete channel" },
{ status: 500 },
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import * as SCHEMA from "@ctrlplane/db/schema";
import { logger } from "@ctrlplane/logger";
import { Permission } from "@ctrlplane/validators/auth";

import { authn, authz } from "../../auth";
import { request } from "../../middleware";
import { authn, authz } from "~/app/api/v1/auth";
import { request } from "~/app/api/v1/middleware";

export const GET = request()
.use(authn)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ export const openapi: Swagger.SwaggerV3 = {
openapi: "3.0.0",
info: { title: "Ctrlplane API", version: "1.0.0" },
paths: {
"/v1/releases/{releaseId}": {
"/v1/deployments/versions/{versionId}": {
patch: {
summary: "Updates a release",
operationId: "updateRelease",
summary: "Updates a deployment version",
operationId: "updateDeploymentVersion",
parameters: [
{
name: "releaseId",
name: "versionId",
in: "path",
required: true,
schema: { type: "string" },
description: "The release ID",
description: "The ID of the deployment version",
},
],
requestBody: {
Expand All @@ -26,7 +26,7 @@ export const openapi: Swagger.SwaggerV3 = {
schema: {
type: "object",
properties: {
version: { type: "string" },
tag: { type: "string" },
deploymentId: { type: "string" },
createdAt: { type: "string", format: "date-time" },
name: { type: "string" },
Expand Down Expand Up @@ -54,7 +54,7 @@ export const openapi: Swagger.SwaggerV3 = {
description: "OK",
content: {
"application/json": {
schema: { $ref: "#/components/schemas/Release" },
schema: { $ref: "#/components/schemas/DeploymentVersion" },
},
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ import {
import { logger } from "@ctrlplane/logger";
import { Permission } from "@ctrlplane/validators/auth";

import { authn, authz } from "../../auth";
import { parseBody } from "../../body-parser";
import { request } from "../../middleware";
import { authn, authz } from "~/app/api/v1/auth";
import { parseBody } from "~/app/api/v1/body-parser";
import { request } from "~/app/api/v1/middleware";

const patchSchema = SCHEMA.updateDeploymentVersion.and(
z.object({ metadata: z.record(z.string()).optional() }),
Expand All @@ -30,18 +30,18 @@ export const PATCH = request()
authz(({ can, extra: { params } }) =>
can
.perform(Permission.DeploymentVersionUpdate)
.on({ type: "deploymentVersion", id: params.releaseId }),
.on({ type: "deploymentVersion", id: params.versionId }),
),
)
.handle<
{ body: z.infer<typeof patchSchema>; user: SCHEMA.User },
{ params: { releaseId: string } }
{ params: { versionId: string } }
>(async (ctx, { params }) => {
const { releaseId: versionId } = params;
const { versionId } = params;
const { body, user, req } = ctx;

try {
const release = await ctx.db
const deploymentVersion = await ctx.db
.update(SCHEMA.deploymentVersion)
.set(body)
.where(eq(SCHEMA.deploymentVersion.id, versionId))
Expand Down Expand Up @@ -83,12 +83,12 @@ export const PATCH = request()
})
.then(() =>
logger.info(
`Version for ${versionId} job triggers created and dispatched.`,
`Jobs for deployment version ${versionId} created and dispatched.`,
req,
),
);

return NextResponse.json(release);
return NextResponse.json(deploymentVersion);
} catch (error) {
logger.error(error);
return NextResponse.json(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@ export const openapi: Swagger.SwaggerV3 = {
version: "1.0.0",
},
paths: {
"/v1/releases": {
"/v1/deployments/versions": {
post: {
summary: "Upserts a release",
operationId: "upsertRelease",
summary: "Upserts a deployment version",
operationId: "upsertDeploymentVersion",
requestBody: {
required: true,
content: {
"application/json": {
schema: {
type: "object",
properties: {
version: { type: "string" },
tag: { type: "string" },
deploymentId: { type: "string" },
createdAt: { type: "string", format: "date-time" },
name: { type: "string" },
Expand All @@ -39,7 +39,7 @@ export const openapi: Swagger.SwaggerV3 = {
additionalProperties: { type: "string" },
},
},
required: ["version", "deploymentId"],
required: ["tag", "deploymentId"],
},
},
},
Expand All @@ -49,12 +49,12 @@ export const openapi: Swagger.SwaggerV3 = {
description: "OK",
content: {
"application/json": {
schema: { $ref: "#/components/schemas/Release" },
schema: { $ref: "#/components/schemas/DeploymentVersion" },
},
},
},
"409": {
description: "Release already exists",
description: "Deployment version already exists",
content: {
"application/json": {
schema: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,14 @@ import { logger } from "@ctrlplane/logger";
import { Permission } from "@ctrlplane/validators/auth";
import { DeploymentVersionStatus } from "@ctrlplane/validators/releases";

import { authn, authz } from "../auth";
import { parseBody } from "../body-parser";
import { request } from "../middleware";
import { authn, authz } from "~/app/api/v1/auth";
import { parseBody } from "~/app/api/v1/body-parser";
import { request } from "~/app/api/v1/middleware";

const bodySchema = schema.createDeploymentVersion.omit({ tag: true }).and(
const bodySchema = schema.createDeploymentVersion.and(
z.object({
metadata: z.record(z.string()).optional(),
status: z.nativeEnum(DeploymentVersionStatus).optional(),
tag: z.string().optional(),
version: z.string().optional(),
}),
);

Expand All @@ -49,33 +47,9 @@ export const POST = request()
.handle<{ user: schema.User; body: z.infer<typeof bodySchema> }>(
async (ctx) => {
const { req, body } = ctx;
const { name, version, tag, metadata = {} } = body;
const getVersionName = () => {
if (name != null && name !== "") return name;
if (tag != null && tag !== "") return tag;
if (version != null && version !== "") return version;
return null;
};
const { name, tag, metadata = {} } = body;

const versionName = getVersionName();
if (versionName == null)
return NextResponse.json(
{ error: "Invalid version name" },
{ status: httpStatus.BAD_REQUEST },
);

const getVersionTag = () => {
if (tag != null && tag !== "") return tag;
if (version != null && version !== "") return version;
return null;
};

const versionTag = getVersionTag();
if (versionTag == null)
return NextResponse.json(
{ error: "Invalid version tag" },
{ status: httpStatus.BAD_REQUEST },
);
const versionName = name ?? tag;

try {
const prevVersion = await db
Expand All @@ -84,14 +58,14 @@ export const POST = request()
.where(
and(
eq(schema.deploymentVersion.deploymentId, body.deploymentId),
eq(schema.deploymentVersion.tag, versionTag),
eq(schema.deploymentVersion.tag, tag),
),
)
.then(takeFirstOrNull);

const depVersion = await db
.insert(schema.deploymentVersion)
.values({ ...body, name: versionName, tag: versionTag })
.values({ ...body, name: versionName, tag })
.onConflictDoUpdate({
target: [
schema.deploymentVersion.deploymentId,
Expand Down Expand Up @@ -150,7 +124,7 @@ export const POST = request()
})
.then(() =>
logger.info(
`Release for ${depVersion.id} job triggers created and dispatched.`,
`Jobs for deployment version ${depVersion.id} created and dispatched.`,
req,
),
);
Expand All @@ -166,7 +140,7 @@ export const POST = request()
{ status: httpStatus.BAD_REQUEST },
);

logger.error("Error creating release:", error);
logger.error("Error creating deployment version:", error);
return NextResponse.json(
{ error: "Internal Server Error" },
{ status: httpStatus.INTERNAL_SERVER_ERROR },
Expand Down
2 changes: 1 addition & 1 deletion apps/webservice/src/app/api/v1/environments/openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export const openapi: Swagger.SwaggerV3 = {
policyId: {
type: "string",
},
releaseChannels: {
channels: {
type: "array",
items: {
type: "string",
Expand Down
Loading
Loading