28 lines · 1.0 KB
import { repoDb } from "../scoped-db";
import { registerLocator } from "../scoped-db/locators";
import { scopedId } from "../scoped-db/ids";
import { jobDb } from "./scope";

export type ArtifactInput = { runId: string; jobId: string; name: string; size: number; r2Key: string };

/** Records an uploaded job artifact and makes it addressable by id (`/api/artifacts/:id`). */
export async function recordJobArtifact(repoId: string, artifact: ArtifactInput): Promise<string> {
  const id = scopedId(repoId);
  await repoDb(repoId).run(
    "INSERT INTO job_artifacts (id, run_id, job_id, name, size, r2_key, created_at) VALUES (?, ?, ?, ?, ?, ?, ?)",
    id,
    artifact.runId,
    artifact.jobId,
    artifact.name,
    artifact.size,
    artifact.r2Key,
    Date.now(),
  );
  await registerLocator("job_artifact", id, repoId);
  return id;
}

export async function saveJobOutputs(jobId: string, outputs: Record<string, string>): Promise<void> {
  await (await jobDb(jobId))?.run("UPDATE workflow_jobs SET outputs = ? WHERE id = ?", JSON.stringify(outputs), jobId);
}