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

fix: Release job agent config #289

Merged
merged 3 commits into from
Jan 22, 2025
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
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,
Comment on lines +177 to 182
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Add error handling for workflow dispatch.

The workflow dispatch operation could fail, but there's no error handling.

-  await octokit.actions.createWorkflowDispatch({
-    owner: mergedConfig.owner,
-    repo: mergedConfig.repo,
-    workflow_id: mergedConfig.workflowId,
-    ref,
-    inputs: { job_id: je.id },
-    headers,
-  });
+  try {
+    await octokit.actions.createWorkflowDispatch({
+      owner: mergedConfig.owner,
+      repo: mergedConfig.repo,
+      workflow_id: mergedConfig.workflowId,
+      ref,
+      inputs: { job_id: je.id },
+      headers,
+    });
+  } catch (error) {
+    logger.error(`Failed to dispatch workflow for job ${je.id}`, { error });
+    await db
+      .update(job)
+      .set({
+        status: JobStatus.InvalidJobAgent,
+        message: `Failed to dispatch workflow: ${error.message}`,
+      })
+      .where(eq(job.id, je.id));
+    return;
+  }

Committable suggestion skipped: line range outside the PR's diff.

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
Loading