refactor: enhance build management by improving status handling, adding human-readable status formatting, and optimizing build detail display

This commit is contained in:
Ben Allfree
2025-11-23 17:21:09 -08:00
parent 798fe5ed58
commit da2f4f3f91
6 changed files with 545 additions and 585 deletions
+252 -259
View File
@@ -4,30 +4,38 @@ import { api } from "./_generated/api";
import { internalMutation, mutation, query } from "./_generated/server";
import modulesData from "./modules.json";
type BuildUpdateData = {
status: string;
completedAt?: number;
artifactUrl?: string;
};
/**
* Computes a stable SHA-256 hash from version, target, and flags.
* This hash uniquely identifies a build configuration based on what is actually executed.
*/
async function computeBuildHash(
version: string,
target: string,
flags: string,
version: string,
target: string,
flags: string,
): Promise<string> {
// Input is now the exact parameters used for the build
const input = JSON.stringify({
version,
target,
flags,
});
// Use Web Crypto API for SHA-256 hashing
const encoder = new TextEncoder();
const data = encoder.encode(input);
const hashBuffer = await crypto.subtle.digest("SHA-256", data);
const hashArray = Array.from(new Uint8Array(hashBuffer));
const hashHex = hashArray.map((b) => b.toString(16).padStart(2, "0")).join("");
return hashHex;
// Input is now the exact parameters used for the build
const input = JSON.stringify({
version,
target,
flags,
});
// Use Web Crypto API for SHA-256 hashing
const encoder = new TextEncoder();
const data = encoder.encode(input);
const hashBuffer = await crypto.subtle.digest("SHA-256", data);
const hashArray = Array.from(new Uint8Array(hashBuffer));
const hashHex = hashArray
.map((b) => b.toString(16).padStart(2, "0"))
.join("");
return hashHex;
}
/**
@@ -36,298 +44,283 @@ async function computeBuildHash(
* Or custom domain if R2_PUBLIC_URL is set.
*/
function getR2ArtifactUrl(buildHash: string): string {
const r2PublicUrl = process.env.R2_PUBLIC_URL;
if (r2PublicUrl) {
// Custom domain configured
return `${r2PublicUrl}/${buildHash}.uf2`;
}
// Default R2 public URL pattern (requires public bucket)
const bucketName = process.env.R2_BUCKET_NAME || "firmware-builds";
const accountId = process.env.R2_ACCOUNT_ID || "";
if (accountId) {
return `https://${bucketName}.${accountId}.r2.cloudflarestorage.com/${buildHash}.uf2`;
}
// Fallback: assume custom domain or public bucket URL
return `https://${bucketName}.r2.cloudflarestorage.com/${buildHash}.uf2`;
const r2PublicUrl = process.env.R2_PUBLIC_URL;
if (r2PublicUrl) {
// Custom domain configured
return `${r2PublicUrl}/${buildHash}.uf2`;
}
// Default R2 public URL pattern (requires public bucket)
const bucketName = process.env.R2_BUCKET_NAME || "firmware-builds";
const accountId = process.env.R2_ACCOUNT_ID || "";
if (accountId) {
return `https://${bucketName}.${accountId}.r2.cloudflarestorage.com/${buildHash}.uf2`;
}
// Fallback: assume custom domain or public bucket URL
return `https://${bucketName}.r2.cloudflarestorage.com/${buildHash}.uf2`;
}
export const triggerBuild = mutation({
args: {
profileId: v.id("profiles"),
},
handler: async (ctx, args) => {
const userId = await getAuthUserId(ctx);
if (!userId) throw new Error("Unauthorized");
args: {
profileId: v.id("profiles"),
},
handler: async (ctx, args) => {
const userId = await getAuthUserId(ctx);
if (!userId) throw new Error("Unauthorized");
const profile = await ctx.db.get(args.profileId);
if (!profile || profile.userId !== userId) {
throw new Error("Unauthorized");
}
const profile = await ctx.db.get(args.profileId);
if (!profile || profile.userId !== userId) {
throw new Error("Unauthorized");
}
// Convert config object to flags string
const flags: string[] = [];
// Convert config object to flags string
const flags: string[] = [];
// Handle Modules (Inverted Logic: Default Excluded)
for (const module of modulesData.modules) {
// If config[id] is NOT false (explicitly included), we exclude it.
if (profile.config[module.id] !== false) {
flags.push(`-D${module.id}=1`);
}
}
const flagsString = flags.join(" ");
// Handle Modules (Inverted Logic: Default Excluded)
for (const module of modulesData.modules) {
// If config[id] is NOT false (explicitly included), we exclude it.
if (profile.config[module.id] !== false) {
flags.push(`-D${module.id}=1`);
}
}
// Create build records for each target
for (const target of profile.targets) {
// Compute build hash using the generated flags
const buildHash = await computeBuildHash(
profile.version,
target,
flagsString,
);
console.log(`Computed build hash for ${target}: ${buildHash} (Flags: ${flagsString})`);
const flagsString = flags.join(" ");
// Check cache for existing build
const cached = await ctx.db
.query("buildCache")
.withIndex("by_hash_target", (q) =>
q.eq("buildHash", buildHash).eq("target", target),
)
.first();
// Create build records for each target
for (const target of profile.targets) {
// Compute build hash using the generated flags
const buildHash = await computeBuildHash(
profile.version,
target,
flagsString,
);
if (cached) {
// Use cached artifact, skip GitHub workflow
const artifactUrl = getR2ArtifactUrl(buildHash);
await ctx.db.insert("builds", {
profileId: profile._id,
target: target,
githubRunId: 0,
status: "success",
artifactUrl: artifactUrl,
logs: "Build completed from cache",
startedAt: Date.now(),
completedAt: Date.now(),
buildHash: buildHash,
});
} else {
// Not cached, proceed with normal build flow
const buildId = await ctx.db.insert("builds", {
profileId: profile._id,
target: target,
githubRunId: 0,
status: "queued",
logs: "Build queued...",
startedAt: Date.now(),
buildHash: buildHash,
});
console.log(
`Computed build hash for ${target}: ${buildHash} (Flags: ${flagsString})`,
);
// Schedule the action to dispatch GitHub workflow
await ctx.scheduler.runAfter(0, api.actions.dispatchGithubBuild, {
buildId: buildId,
target: target,
flags: flagsString,
version: profile.version,
buildHash: buildHash,
});
}
}
},
// Check cache for existing build
const cached = await ctx.db
.query("buildCache")
.withIndex("by_hash_target", (q) =>
q.eq("buildHash", buildHash).eq("target", target),
)
.first();
if (cached) {
// Use cached artifact, skip GitHub workflow
const artifactUrl = getR2ArtifactUrl(buildHash);
await ctx.db.insert("builds", {
profileId: profile._id,
target: target,
githubRunId: 0,
status: "success",
artifactUrl: artifactUrl,
startedAt: Date.now(),
completedAt: Date.now(),
buildHash: buildHash,
});
} else {
// Not cached, proceed with normal build flow
const buildId = await ctx.db.insert("builds", {
profileId: profile._id,
target: target,
githubRunId: 0,
status: "queued",
startedAt: Date.now(),
buildHash: buildHash,
});
// Schedule the action to dispatch GitHub workflow
await ctx.scheduler.runAfter(0, api.actions.dispatchGithubBuild, {
buildId: buildId,
target: target,
flags: flagsString,
version: profile.version,
buildHash: buildHash,
});
}
}
},
});
export const listByProfile = query({
args: { profileId: v.id("profiles") },
handler: async (ctx, args) => {
return await ctx.db
.query("builds")
.withIndex("by_profile", (q) => q.eq("profileId", args.profileId))
.order("desc")
.take(10);
},
args: { profileId: v.id("profiles") },
handler: async (ctx, args) => {
return await ctx.db
.query("builds")
.withIndex("by_profile", (q) => q.eq("profileId", args.profileId))
.order("desc")
.take(10);
},
});
export const get = query({
args: { buildId: v.id("builds") },
handler: async (ctx, args) => {
const userId = await getAuthUserId(ctx);
if (!userId) return null;
args: { buildId: v.id("builds") },
handler: async (ctx, args) => {
const userId = await getAuthUserId(ctx);
if (!userId) return null;
const build = await ctx.db.get(args.buildId);
if (!build) return null;
const build = await ctx.db.get(args.buildId);
if (!build) return null;
const profile = await ctx.db.get(build.profileId);
if (!profile || profile.userId !== userId) return null;
const profile = await ctx.db.get(build.profileId);
if (!profile || profile.userId !== userId) return null;
return build;
},
return build;
},
});
// Internal query to get build without auth checks (for webhooks)
export const getInternal = internalMutation({
args: { buildId: v.id("builds") },
handler: async (ctx, args) => {
return await ctx.db.get(args.buildId);
},
args: { buildId: v.id("builds") },
handler: async (ctx, args) => {
return await ctx.db.get(args.buildId);
},
});
export const deleteBuild = mutation({
args: { buildId: v.id("builds") },
handler: async (ctx, args) => {
const userId = await getAuthUserId(ctx);
if (!userId) throw new Error("Unauthorized");
args: { buildId: v.id("builds") },
handler: async (ctx, args) => {
const userId = await getAuthUserId(ctx);
if (!userId) throw new Error("Unauthorized");
const build = await ctx.db.get(args.buildId);
if (!build) throw new Error("Build not found");
const build = await ctx.db.get(args.buildId);
if (!build) throw new Error("Build not found");
const profile = await ctx.db.get(build.profileId);
if (!profile || profile.userId !== userId) {
throw new Error("Unauthorized");
}
const profile = await ctx.db.get(build.profileId);
if (!profile || profile.userId !== userId) {
throw new Error("Unauthorized");
}
await ctx.db.delete(args.buildId);
},
await ctx.db.delete(args.buildId);
},
});
export const retryBuild = mutation({
args: { buildId: v.id("builds") },
handler: async (ctx, args) => {
const userId = await getAuthUserId(ctx);
if (!userId) throw new Error("Unauthorized");
args: { buildId: v.id("builds") },
handler: async (ctx, args) => {
const userId = await getAuthUserId(ctx);
if (!userId) throw new Error("Unauthorized");
const build = await ctx.db.get(args.buildId);
if (!build) throw new Error("Build not found");
const build = await ctx.db.get(args.buildId);
if (!build) throw new Error("Build not found");
const profile = await ctx.db.get(build.profileId);
if (!profile || profile.userId !== userId) {
throw new Error("Unauthorized");
}
const profile = await ctx.db.get(build.profileId);
if (!profile || profile.userId !== userId) {
throw new Error("Unauthorized");
}
// Reset build status
await ctx.db.patch(args.buildId, {
status: "queued",
logs: "Build retry queued...",
startedAt: Date.now(),
completedAt: undefined,
});
// Reset build status
await ctx.db.patch(args.buildId, {
status: "queued",
startedAt: Date.now(),
completedAt: undefined,
});
// Convert config object to flags string
const flags: string[] = [];
// Convert config object to flags string
const flags: string[] = [];
// Handle Modules (Inverted Logic: Default Excluded)
for (const module of modulesData.modules) {
// If config[id] is NOT false (explicitly included), we exclude it.
if (profile.config[module.id] !== false) {
flags.push(`-D${module.id}=1`);
}
}
const flagsString = flags.join(" ");
// Handle Modules (Inverted Logic: Default Excluded)
for (const module of modulesData.modules) {
// If config[id] is NOT false (explicitly included), we exclude it.
if (profile.config[module.id] !== false) {
flags.push(`-D${module.id}=1`);
}
}
// Compute build hash for retry using flags
const buildHash = await computeBuildHash(
profile.version,
build.target,
flagsString,
);
console.log(`Computed retry hash: ${buildHash} (Flags: ${flagsString})`);
const flagsString = flags.join(" ");
await ctx.scheduler.runAfter(0, api.actions.dispatchGithubBuild, {
buildId: args.buildId,
target: build.target,
flags: flagsString,
version: profile.version,
buildHash: buildHash,
});
},
// Compute build hash for retry using flags
const buildHash = await computeBuildHash(
profile.version,
build.target,
flagsString,
);
console.log(`Computed retry hash: ${buildHash} (Flags: ${flagsString})`);
await ctx.scheduler.runAfter(0, api.actions.dispatchGithubBuild, {
buildId: args.buildId,
target: build.target,
flags: flagsString,
version: profile.version,
buildHash: buildHash,
});
},
});
// Internal mutation to log errors from actions
export const logBuildError = internalMutation({
args: {
buildId: v.id("builds"),
error: v.string(),
},
handler: async (ctx, args) => {
await ctx.db.patch(args.buildId, {
status: "failure",
logs: `Error triggering build: ${args.error}`,
completedAt: Date.now(),
});
},
});
// Internal mutation to append logs
export const appendLogs = internalMutation({
args: {
buildId: v.id("builds"),
logs: v.string(),
},
handler: async (ctx, args) => {
const build = await ctx.db.get(args.buildId);
if (!build) return;
await ctx.db.patch(args.buildId, {
logs: (build.logs || "") + args.logs,
});
},
args: {
buildId: v.id("builds"),
error: v.string(),
},
handler: async (ctx, args) => {
await ctx.db.patch(args.buildId, {
status: "failure",
completedAt: Date.now(),
});
},
});
// Internal mutation to update build status
export const updateBuildStatus = internalMutation({
args: {
buildId: v.id("builds"),
status: v.union(v.literal("success"), v.literal("failure")),
artifactUrl: v.optional(v.string()),
},
handler: async (ctx, args) => {
const build = await ctx.db.get(args.buildId);
if (!build) return;
args: {
buildId: v.id("builds"),
status: v.string(), // Accepts any status string value
artifactUrl: v.optional(v.string()),
},
handler: async (ctx, args) => {
const build = await ctx.db.get(args.buildId);
if (!build) return;
const updateData: any = {
status: args.status,
completedAt: Date.now(),
};
const updateData: BuildUpdateData = {
status: args.status,
};
if (args.artifactUrl) {
updateData.artifactUrl = args.artifactUrl;
}
// Only set completedAt for final statuses
if (args.status === "success" || args.status === "failure") {
updateData.completedAt = Date.now();
}
await ctx.db.patch(args.buildId, updateData);
if (args.artifactUrl) {
updateData.artifactUrl = args.artifactUrl;
}
// If build succeeded and we have buildHash, store in cache with R2 URL
if (args.status === "success" && build.buildHash && build.target) {
// Get version from profile
const profile = await ctx.db.get(build.profileId);
if (profile) {
// Construct R2 URL from hash
const artifactUrl = getR2ArtifactUrl(build.buildHash);
// Update build with R2 URL if not already set
if (!args.artifactUrl) {
updateData.artifactUrl = artifactUrl;
await ctx.db.patch(args.buildId, { artifactUrl });
}
await ctx.db.patch(args.buildId, updateData);
// Check if cache entry already exists
const existing = await ctx.db
.query("buildCache")
.withIndex("by_hash_target", (q) =>
q.eq("buildHash", build.buildHash!).eq("target", build.target),
)
.first();
// If build succeeded, store in cache with R2 URL
if (args.status === "success" && build.buildHash && build.target) {
// Get version from profile
const profile = await ctx.db.get(build.profileId);
if (profile) {
// Construct R2 URL from hash
const artifactUrl = getR2ArtifactUrl(build.buildHash);
if (!existing) {
// Store in cache
await ctx.db.insert("buildCache", {
buildHash: build.buildHash,
target: build.target,
artifactUrl: artifactUrl,
version: profile.version,
createdAt: Date.now(),
});
}
}
}
},
// Update build with R2 URL if not already set
if (!args.artifactUrl) {
await ctx.db.patch(args.buildId, { artifactUrl });
}
// Check if cache entry already exists
const existing = await ctx.db
.query("buildCache")
.withIndex("by_hash_target", (q) =>
q.eq("buildHash", build.buildHash).eq("target", build.target),
)
.first();
if (!existing) {
// Store in cache
await ctx.db.insert("buildCache", {
buildHash: build.buildHash,
target: build.target,
artifactUrl: artifactUrl,
version: profile.version,
createdAt: Date.now(),
});
}
}
}
},
});
+20 -40
View File
@@ -12,52 +12,32 @@ http.route({
method: "POST",
handler: httpAction(async (ctx, request) => {
const payload = await request.json();
// Verify signature (TODO: Add HMAC verification)
// Handle build completion from our custom workflow
if (payload.action === "completed" && payload.build_id) {
const status = payload.status === "success" ? "success" : "failure";
// Update build status - R2 URL will be constructed automatically from buildHash
// Validate build_id is present
if (!payload.build_id || !payload.status) {
return new Response("Missing build_id or status", { status: 400 });
}
// Verify build exists
const build = await ctx.runMutation(internal.builds.getInternal, {
buildId: payload.build_id,
});
if (!build) {
return new Response("Build not found", { status: 404 });
}
// Handle status updates (intermediate statuses) and completion (final statuses)
if (payload.action === "status_update" || payload.action === "completed") {
await ctx.runMutation(internal.builds.updateBuildStatus, {
buildId: payload.build_id,
status,
status: payload.status,
});
return new Response(null, { status: 200 });
}
// Legacy handling for GitHub webhook events
if (payload.action === "completed" && payload.workflow_job) {
const runId = payload.workflow_job.run_id;
const status = payload.workflow_job.conclusion;
// TODO: Update build status in database
// Need to match by profile/target since we don't have runId stored yet
console.log("Build completed:", runId, status);
}
return new Response(null, { status: 200 });
}),
});
http.route({
path: "/api/logs",
method: "POST",
handler: httpAction(async (ctx, request) => {
const { buildId, logs } = await request.json();
if (!buildId || !logs) {
return new Response("Missing buildId or logs", { status: 400 });
}
// TODO: Add some verification (e.g. secret token)
await ctx.runMutation(internal.builds.appendLogs, {
buildId,
logs,
});
return new Response(null, { status: 200 });
}),
+26 -28
View File
@@ -3,33 +3,31 @@ import { defineSchema, defineTable } from "convex/server";
import { v } from "convex/values";
export default defineSchema({
...authTables,
profiles: defineTable({
userId: v.id("users"),
name: v.string(),
targets: v.array(v.string()), // e.g. ["tbeam", "rak4631"]
config: v.any(), // JSON object for flags
version: v.string(),
updatedAt: v.number(),
}).index("by_user", ["userId"]),
...authTables,
profiles: defineTable({
userId: v.id("users"),
name: v.string(),
targets: v.array(v.string()), // e.g. ["tbeam", "rak4631"]
config: v.any(), // JSON object for flags
version: v.string(),
updatedAt: v.number(),
}).index("by_user", ["userId"]),
builds: defineTable({
profileId: v.id("profiles"),
target: v.string(),
githubRunId: v.number(),
status: v.string(), // Accepts arbitrary status strings (e.g., "queued", "checking_out", "building", "uploading", "success", "failure")
artifactUrl: v.optional(v.string()),
startedAt: v.number(),
completedAt: v.optional(v.number()),
buildHash: v.string(),
}).index("by_profile", ["profileId"]),
builds: defineTable({
profileId: v.id("profiles"),
target: v.string(),
githubRunId: v.number(),
status: v.string(), // "queued", "in_progress", "success", "failure"
artifactUrl: v.optional(v.string()),
logs: v.optional(v.string()),
startedAt: v.number(),
completedAt: v.optional(v.number()),
buildHash: v.optional(v.string()),
}).index("by_profile", ["profileId"]),
buildCache: defineTable({
buildHash: v.string(),
target: v.string(),
artifactUrl: v.string(),
version: v.string(),
createdAt: v.number(),
}).index("by_hash_target", ["buildHash", "target"]),
buildCache: defineTable({
buildHash: v.string(),
target: v.string(),
artifactUrl: v.string(),
version: v.string(),
createdAt: v.number(),
}).index("by_hash_target", ["buildHash", "target"]),
});