From ff97dd2c14f7174006491c2e2c820721efb64159 Mon Sep 17 00:00:00 2001 From: Zachary Blasczyk <77289967+zacharyblasczyk@users.noreply.github.com> Date: Mon, 30 Sep 2024 22:47:06 -0500 Subject: [PATCH] Target Metadata for get-job-inputs (#88) --- .../src/app/api/v1/jobs/[jobId]/route.ts | 22 ++++++++++++++++++- github/get-job-inputs/index.js | 10 ++++++--- integrations/get-job-inputs/src/index.ts | 3 ++- packages/node-sdk/openapi.yaml | 11 +++++++++- .../src/models/GetJob200ResponseTarget.ts | 9 ++++++++ 5 files changed, 49 insertions(+), 6 deletions(-) diff --git a/apps/webservice/src/app/api/v1/jobs/[jobId]/route.ts b/apps/webservice/src/app/api/v1/jobs/[jobId]/route.ts index 12cc5864..03ed658e 100644 --- a/apps/webservice/src/app/api/v1/jobs/[jobId]/route.ts +++ b/apps/webservice/src/app/api/v1/jobs/[jobId]/route.ts @@ -13,6 +13,7 @@ import { runbook, runbookJobTrigger, target, + targetMetadata, updateJob, } from "@ctrlplane/db/schema"; import { onJobCompletion } from "@ctrlplane/job-dispatch"; @@ -58,7 +59,26 @@ export const GET = async ( jobVariableRows.map((v) => [v.key, v.value]), ); - return NextResponse.json({ ...je.job, ...je, variables }); + const jobTargetMetadataRows = await db + .select() + .from(targetMetadata) + .where(eq(targetMetadata.targetId, je.target?.id ?? "")); + + const metadata = Object.fromEntries( + jobTargetMetadataRows.map((m) => [m.key, m.value]), + ); + + const targetWithMetadata = { + ...je.target, + metadata, + }; + + return NextResponse.json({ + ...je.job, + ...je, + variables, + target: targetWithMetadata, + }); }; const bodySchema = updateJob; diff --git a/github/get-job-inputs/index.js b/github/get-job-inputs/index.js index ca7f567c..b34b1f7c 100644 --- a/github/get-job-inputs/index.js +++ b/github/get-job-inputs/index.js @@ -27269,6 +27269,7 @@ function instanceOfGetJob200ResponseTarget(value) { if (!("workspaceId" in value) || value["workspaceId"] === void 0) return false; if (!("config" in value) || value["config"] === void 0) return false; + if (!("metadata" in value) || value["metadata"] === void 0) return false; return true; } function GetJob200ResponseTargetFromJSON(json) { @@ -27285,7 +27286,8 @@ function GetJob200ResponseTargetFromJSONTyped(json, ignoreDiscriminator) { kind: json["kind"], identifier: json["identifier"], workspaceId: json["workspaceId"], - config: json["config"] + config: json["config"], + metadata: json["metadata"] }; } function GetJob200ResponseTargetToJSON(value) { @@ -27299,7 +27301,8 @@ function GetJob200ResponseTargetToJSON(value) { kind: value["kind"], identifier: value["identifier"], workspaceId: value["workspaceId"], - config: value["config"] + config: value["config"], + metadata: value["metadata"] }; } @@ -28043,7 +28046,7 @@ const setOutputAndLog = (key, value) => { const setOutputsRecursively = (prefix, obj) => { if (typeof obj === "object" && obj !== null) { for (const [key, value] of Object.entries(obj)) { - const sanitizedKey = key.split(".").join("_"); + const sanitizedKey = key.replace(/[.\-/\s\t]+/g, "_"); const newPrefix = prefix ? `${prefix}_${sanitizedKey}` : sanitizedKey; if (typeof value === "object" && value !== null) setOutputsRecursively(newPrefix, value); @@ -28066,6 +28069,7 @@ async function run() { setOutputAndLog("target_version", target?.version); setOutputAndLog("target_identifier", target?.identifier); setOutputsRecursively("target_config", target?.config); + setOutputsRecursively("target_metadata", target?.metadata); setOutputAndLog("workspace_id", target?.workspaceId); setOutputAndLog("environment_id", environment?.id); setOutputAndLog("environment_name", environment?.name); diff --git a/integrations/get-job-inputs/src/index.ts b/integrations/get-job-inputs/src/index.ts index 65582099..d91d57ea 100644 --- a/integrations/get-job-inputs/src/index.ts +++ b/integrations/get-job-inputs/src/index.ts @@ -31,7 +31,7 @@ const setOutputAndLog = (key: string, value: any) => { const setOutputsRecursively = (prefix: string, obj: any) => { if (typeof obj === "object" && obj !== null) { for (const [key, value] of Object.entries(obj)) { - const sanitizedKey = key.split(".").join("_"); + const sanitizedKey = key.replace(/[.\-/\s\t]+/g, "_"); const newPrefix = prefix ? `${prefix}_${sanitizedKey}` : sanitizedKey; if (typeof value === "object" && value !== null) setOutputsRecursively(newPrefix, value); @@ -59,6 +59,7 @@ async function run() { setOutputAndLog("target_version", target?.version); setOutputAndLog("target_identifier", target?.identifier); setOutputsRecursively("target_config", target?.config); + setOutputsRecursively("target_metadata", target?.metadata); setOutputAndLog("workspace_id", target?.workspaceId); diff --git a/packages/node-sdk/openapi.yaml b/packages/node-sdk/openapi.yaml index 45c0ad50..0e022b31 100644 --- a/packages/node-sdk/openapi.yaml +++ b/packages/node-sdk/openapi.yaml @@ -337,8 +337,17 @@ paths: type: string config: type: object + metadata: + type: object required: - [id, name, version, kind, identifier, workspaceId, config] + - id + - name + - version + - kind + - identifier + - workspaceId + - config + - metadata environment: type: object diff --git a/packages/node-sdk/src/models/GetJob200ResponseTarget.ts b/packages/node-sdk/src/models/GetJob200ResponseTarget.ts index f3b85251..d7fb7d76 100644 --- a/packages/node-sdk/src/models/GetJob200ResponseTarget.ts +++ b/packages/node-sdk/src/models/GetJob200ResponseTarget.ts @@ -62,6 +62,12 @@ export interface GetJob200ResponseTarget { * @memberof GetJob200ResponseTarget */ config: object; + /** + * + * @type {object} + * @memberof GetJob200ResponseTarget + */ + metadata: object; } /** @@ -79,6 +85,7 @@ export function instanceOfGetJob200ResponseTarget( if (!("workspaceId" in value) || value["workspaceId"] === undefined) return false; if (!("config" in value) || value["config"] === undefined) return false; + if (!("metadata" in value) || value["metadata"] === undefined) return false; return true; } @@ -103,6 +110,7 @@ export function GetJob200ResponseTargetFromJSONTyped( identifier: json["identifier"], workspaceId: json["workspaceId"], config: json["config"], + metadata: json["metadata"], }; } @@ -120,5 +128,6 @@ export function GetJob200ResponseTargetToJSON( identifier: value["identifier"], workspaceId: value["workspaceId"], config: value["config"], + metadata: value["metadata"], }; }