CodeIssuesDiscussionsWikiPull RequestsProjectsCommitsActionsReleasesContributorsPulse● GatesSecuritySettingsDeploymentsPipelineInsightsAgents✨ Explain✨ Ask AI✨ Workspace✨ Spec✨ Tests▓ Debt Map✨ NL Search🏛 Archaeology
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 | /**
* Workflow artifact helpers (Block C1 / Sprint 1 — Agent 6).
*
* Pure functions for uploading, listing, downloading and deleting workflow
* run artifacts. Shared between the REST API (`src/routes/workflow-artifacts.ts`)
* and in-process action handlers (e.g. `gluecron/upload-artifact@v1`,
* `gluecron/download-artifact@v1` — built by Agent 8).
*
* Storage contract: `workflow_artifacts.content` is declared as `text` in
* drizzle (base64-encoded bytes), even though the underlying column type is
* `bytea`. This mismatch is intentional for v1 — see the `bytea` customType
* comment in `src/db/schema.ts`. We therefore base64-encode on write and
* base64-decode on read.
*
* These functions never throw. DB/validation failures return
* `{ ok: false, error }`.
*/
import { eq } from "drizzle-orm";
import { db } from "../db";
import { workflowArtifacts } from "../db/schema";
/** 100 MiB — matches the REST API cap. */
export const MAX_ARTIFACT_BYTES = 100 * 1024 * 1024;
const NAME_RE = /^[A-Za-z0-9._-]+$/;
function toBuffer(input: Uint8Array | Buffer): Buffer {
if (Buffer.isBuffer(input)) return input;
return Buffer.from(input.buffer, input.byteOffset, input.byteLength);
}
export async function uploadArtifact(args: {
runId: string;
jobId: string;
name: string;
content: Uint8Array | Buffer;
contentType?: string;
}): Promise<{ ok: true; artifactId: string } | { ok: false; error: string }> {
const { runId, jobId, name, content } = args;
const contentType = args.contentType || "application/octet-stream";
if (!runId || typeof runId !== "string") {
return { ok: false, error: "runId is required" };
}
if (!jobId || typeof jobId !== "string") {
return { ok: false, error: "jobId is required" };
}
if (!name || typeof name !== "string") {
return { ok: false, error: "name is required" };
}
if (name.length > 255) {
return { ok: false, error: "name too long (max 255 chars)" };
}
if (!NAME_RE.test(name)) {
return {
ok: false,
error: "name must match /^[A-Za-z0-9._-]+$/",
};
}
const buf = toBuffer(content);
if (buf.byteLength > MAX_ARTIFACT_BYTES) {
return { ok: false, error: "artifact exceeds 100MB limit" };
}
try {
const [row] = await db
.insert(workflowArtifacts)
.values({
runId,
jobId,
name,
sizeBytes: buf.byteLength,
contentType,
// Stored as base64 text for v1 (see schema comment).
content: buf.toString("base64"),
})
.returning({ id: workflowArtifacts.id });
if (!row) {
return { ok: false, error: "insert returned no row" };
}
return { ok: true, artifactId: row.id };
} catch (err) {
console.error("[workflow-artifacts] uploadArtifact:", err);
return { ok: false, error: "database error" };
}
}
export async function listArtifacts(
runId: string
): Promise<
| {
ok: true;
artifacts: {
id: string;
name: string;
size: number;
contentType: string;
createdAt: Date;
}[];
}
| { ok: false; error: string }
> {
if (!runId || typeof runId !== "string") {
return { ok: false, error: "runId is required" };
}
try {
const rows = await db
.select({
id: workflowArtifacts.id,
name: workflowArtifacts.name,
size: workflowArtifacts.sizeBytes,
contentType: workflowArtifacts.contentType,
createdAt: workflowArtifacts.createdAt,
})
.from(workflowArtifacts)
.where(eq(workflowArtifacts.runId, runId));
return { ok: true, artifacts: rows };
} catch (err) {
console.error("[workflow-artifacts] listArtifacts:", err);
return { ok: false, error: "database error" };
}
}
export async function downloadArtifact(
artifactId: string
): Promise<
| { ok: true; name: string; contentType: string; content: Buffer }
| { ok: false; error: string }
> {
if (!artifactId || typeof artifactId !== "string") {
return { ok: false, error: "artifactId is required" };
}
try {
const [row] = await db
.select()
.from(workflowArtifacts)
.where(eq(workflowArtifacts.id, artifactId))
.limit(1);
if (!row) {
return { ok: false, error: "not found" };
}
const raw = row.content;
const buf = raw ? Buffer.from(raw, "base64") : Buffer.alloc(0);
return {
ok: true,
name: row.name,
contentType: row.contentType,
content: buf,
};
} catch (err) {
console.error("[workflow-artifacts] downloadArtifact:", err);
return { ok: false, error: "database error" };
}
}
export async function deleteArtifact(
artifactId: string
): Promise<{ ok: true } | { ok: false; error: string }> {
if (!artifactId || typeof artifactId !== "string") {
return { ok: false, error: "artifactId is required" };
}
try {
const res = await db
.delete(workflowArtifacts)
.where(eq(workflowArtifacts.id, artifactId))
.returning({ id: workflowArtifacts.id });
if (res.length === 0) {
return { ok: false, error: "not found" };
}
return { ok: true };
} catch (err) {
console.error("[workflow-artifacts] deleteArtifact:", err);
return { ok: false, error: "database error" };
}
}
/**
* Internal helper for the REST layer: returns just the owning repositoryId
* for a run. Kept here so both the API route and any future helpers share
* the same lookup without reaching into `workflowRuns` directly from N places.
*/
export async function getRunRepositoryId(
runId: string
): Promise<string | null> {
try {
const { workflowRuns } = await import("../db/schema");
const [row] = await db
.select({ repositoryId: workflowRuns.repositoryId })
.from(workflowRuns)
.where(eq(workflowRuns.id, runId))
.limit(1);
return row ? row.repositoryId : null;
} catch (err) {
console.error("[workflow-artifacts] getRunRepositoryId:", err);
return null;
}
}
/**
* Internal helper: look up a single artifact's runId (used by GET/DELETE
* endpoints that only receive `:artifactId`).
*/
export async function getArtifactRunId(
artifactId: string
): Promise<string | null> {
try {
const [row] = await db
.select({ runId: workflowArtifacts.runId })
.from(workflowArtifacts)
.where(eq(workflowArtifacts.id, artifactId))
.limit(1);
return row ? row.runId : null;
} catch (err) {
console.error("[workflow-artifacts] getArtifactRunId:", err);
return null;
}
}
|