Skip to content

Commit

Permalink
fix: Job output includes release metadata (#258)
Browse files Browse the repository at this point in the history
  • Loading branch information
adityachoudhari26 authored Dec 10, 2024
1 parent c41c77e commit b6e3648
Show file tree
Hide file tree
Showing 4 changed files with 1,837 additions and 1,790 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import type { NextRequest } from "next/server";
import { NextResponse } from "next/server";
import _ from "lodash";
import { isPresent } from "ts-is-present";

import { and, eq, isNull, notInArray } from "@ctrlplane/db";
import { db } from "@ctrlplane/db/client";
Expand All @@ -9,6 +11,7 @@ import {
job,
release,
releaseJobTrigger,
releaseMetadata,
resource,
} from "@ctrlplane/db/schema";

Expand All @@ -29,6 +32,7 @@ export const GET = async (
.leftJoin(environment, eq(environment.id, releaseJobTrigger.environmentId))
.leftJoin(resource, eq(resource.id, releaseJobTrigger.resourceId))
.leftJoin(release, eq(release.id, releaseJobTrigger.releaseId))
.leftJoin(releaseMetadata, eq(releaseMetadata.releaseId, release.id))
.leftJoin(deployment, eq(deployment.id, release.deploymentId))
.where(
and(
Expand All @@ -44,14 +48,25 @@ export const GET = async (
),
)
.then((rows) =>
rows.map((row) => ({
...row.job,
config: row.release_job_trigger,
environment: row.environment,
target: row.resource,
deployment: row.deployment,
release: row.release,
})),
_.chain(rows)
.groupBy((row) => row.job.id)
.map((jobRows) => ({
...jobRows[0]!.job,
config: jobRows[0]!.release_job_trigger,
environment: jobRows[0]!.environment,
target: jobRows[0]!.resource,
deployment: jobRows[0]!.deployment,
release:
jobRows[0]!.release != null
? {
...jobRows[0]!.release,
metadata: jobRows
.map((r) => r.release_metadata)
.filter(isPresent),
}
: null,
}))
.value(),
);

return NextResponse.json(je);
Expand Down
22 changes: 18 additions & 4 deletions apps/webservice/src/app/api/v1/jobs/[jobId]/route.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { NextRequest } from "next/server";
import { NextResponse } from "next/server";
import { isPresent } from "ts-is-present";

import { and, eq, isNull, takeFirstOrNull } from "@ctrlplane/db";
import { db } from "@ctrlplane/db/client";
Expand Down Expand Up @@ -58,7 +59,7 @@ export const GET = request()
}),
)
.handle<object, { params: { jobId: string } }>(async ({ db }, { params }) => {
const row = await db
const rows = await db
.select()
.from(schema.job)
.leftJoin(
Expand All @@ -85,28 +86,41 @@ export const GET = request()
schema.release,
eq(schema.releaseJobTrigger.releaseId, schema.release.id),
)
.leftJoin(
schema.releaseMetadata,
eq(schema.release.id, schema.releaseMetadata.releaseId),
)
.leftJoin(
schema.deployment,
eq(schema.release.deploymentId, schema.deployment.id),
)
.where(
and(eq(schema.job.id, params.jobId), isNull(schema.resource.deletedAt)),
)
.then(takeFirstOrNull);
);

const row = rows.at(0);

if (row == null)
return NextResponse.json(
{ error: "Job execution not found." },
{ status: 404 },
);

const release =
row.release != null
? {
...row.release,
metadata: rows.map((r) => r.release_metadata).filter(isPresent),
}
: null;

const je = {
job: row.job,
runbook: row.runbook,
environment: row.environment,
resource: row.resource,
deployment: row.deployment,
release: row.release,
release,
};

const policyId = je.environment?.policyId;
Expand Down
5 changes: 5 additions & 0 deletions openapi.v1.json
Original file line number Diff line number Diff line change
Expand Up @@ -2355,6 +2355,10 @@
"type": "object",
"additionalProperties": true
},
"metadata": {
"type": "object",
"additionalProperties": { "type": "string" }
},
"deploymentId": {
"type": "string",
"format": "uuid"
Expand All @@ -2369,6 +2373,7 @@
"name",
"version",
"config",
"metadata",
"deploymentId",
"createdAt"
]
Expand Down
Loading

0 comments on commit b6e3648

Please sign in to comment.