Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"use client";

import type { JobCondition } from "@ctrlplane/validators/jobs";
import React, { useState } from "react";
import { useParams, useRouter } from "next/navigation";
import { IconSearch } from "@tabler/icons-react";
Expand All @@ -26,7 +27,7 @@ import {
TableRow,
} from "@ctrlplane/ui/table";
import { ColumnOperator } from "@ctrlplane/validators/conditions";
import { JobCondition, JobConditionType } from "@ctrlplane/validators/jobs";
import { JobConditionType } from "@ctrlplane/validators/jobs";

import { urls } from "~/app/urls";
import { api } from "~/trpc/react";
Expand Down
116 changes: 116 additions & 0 deletions apps/webservice/src/app/api/v1/deployment-version-channels/openapi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
import type { Swagger } from "atlassian-openapi";

export const openapi: Swagger.SwaggerV3 = {
openapi: "3.0.0",
info: {
title: "Ctrlplane API",
version: "1.0.0",
},
paths: {
"/v1/deployment-version-channels": {
post: {
summary: "Create a deployment version channel",
operationId: "createDeploymentVersionChannel",
requestBody: {
required: true,
content: {
"application/json": {
schema: {
type: "object",
required: ["deploymentId", "name", "versionSelector"],
properties: {
deploymentId: { type: "string" },
name: { type: "string" },
description: { type: "string", nullable: true },
versionSelector: {
type: "object",
additionalProperties: true,
},
},
},
},
},
},
responses: {
"200": {
description: "Deployment version channel created successfully",
content: {
"application/json": {
schema: {
type: "object",
properties: {
id: { type: "string" },
deploymentId: { type: "string" },
name: { type: "string" },
description: { type: "string", nullable: true },
createdAt: { type: "string", format: "date-time" },
versionSelector: {
type: "object",
additionalProperties: true,
},
},
required: ["id", "deploymentId", "name", "createdAt"],
},
},
},
},
"409": {
description: "Deployment version channel already exists",
content: {
"application/json": {
schema: {
type: "object",
properties: {
error: { type: "string" },
id: { type: "string" },
},
required: ["error", "id"],
},
},
},
},
"500": {
description: "Failed to create deployment version channel",
content: {
"application/json": {
schema: {
type: "object",
properties: { error: { type: "string" } },
required: ["error"],
},
},
},
},
"401": {
description: "Unauthorized",
content: {
"application/json": {
schema: {
type: "object",
properties: { error: { type: "string" } },
required: ["error"],
},
},
},
},
"403": {
description: "Forbidden",
content: {
"application/json": {
schema: {
type: "object",
properties: { error: { type: "string" } },
required: ["error"],
},
},
},
},
},
security: [{ bearerAuth: [] }],
},
},
},
components: {
securitySchemes: { bearerAuth: { type: "http", scheme: "bearer" } },
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import type { z } from "zod";
import { NextResponse } from "next/server";

import { buildConflictUpdateColumns, takeFirst } from "@ctrlplane/db";
import { createDeploymentVersionChannel } from "@ctrlplane/db/schema";
import * as SCHEMA from "@ctrlplane/db/schema";
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";

const schema = createDeploymentVersionChannel;

export const POST = request()
.use(authn)
.use(parseBody(schema))
.use(
authz(({ ctx, can }) =>
can
.perform(Permission.DeploymentVersionChannelCreate)
.on({ type: "deployment", id: ctx.body.deploymentId }),
),
)
.handle<{ body: z.infer<typeof schema> }>(({ db, body }) => {
const { versionSelector } = body;

return db
.insert(SCHEMA.deploymentVersionChannel)
.values({ ...body, versionSelector })
.onConflictDoUpdate({
target: [
SCHEMA.deploymentVersionChannel.deploymentId,
SCHEMA.deploymentVersionChannel.name,
],
set: buildConflictUpdateColumns(SCHEMA.deploymentVersionChannel, [
"versionSelector",
]),
})
.returning()
.then(takeFirst)
.then((deploymentVersionChannel) =>
NextResponse.json(deploymentVersionChannel),
)
.catch((error) => {
logger.error(error);
return NextResponse.json(
{ error: "Failed to create deployment version channel" },
{ status: 500 },
);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import type { Swagger } from "atlassian-openapi";

import { DeploymentVersionStatus } from "@ctrlplane/validators/releases";

export const openapi: Swagger.SwaggerV3 = {
openapi: "3.0.0",
info: { title: "Ctrlplane API", version: "1.0.0" },
paths: {
"/v1/deployment-versions/{deploymentVersionId}": {
patch: {
summary: "Updates a deployment version",
operationId: "updateDeploymentVersion",
parameters: [
{
name: "deploymentVersionId",
in: "path",
required: true,
schema: { type: "string" },
description: "The deployment version ID",
},
],
requestBody: {
required: true,
content: {
"application/json": {
schema: {
type: "object",
properties: {
tag: { type: "string" },
deploymentId: { type: "string" },
createdAt: { type: "string", format: "date-time" },
name: { type: "string" },
config: { type: "object", additionalProperties: true },
jobAgentConfig: {
type: "object",
additionalProperties: true,
},
status: {
type: "string",
enum: Object.values(DeploymentVersionStatus),
},
message: { type: "string" },
metadata: {
type: "object",
additionalProperties: { type: "string" },
},
},
},
},
},
},
responses: {
"200": {
description: "OK",
content: {
"application/json": {
schema: { $ref: "#/components/schemas/DeploymentVersion" },
},
},
},
},
},
},
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import { NextResponse } from "next/server";
import httpStatus from "http-status";
import { z } from "zod";

import { buildConflictUpdateColumns, eq, takeFirst } from "@ctrlplane/db";
import * as SCHEMA from "@ctrlplane/db/schema";
import {
cancelOldReleaseJobTriggersOnJobDispatch,
createJobApprovals,
createReleaseJobTriggers,
dispatchReleaseJobTriggers,
isPassingAllPolicies,
isPassingChannelSelectorPolicy,
} from "@ctrlplane/job-dispatch";
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";

const patchSchema = SCHEMA.updateDeploymentVersion.and(
z.object({ metadata: z.record(z.string()).optional() }),
);

export const PATCH = request()
.use(authn)
.use(parseBody(patchSchema))
.use(
authz(({ can, extra: { params } }) =>
can
.perform(Permission.DeploymentVersionUpdate)
.on({ type: "deploymentVersion", id: params.deploymentVersionId }),
),
)
.handle<
{ body: z.infer<typeof patchSchema>; user: SCHEMA.User },
{ params: { deploymentVersionId: string } }
>(async (ctx, { params }) => {
const { deploymentVersionId } = params;
const { body, user, req } = ctx;

try {
const deploymentVersion = await ctx.db
.update(SCHEMA.deploymentVersion)
.set(body)
.where(eq(SCHEMA.deploymentVersion.id, deploymentVersionId))
.returning()
.then(takeFirst);

if (Object.keys(body.metadata ?? {}).length > 0)
await ctx.db
.insert(SCHEMA.deploymentVersionMetadata)
.values(
Object.entries(body.metadata ?? {}).map(([key, value]) => ({
versionId: deploymentVersionId,
key,
value,
})),
)
.onConflictDoUpdate({
target: [
SCHEMA.deploymentVersionMetadata.key,
SCHEMA.deploymentVersionMetadata.versionId,
],
set: buildConflictUpdateColumns(SCHEMA.deploymentVersionMetadata, [
"value",
]),
});

await createReleaseJobTriggers(ctx.db, "version_updated")
.causedById(user.id)
.filter(isPassingChannelSelectorPolicy)
.versions([deploymentVersionId])
.then(createJobApprovals)
.insert()
.then((releaseJobTriggers) => {
dispatchReleaseJobTriggers(ctx.db)
.releaseTriggers(releaseJobTriggers)
.filter(isPassingAllPolicies)
.then(cancelOldReleaseJobTriggersOnJobDispatch)
.dispatch();
})
.then(() =>
logger.info(
`Version for ${deploymentVersionId} job triggers created and dispatched.`,
req,
),
);

return NextResponse.json(deploymentVersion);
} catch (error) {
logger.error(error);
return NextResponse.json(
{ error: "Failed to update version" },
{ status: httpStatus.INTERNAL_SERVER_ERROR },
);
}
});
Loading
Loading