Skip to content

Commit

Permalink
fix: Release job agent config (#289)
Browse files Browse the repository at this point in the history
  • Loading branch information
adityachoudhari26 authored Jan 22, 2025
1 parent 1671c5e commit 73a6ca6
Show file tree
Hide file tree
Showing 6 changed files with 4,636 additions and 10 deletions.
33 changes: 23 additions & 10 deletions apps/event-worker/src/job-dispatch/github.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import type { Job } from "@ctrlplane/db/schema";
import _ from "lodash";

import { and, eq, takeFirstOrNull } from "@ctrlplane/db";
import { db } from "@ctrlplane/db/client";
import {
environment,
githubEntity,
job,
release,
releaseJobTrigger,
runbook,
runbookJobTrigger,
Expand Down Expand Up @@ -68,6 +70,15 @@ const getGithubEntity = async (
);
};

const getReleaseJobAgentConfig = (jobId: string) =>
db
.select({ jobAgentConfig: release.jobAgentConfig })
.from(release)
.innerJoin(releaseJobTrigger, eq(releaseJobTrigger.releaseId, release.id))
.where(eq(releaseJobTrigger.jobId, jobId))
.then(takeFirstOrNull)
.then((r) => r?.jobAgentConfig);

export const dispatchGithubJob = async (je: Job) => {
logger.info(`Dispatching github job ${je.id}...`);

Expand All @@ -88,11 +99,13 @@ export const dispatchGithubJob = async (je: Job) => {
}

const { data: parsedConfig } = parsed;
const releaseJobAgentConfig = await getReleaseJobAgentConfig(je.id);
const mergedConfig = _.merge(parsedConfig, releaseJobAgentConfig);

const ghEntity = await getGithubEntity(
je.id,
parsedConfig.installationId,
parsedConfig.owner,
mergedConfig.installationId,
mergedConfig.owner,
);
if (ghEntity == null) {
await db
Expand Down Expand Up @@ -129,9 +142,9 @@ export const dispatchGithubJob = async (je: Job) => {
};

const ref =
parsedConfig.ref ??
mergedConfig.ref ??
(await octokit.rest.repos
.get({ ...parsedConfig, headers })
.get({ ...mergedConfig, headers })
.then((r) => r.data.default_branch)
.catch((e) => {
logger.error(`Failed to get ref for github action job ${je.id}`, {
Expand All @@ -153,17 +166,17 @@ export const dispatchGithubJob = async (je: Job) => {
}

logger.info(`Creating workflow dispatch for job ${je.id}...`, {
owner: parsedConfig.owner,
repo: parsedConfig.repo,
workflow_id: parsedConfig.workflowId,
owner: mergedConfig.owner,
repo: mergedConfig.repo,
workflow_id: mergedConfig.workflowId,
ref,
inputs: { job_id: je.id },
});

await octokit.actions.createWorkflowDispatch({
owner: parsedConfig.owner,
repo: parsedConfig.repo,
workflow_id: parsedConfig.workflowId,
owner: mergedConfig.owner,
repo: mergedConfig.repo,
workflow_id: mergedConfig.workflowId,
ref,
inputs: { job_id: je.id },
headers,
Expand Down
1 change: 1 addition & 0 deletions packages/db/drizzle/0062_right_the_watchers.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE "release" ADD COLUMN "job_agent_config" jsonb DEFAULT '{}' NOT NULL;
Loading

0 comments on commit 73a6ca6

Please sign in to comment.