-
Notifications
You must be signed in to change notification settings - Fork 11
fix: Post release channel route #201
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 |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| 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/release-channels": { | ||
| post: { | ||
| summary: "Create a release channel", | ||
| operationId: "createReleaseChannel", | ||
| requestBody: { | ||
| required: true, | ||
| content: { | ||
| "application/json": { | ||
| schema: { | ||
| type: "object", | ||
| required: ["deploymentId", "name"], | ||
| properties: { | ||
| deploymentId: { type: "string" }, | ||
| name: { type: "string" }, | ||
| description: { type: "string", nullable: true }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| responses: { | ||
| "200": { | ||
| description: "Release 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" }, | ||
| }, | ||
| required: ["id", "deploymentId", "name", "createdAt"], | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| "500": { | ||
| description: "Failed to create release 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,32 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import type { z } from "zod"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { NextResponse } from "next/server"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { takeFirst } from "@ctrlplane/db"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { createReleaseChannel } from "@ctrlplane/db/schema"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import * as SCHEMA from "@ctrlplane/db/schema"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { Permission } from "@ctrlplane/validators/auth"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { authn, authz } from "../auth"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { parseBody } from "../body-parser"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { request } from "../middleware"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| export const POST = request() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .use(authn) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .use(parseBody(createReleaseChannel)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .use( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| authz(({ ctx, can }) => | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| can | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .perform(Permission.ReleaseChannelCreate) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .on({ type: "deployment", id: ctx.body.deploymentId }), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .handle<{ body: z.infer<typeof createReleaseChannel> }>( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| async ({ db, body }) => | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| db | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .insert(SCHEMA.releaseChannel) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .values(body) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .returning() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .then(takeFirst) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .then((releaseChannel) => NextResponse.json(releaseChannel)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .catch((error) => NextResponse.json({ error }, { status: 500 })), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+23
to
+32
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Enhance error handling and add transaction support. The current implementation has several areas for improvement:
Consider applying this improvement: .handle<{ body: z.infer<typeof createReleaseChannel> }>(
- async ({ db, body }) =>
+ async ({ db, body }) => {
+ try {
+ const releaseChannel = await db.transaction(async (trx) => {
+ const [result] = await trx
+ .insert(SCHEMA.releaseChannel)
+ .values(body)
+ .returning();
+ return result;
+ });
+ return NextResponse.json(releaseChannel);
+ } catch (error) {
+ if (error.code === '23505') { // PostgreSQL unique violation
+ return NextResponse.json(
+ { message: 'Release channel already exists' },
+ { status: 409 }
+ );
+ }
+ console.error('Failed to create release channel:', error);
+ return NextResponse.json(
+ { message: 'Internal server error' },
+ { status: 500 }
+ );
+ }
+ }
- db
- .insert(SCHEMA.releaseChannel)
- .values(body)
- .returning()
- .then(takeFirst)
- .then((releaseChannel) => NextResponse.json(releaseChannel))
- .catch((error) => NextResponse.json({ error }, { status: 500 })),
);📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Refactor duplicated error response schemas into reusable components
To improve maintainability and reduce duplication, consider defining a reusable
ErrorResponseschema in thecomponentssection and referencing it in the error responses for status codes 500, 401, and 403.Apply the following changes:
Add the
ErrorResponseschema tocomponents.schemas(after line 91):Update the error responses to reference the
ErrorResponseschema.For the 500 response (lines 53-57):
For the 401 response (lines 65-69):
For the 403 response (lines 77-81):
This change centralizes the error response schema and enhances maintainability.