Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Add approval and approval approver to openapi spec and get-job-status action #176

Merged
merged 9 commits into from
Oct 29, 2024
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
48 changes: 46 additions & 2 deletions apps/webservice/src/app/api/v1/jobs/[jobId]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { db } from "@ctrlplane/db/client";
import {
deployment,
environment,
environmentPolicyApproval,
job,
jobVariable,
release,
Expand All @@ -15,19 +16,54 @@ import {
target,
targetMetadata,
updateJob,
user,
} from "@ctrlplane/db/schema";
import { onJobCompletion } from "@ctrlplane/job-dispatch";
import { variablesAES256 } from "@ctrlplane/secrets";
import { JobStatus } from "@ctrlplane/validators/jobs";

import { getUser } from "~/app/api/v1/auth";

type ApprovalJoinResult = {
environment_policy_approval: typeof environmentPolicyApproval.$inferSelect;
user: typeof user.$inferSelect | null;
};

const getApprovalDetails = async (releaseId: string, policyId: string) =>
db
.select()
.from(environmentPolicyApproval)
.leftJoin(user, eq(environmentPolicyApproval.userId, user.id))
.where(
and(
eq(environmentPolicyApproval.releaseId, releaseId),
eq(environmentPolicyApproval.policyId, policyId),
),
)
.then(takeFirstOrNull)
.then(mapApprovalResponse);

const mapApprovalResponse = (row: ApprovalJoinResult | null) =>
!row
? null
: {
id: row.environment_policy_approval.id,
status: row.environment_policy_approval.status,
approver:
row.user && row.environment_policy_approval.status !== "pending"
? {
id: row.user.id,
name: row.user.name,
}
: null,
};

export const GET = async (
req: NextRequest,
{ params }: { params: { jobId: string } },
) => {
const user = await getUser(req);
if (!user)
const caller = await getUser(req);
if (!caller)
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });

const je = await db
Expand All @@ -51,6 +87,13 @@ export const GET = async (
release: row.release,
}));

const policyId = je.environment?.policyId;

const approval =
je.release?.id && policyId
? await getApprovalDetails(je.release.id, policyId)
: null;

const jobVariableRows = await db
.select()
.from(jobVariable)
Expand Down Expand Up @@ -80,6 +123,7 @@ export const GET = async (
...je,
variables,
target: targetWithMetadata,
approval,
});
};

Expand Down
79 changes: 75 additions & 4 deletions github/get-job-inputs/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 14 additions & 3 deletions integrations/github-get-job-inputs/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,15 @@ async function run() {
.then((response) => {
core.info(JSON.stringify(response, null, 2));

const { variables, target, release, environment, runbook, deployment } =
response;
const {
variables,
target,
release,
environment,
runbook,
deployment,
approval,
} = response;

setOutputAndLog("base_url", baseUrl);

Expand All @@ -75,11 +82,15 @@ async function run() {
setOutputsRecursively("release_config", release?.config);
setOutputsRecursively("release_metadata", release?.metadata);

setOutputAndLog("approval_status", approval.status);
setOutputAndLog("approval_approver_id", approval.approver?.id);
setOutputAndLog("approval_approver_name", approval.approver?.name);

setOutputAndLog("deployment_id", deployment?.id);
setOutputAndLog("deployment_name", deployment?.name);
setOutputAndLog("deployment_slug", deployment?.slug);

for (const [key, value] of Object.entries(variables ?? {})) {
for (const [key, value] of Object.entries(variables)) {
const sanitizedKey = key.replace(/[.\-/\s\t]+/g, "_");
setOutputAndLog(`variable_${sanitizedKey}`, value);
}
Expand Down
33 changes: 26 additions & 7 deletions openapi.v1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,6 @@ paths:
invalid_integration,
external_run_not_found,
]

release:
type: object
properties:
Expand All @@ -323,7 +322,6 @@ paths:
config:
type: object
required: [id, version, metadata, config]

deployment:
type: object
properties:
Expand All @@ -338,7 +336,6 @@ paths:
jobAgentId:
type: string
required: [id, version, slug, systemId, jobAgentId]

runbook:
type: object
properties:
Expand All @@ -351,7 +348,6 @@ paths:
jobAgentId:
type: string
required: [id, name, systemId, jobAgentId]

target:
type: object
properties:
Expand Down Expand Up @@ -380,7 +376,6 @@ paths:
- workspaceId
- config
- metadata

environment:
type: object
properties:
Expand All @@ -391,15 +386,39 @@ paths:
systemId:
type: string
required: [id, name, systemId]

variables:
type: object
approval:
type: object
nullable: true
properties:
id:
type: string
status:
type: string
enum: [pending, approved, rejected]
approver:
type: object
nullable: true
description: Null when status is pending, contains approver details when approved or rejected
properties:
id:
type: string
name:
type: string
required:
- id
- name
required:
- id
- status
required:
- id
- status
- createdAt
- updatedAt
- variable
- variables
- approval
patch:
summary: Update a job
operationId: updateJob
Expand Down
2 changes: 1 addition & 1 deletion packages/node-sdk/openapitools.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"v1": {
"generatorName": "typescript-fetch",
"output": "#{cwd}/src",
"glob": "../../openapi.yaml"
"glob": "../../openapi.v1.yaml"
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions packages/node-sdk/src/.openapi-generator/FILES
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ models/CreateRelease200Response.ts
models/CreateReleaseRequest.ts
models/GetAgentRunningJob200ResponseInner.ts
models/GetJob200Response.ts
models/GetJob200ResponseApproval.ts
models/GetJob200ResponseApprovalApprover.ts
models/GetJob200ResponseDeployment.ts
models/GetJob200ResponseEnvironment.ts
models/GetJob200ResponseRelease.ts
Expand Down
Loading
Loading