-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c86aa22
commit 16a34bf
Showing
4 changed files
with
276 additions
and
0 deletions.
There are no files selected for viewing
87 changes: 87 additions & 0 deletions
87
apps/webservice/src/app/api/v1/releases/[releaseId]/openapi.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
69
apps/webservice/src/app/api/v1/releases/[releaseId]/route.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }, | ||
); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters