Skip to content

Commit

Permalink
fix: Update release endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
adityachoudhari26 committed Jan 13, 2025
1 parent c86aa22 commit 16a34bf
Show file tree
Hide file tree
Showing 4 changed files with 276 additions and 0 deletions.
87 changes: 87 additions & 0 deletions apps/webservice/src/app/api/v1/releases/[releaseId]/openapi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import type { Swagger } from "atlassian-openapi";

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

export const openapi: Swagger.SwaggerV3 = {
openapi: "3.0.0",
info: { title: "Ctrlplane API", version: "1.0.0" },
paths: {
"/v1/releases/{releaseId}": {
patch: {
summary: "Updates a release",
operationId: "updateRelease",
parameters: [
{
name: "releaseId",
in: "path",
required: true,
schema: { type: "string" },
description: "The release ID",
},
],
requestBody: {
required: true,
content: {
"application/json": {
schema: {
type: "object",
properties: {
version: { type: "string" },
deploymentId: { type: "string" },
createdAt: { type: "string", format: "date-time" },
name: { type: "string" },
config: { type: "object", additionalProperties: true },
status: {
type: "string",
enum: Object.values(ReleaseStatus),
},
message: { type: "string" },
metadata: {
type: "object",
additionalProperties: { type: "string" },
},
},
},
},
},
},
responses: {
"200": {
description: "OK",
content: {
"application/json": {
schema: {
type: "object",
properties: {
id: { type: "string" },
version: { type: "string" },
deploymentId: { type: "string" },
createdAt: { type: "string", format: "date-time" },
name: { type: "string" },
config: { type: "object", additionalProperties: true },
status: { type: "string" },
message: { type: "string" },
metadata: {
type: "object",
additionalProperties: { type: "string" },
},
},
required: [
"id",
"version",
"deploymentId",
"createdAt",
"name",
"config",
"status",
"metadata",
],
},
},
},
},
},
},
},
},
};
69 changes: 69 additions & 0 deletions apps/webservice/src/app/api/v1/releases/[releaseId]/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
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 { 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.updateRelease.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.ReleaseUpdate)
.on({ type: "release", id: params.releaseId }),
),
)
.handle<
{ body: z.infer<typeof patchSchema> },
{ params: { releaseId: string } }
>(async (ctx, { params }) => {
const { releaseId } = params;
const { body } = ctx;

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

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

return NextResponse.json(release);
} catch (error) {
logger.error(error);
return NextResponse.json(
{ error: "Failed to update release" },
{ status: httpStatus.INTERNAL_SERVER_ERROR },
);
}
});
119 changes: 119 additions & 0 deletions openapi.v1.json
Original file line number Diff line number Diff line change
Expand Up @@ -1312,6 +1312,125 @@
]
}
},
"/v1/releases/{releaseId}": {
"patch": {
"summary": "Updates a release",
"operationId": "updateRelease",
"parameters": [
{
"name": "releaseId",
"in": "path",
"required": true,
"schema": {
"type": "string"
},
"description": "The release ID"
}
],
"requestBody": {
"required": true,
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": {
"version": {
"type": "string"
},
"deploymentId": {
"type": "string"
},
"createdAt": {
"type": "string",
"format": "date-time"
},
"name": {
"type": "string"
},
"config": {
"type": "object",
"additionalProperties": true
},
"status": {
"type": "string",
"enum": [
"ready",
"building",
"failed"
]
},
"message": {
"type": "string"
},
"metadata": {
"type": "object",
"additionalProperties": {
"type": "string"
}
}
}
}
}
}
},
"responses": {
"200": {
"description": "OK",
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": {
"id": {
"type": "string"
},
"version": {
"type": "string"
},
"deploymentId": {
"type": "string"
},
"createdAt": {
"type": "string",
"format": "date-time"
},
"name": {
"type": "string"
},
"config": {
"type": "object",
"additionalProperties": true
},
"status": {
"type": "string"
},
"message": {
"type": "string"
},
"metadata": {
"type": "object",
"additionalProperties": {
"type": "string"
}
}
},
"required": [
"id",
"version",
"deploymentId",
"createdAt",
"name",
"config",
"status",
"metadata"
]
}
}
}
}
}
}
},
"/v1/releases": {
"post": {
"summary": "Upserts a release",
Expand Down
1 change: 1 addition & 0 deletions packages/db/src/schema/release.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ export const createRelease = createInsertSchema(release, {
});

export const updateRelease = createRelease.partial();
export type UpdateRelease = z.infer<typeof updateRelease>;
export const releaseMetadata = pgTable(
"release_metadata",
{
Expand Down

0 comments on commit 16a34bf

Please sign in to comment.