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
86 changes: 86 additions & 0 deletions src/app/api/v2/builds/[uuid]/metadata/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/**
* Copyright 2026 Lifecycle contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { NextRequest } from 'next/server';
import 'server/lib/dependencies';
import { createApiHandler } from 'server/lib/createApiHandler';
import { errorResponse, successResponse } from 'server/lib/response';
import BuildMetadataService, { BuildMetadataError } from 'server/services/buildMetadata';

interface RouteContext {
params: {
uuid: string;
};
}

/**
* @openapi
* /api/v2/builds/{uuid}/metadata:
* get:
* summary: Get rendered build metadata
* description: Returns build metadata with configured links rendered for the requested build.
* tags:
* - Builds
* operationId: getBuildMetadata
* parameters:
* - in: path
* name: uuid
* required: true
* schema:
* type: string
* description: The UUID of the build to render metadata for.
* responses:
* '200':
* description: Rendered build metadata.
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/BuildMetadataSuccessResponse'
* '404':
* description: Build not found.
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/ApiErrorResponse'
* '400':
* description: Invalid rendered metadata link.
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/ApiErrorResponse'
* '500':
* description: Server error.
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/ApiErrorResponse'
*/
const getHandler = async (req: NextRequest, { params }: RouteContext) => {
const service = new BuildMetadataService();

try {
const metadata = await service.renderMetadataForBuildUUID(params.uuid);
return successResponse(metadata, { status: 200 }, req);
} catch (error) {
if (error instanceof BuildMetadataError) {
return errorResponse(error, { status: error.code === 'not_found' ? 404 : 400 }, req);
}

throw error;
}
};

export const GET = createApiHandler(getHandler);
143 changes: 143 additions & 0 deletions src/app/api/v2/config/metadata/[id]/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
/**
* Copyright 2026 Lifecycle contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { NextRequest, NextResponse } from 'next/server';
import 'server/lib/dependencies';
import { createApiHandler } from 'server/lib/createApiHandler';
import { errorResponse, successResponse } from 'server/lib/response';
import BuildMetadataService, { BuildMetadataError } from 'server/services/buildMetadata';

interface RouteContext {
params: {
id: string;
};
}

function mapMetadataError(error: unknown, req: NextRequest) {
if (error instanceof BuildMetadataError) {
return errorResponse(error, { status: error.code === 'not_found' ? 404 : 400 }, req);
}

throw error;
}

async function readRequestBody(req: NextRequest): Promise<unknown> {
try {
return await req.json();
} catch {
throw new BuildMetadataError('Invalid JSON in request body.', 'invalid_input');
}
}

/**
* @openapi
* /api/v2/config/metadata/{id}:
* patch:
* summary: Update a build metadata link
* description: Updates selected fields on one configured build metadata link template.
* tags:
* - Config
* operationId: updateBuildMetadataLink
* parameters:
* - in: path
* name: id
* required: true
* schema:
* type: string
* requestBody:
* required: true
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/BuildMetadataLinkPatchRequest'
* responses:
* '200':
* description: Build metadata config after the link was updated.
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/BuildMetadataSuccessResponse'
* '400':
* description: Invalid metadata link input.
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/ApiErrorResponse'
* '403':
* description: Forbidden.
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/ApiErrorResponse'
* '404':
* description: Metadata link not found.
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/ApiErrorResponse'
* delete:
* summary: Delete a build metadata link
* description: Removes one configured build metadata link template.
* tags:
* - Config
* operationId: deleteBuildMetadataLink
* parameters:
* - in: path
* name: id
* required: true
* schema:
* type: string
* responses:
* '204':
* description: Metadata link deleted.
* '403':
* description: Forbidden.
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/ApiErrorResponse'
* '404':
* description: Metadata link not found.
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/ApiErrorResponse'
*/
const patchHandler = async (req: NextRequest, { params }: RouteContext) => {
const service = new BuildMetadataService();

try {
const body = await readRequestBody(req);
const metadata = await service.updateLink(params.id, body);
return successResponse(metadata, { status: 200 }, req);
} catch (error) {
return mapMetadataError(error, req);
}
};

const deleteHandler = async (req: NextRequest, { params }: RouteContext) => {
const service = new BuildMetadataService();

try {
await service.deleteLink(params.id);
return new NextResponse(null, { status: 204 });
} catch (error) {
return mapMetadataError(error, req);
}
};

export const PATCH = createApiHandler(patchHandler, { roles: ['admin'] });
export const DELETE = createApiHandler(deleteHandler, { roles: ['admin'] });
111 changes: 111 additions & 0 deletions src/app/api/v2/config/metadata/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/**
* Copyright 2026 Lifecycle contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { NextRequest } from 'next/server';
import 'server/lib/dependencies';
import { createApiHandler } from 'server/lib/createApiHandler';
import { errorResponse, successResponse } from 'server/lib/response';
import BuildMetadataService, { BuildMetadataError } from 'server/services/buildMetadata';

function mapMetadataError(error: unknown, req: NextRequest) {
if (error instanceof BuildMetadataError) {
return errorResponse(error, { status: error.code === 'not_found' ? 404 : 400 }, req);
}

throw error;
}

async function readRequestBody(req: NextRequest): Promise<unknown> {
try {
return await req.json();
} catch {
throw new BuildMetadataError('Invalid JSON in request body.', 'invalid_input');
}
}

/**
* @openapi
* /api/v2/config/metadata:
* get:
* summary: Get build metadata config
* description: Returns the global build metadata configuration, including unrendered link templates.
* tags:
* - Config
* operationId: getBuildMetadataConfig
* responses:
* '200':
* description: Build metadata config.
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/BuildMetadataSuccessResponse'
* '403':
* description: Forbidden.
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/ApiErrorResponse'
* post:
* summary: Create a build metadata link
* description: Adds a link template to the global build metadata configuration.
* tags:
* - Config
* operationId: createBuildMetadataLink
* requestBody:
* required: true
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/BuildMetadataLinkCreateRequest'
* responses:
* '201':
* description: Build metadata config after the link was created.
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/BuildMetadataSuccessResponse'
* '400':
* description: Invalid metadata link input.
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/ApiErrorResponse'
* '403':
* description: Forbidden.
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/ApiErrorResponse'
*/
const getHandler = async (req: NextRequest) => {
const metadata = await new BuildMetadataService().getConfig();
return successResponse(metadata, { status: 200 }, req);
};

const postHandler = async (req: NextRequest) => {
const service = new BuildMetadataService();

try {
const body = await readRequestBody(req);
const metadata = await service.createLink(body);
return successResponse(metadata, { status: 201 }, req);
} catch (error) {
return mapMetadataError(error, req);
}
};

export const GET = createApiHandler(getHandler, { roles: ['admin'] });
export const POST = createApiHandler(postHandler, { roles: ['admin'] });
9 changes: 1 addition & 8 deletions src/pages/api/v1/builds/[uuid]/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import { nanoid } from 'nanoid';
import { NextApiRequest, NextApiResponse } from 'next/types';
import { withLogContext, getLogger, LogStage } from 'server/lib/logger';
import { Build } from 'server/models';
import BuildService from 'server/services/build';
import OverrideService from 'server/services/override';

Expand All @@ -41,7 +40,6 @@ async function retrieveBuild(req: NextApiRequest, res: NextApiResponse) {
'pullRequestId',
'manifest',
'webhooksYaml',
'dashboardLinks',
'isStatic',
'namespace'
);
Expand Down Expand Up @@ -70,7 +68,7 @@ async function updateBuild(req: NextApiRequest, res: NextApiResponse, correlatio
try {
const override = new OverrideService();

const build: Build = await override.db.models.Build.query().findOne({ uuid }).withGraphFetched('pullRequest');
const build = await override.db.models.Build.query().findOne({ uuid }).withGraphFetched('pullRequest');

if (!build) {
getLogger().debug('Build not found, cannot patch uuid');
Expand Down Expand Up @@ -162,8 +160,6 @@ async function updateBuild(req: NextApiRequest, res: NextApiResponse, correlatio
* type: object
* webhooksYaml:
* type: object
* dashboardLinks:
* type: object
* isStatic:
* type: boolean
* namespace:
Expand Down Expand Up @@ -268,9 +264,6 @@ async function updateBuild(req: NextApiRequest, res: NextApiResponse, correlatio
* webhooksYaml:
* type: object
* example: {}
* dashboardLinks:
* type: object
* example: {}
* isStatic:
* type: boolean
* example: false
Expand Down
Loading
Loading