Skip to content

Commit a068b46

Browse files
committed
feat: add configurable build metadata
1 parent 55314f4 commit a068b46

20 files changed

Lines changed: 1154 additions & 177 deletions

File tree

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/**
2+
* Copyright 2026 Lifecycle contributors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import { NextRequest } from 'next/server';
18+
import 'server/lib/dependencies';
19+
import { createApiHandler } from 'server/lib/createApiHandler';
20+
import { errorResponse, successResponse } from 'server/lib/response';
21+
import BuildMetadataService, { BuildMetadataError } from 'server/services/buildMetadata';
22+
23+
interface RouteContext {
24+
params: {
25+
uuid: string;
26+
};
27+
}
28+
29+
/**
30+
* @openapi
31+
* /api/v2/builds/{uuid}/metadata:
32+
* get:
33+
* summary: Get rendered build metadata
34+
* description: Returns build metadata with configured links rendered for the requested build.
35+
* tags:
36+
* - Builds
37+
* operationId: getBuildMetadata
38+
* parameters:
39+
* - in: path
40+
* name: uuid
41+
* required: true
42+
* schema:
43+
* type: string
44+
* description: The UUID of the build to render metadata for.
45+
* responses:
46+
* '200':
47+
* description: Rendered build metadata.
48+
* content:
49+
* application/json:
50+
* schema:
51+
* $ref: '#/components/schemas/BuildMetadataSuccessResponse'
52+
* '404':
53+
* description: Build not found.
54+
* content:
55+
* application/json:
56+
* schema:
57+
* $ref: '#/components/schemas/ApiErrorResponse'
58+
* '400':
59+
* description: Invalid rendered metadata link.
60+
* content:
61+
* application/json:
62+
* schema:
63+
* $ref: '#/components/schemas/ApiErrorResponse'
64+
* '500':
65+
* description: Server error.
66+
* content:
67+
* application/json:
68+
* schema:
69+
* $ref: '#/components/schemas/ApiErrorResponse'
70+
*/
71+
const getHandler = async (req: NextRequest, { params }: RouteContext) => {
72+
const service = new BuildMetadataService();
73+
74+
try {
75+
const metadata = await service.renderMetadataForBuildUUID(params.uuid);
76+
return successResponse(metadata, { status: 200 }, req);
77+
} catch (error) {
78+
if (error instanceof BuildMetadataError) {
79+
return errorResponse(error, { status: error.code === 'not_found' ? 404 : 400 }, req);
80+
}
81+
82+
throw error;
83+
}
84+
};
85+
86+
export const GET = createApiHandler(getHandler);
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
/**
2+
* Copyright 2026 Lifecycle contributors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import { NextRequest, NextResponse } from 'next/server';
18+
import 'server/lib/dependencies';
19+
import { createApiHandler } from 'server/lib/createApiHandler';
20+
import { errorResponse, successResponse } from 'server/lib/response';
21+
import BuildMetadataService, { BuildMetadataError } from 'server/services/buildMetadata';
22+
23+
interface RouteContext {
24+
params: {
25+
id: string;
26+
};
27+
}
28+
29+
function mapMetadataError(error: unknown, req: NextRequest) {
30+
if (error instanceof BuildMetadataError) {
31+
return errorResponse(error, { status: error.code === 'not_found' ? 404 : 400 }, req);
32+
}
33+
34+
throw error;
35+
}
36+
37+
async function readRequestBody(req: NextRequest): Promise<unknown> {
38+
try {
39+
return await req.json();
40+
} catch {
41+
throw new BuildMetadataError('Invalid JSON in request body.', 'invalid_input');
42+
}
43+
}
44+
45+
/**
46+
* @openapi
47+
* /api/v2/config/metadata/{id}:
48+
* patch:
49+
* summary: Update a build metadata link
50+
* description: Updates selected fields on one configured build metadata link template.
51+
* tags:
52+
* - Config
53+
* operationId: updateBuildMetadataLink
54+
* parameters:
55+
* - in: path
56+
* name: id
57+
* required: true
58+
* schema:
59+
* type: string
60+
* requestBody:
61+
* required: true
62+
* content:
63+
* application/json:
64+
* schema:
65+
* $ref: '#/components/schemas/BuildMetadataLinkPatchRequest'
66+
* responses:
67+
* '200':
68+
* description: Build metadata config after the link was updated.
69+
* content:
70+
* application/json:
71+
* schema:
72+
* $ref: '#/components/schemas/BuildMetadataSuccessResponse'
73+
* '400':
74+
* description: Invalid metadata link input.
75+
* content:
76+
* application/json:
77+
* schema:
78+
* $ref: '#/components/schemas/ApiErrorResponse'
79+
* '403':
80+
* description: Forbidden.
81+
* content:
82+
* application/json:
83+
* schema:
84+
* $ref: '#/components/schemas/ApiErrorResponse'
85+
* '404':
86+
* description: Metadata link not found.
87+
* content:
88+
* application/json:
89+
* schema:
90+
* $ref: '#/components/schemas/ApiErrorResponse'
91+
* delete:
92+
* summary: Delete a build metadata link
93+
* description: Removes one configured build metadata link template.
94+
* tags:
95+
* - Config
96+
* operationId: deleteBuildMetadataLink
97+
* parameters:
98+
* - in: path
99+
* name: id
100+
* required: true
101+
* schema:
102+
* type: string
103+
* responses:
104+
* '204':
105+
* description: Metadata link deleted.
106+
* '403':
107+
* description: Forbidden.
108+
* content:
109+
* application/json:
110+
* schema:
111+
* $ref: '#/components/schemas/ApiErrorResponse'
112+
* '404':
113+
* description: Metadata link not found.
114+
* content:
115+
* application/json:
116+
* schema:
117+
* $ref: '#/components/schemas/ApiErrorResponse'
118+
*/
119+
const patchHandler = async (req: NextRequest, { params }: RouteContext) => {
120+
const service = new BuildMetadataService();
121+
122+
try {
123+
const body = await readRequestBody(req);
124+
const metadata = await service.updateLink(params.id, body);
125+
return successResponse(metadata, { status: 200 }, req);
126+
} catch (error) {
127+
return mapMetadataError(error, req);
128+
}
129+
};
130+
131+
const deleteHandler = async (req: NextRequest, { params }: RouteContext) => {
132+
const service = new BuildMetadataService();
133+
134+
try {
135+
await service.deleteLink(params.id);
136+
return new NextResponse(null, { status: 204 });
137+
} catch (error) {
138+
return mapMetadataError(error, req);
139+
}
140+
};
141+
142+
export const PATCH = createApiHandler(patchHandler, { roles: ['admin'] });
143+
export const DELETE = createApiHandler(deleteHandler, { roles: ['admin'] });
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/**
2+
* Copyright 2026 Lifecycle contributors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import { NextRequest } from 'next/server';
18+
import 'server/lib/dependencies';
19+
import { createApiHandler } from 'server/lib/createApiHandler';
20+
import { errorResponse, successResponse } from 'server/lib/response';
21+
import BuildMetadataService, { BuildMetadataError } from 'server/services/buildMetadata';
22+
23+
function mapMetadataError(error: unknown, req: NextRequest) {
24+
if (error instanceof BuildMetadataError) {
25+
return errorResponse(error, { status: error.code === 'not_found' ? 404 : 400 }, req);
26+
}
27+
28+
throw error;
29+
}
30+
31+
async function readRequestBody(req: NextRequest): Promise<unknown> {
32+
try {
33+
return await req.json();
34+
} catch {
35+
throw new BuildMetadataError('Invalid JSON in request body.', 'invalid_input');
36+
}
37+
}
38+
39+
/**
40+
* @openapi
41+
* /api/v2/config/metadata:
42+
* get:
43+
* summary: Get build metadata config
44+
* description: Returns the global build metadata configuration, including unrendered link templates.
45+
* tags:
46+
* - Config
47+
* operationId: getBuildMetadataConfig
48+
* responses:
49+
* '200':
50+
* description: Build metadata config.
51+
* content:
52+
* application/json:
53+
* schema:
54+
* $ref: '#/components/schemas/BuildMetadataSuccessResponse'
55+
* '403':
56+
* description: Forbidden.
57+
* content:
58+
* application/json:
59+
* schema:
60+
* $ref: '#/components/schemas/ApiErrorResponse'
61+
* post:
62+
* summary: Create a build metadata link
63+
* description: Adds a link template to the global build metadata configuration.
64+
* tags:
65+
* - Config
66+
* operationId: createBuildMetadataLink
67+
* requestBody:
68+
* required: true
69+
* content:
70+
* application/json:
71+
* schema:
72+
* $ref: '#/components/schemas/BuildMetadataLinkCreateRequest'
73+
* responses:
74+
* '201':
75+
* description: Build metadata config after the link was created.
76+
* content:
77+
* application/json:
78+
* schema:
79+
* $ref: '#/components/schemas/BuildMetadataSuccessResponse'
80+
* '400':
81+
* description: Invalid metadata link input.
82+
* content:
83+
* application/json:
84+
* schema:
85+
* $ref: '#/components/schemas/ApiErrorResponse'
86+
* '403':
87+
* description: Forbidden.
88+
* content:
89+
* application/json:
90+
* schema:
91+
* $ref: '#/components/schemas/ApiErrorResponse'
92+
*/
93+
const getHandler = async (req: NextRequest) => {
94+
const metadata = await new BuildMetadataService().getConfig();
95+
return successResponse(metadata, { status: 200 }, req);
96+
};
97+
98+
const postHandler = async (req: NextRequest) => {
99+
const service = new BuildMetadataService();
100+
101+
try {
102+
const body = await readRequestBody(req);
103+
const metadata = await service.createLink(body);
104+
return successResponse(metadata, { status: 201 }, req);
105+
} catch (error) {
106+
return mapMetadataError(error, req);
107+
}
108+
};
109+
110+
export const GET = createApiHandler(getHandler, { roles: ['admin'] });
111+
export const POST = createApiHandler(postHandler, { roles: ['admin'] });

‎src/pages/api/v1/builds/[uuid]/index.ts‎

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
import { nanoid } from 'nanoid';
1818
import { NextApiRequest, NextApiResponse } from 'next/types';
1919
import { withLogContext, getLogger, LogStage } from 'server/lib/logger';
20-
import { Build } from 'server/models';
2120
import BuildService from 'server/services/build';
2221
import OverrideService from 'server/services/override';
2322

@@ -41,7 +40,6 @@ async function retrieveBuild(req: NextApiRequest, res: NextApiResponse) {
4140
'pullRequestId',
4241
'manifest',
4342
'webhooksYaml',
44-
'dashboardLinks',
4543
'isStatic',
4644
'namespace'
4745
);
@@ -70,7 +68,7 @@ async function updateBuild(req: NextApiRequest, res: NextApiResponse, correlatio
7068
try {
7169
const override = new OverrideService();
7270

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

7573
if (!build) {
7674
getLogger().debug('Build not found, cannot patch uuid');
@@ -162,8 +160,6 @@ async function updateBuild(req: NextApiRequest, res: NextApiResponse, correlatio
162160
* type: object
163161
* webhooksYaml:
164162
* type: object
165-
* dashboardLinks:
166-
* type: object
167163
* isStatic:
168164
* type: boolean
169165
* namespace:
@@ -268,9 +264,6 @@ async function updateBuild(req: NextApiRequest, res: NextApiResponse, correlatio
268264
* webhooksYaml:
269265
* type: object
270266
* example: {}
271-
* dashboardLinks:
272-
* type: object
273-
* example: {}
274267
* isStatic:
275268
* type: boolean
276269
* example: false

0 commit comments

Comments
 (0)