From b6bc602f37b9c7ac226897211f5463a4718e014b Mon Sep 17 00:00:00 2001 From: "Builder.io" Date: Wed, 17 Sep 2025 10:01:43 +0000 Subject: [PATCH] Issues model and branch/commit helpers cgen-5978319a6b2d4949aeabd9574097ced7 --- website/src/website/switch/logic.ts | 71 +++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 website/src/website/switch/logic.ts diff --git a/website/src/website/switch/logic.ts b/website/src/website/switch/logic.ts new file mode 100644 index 00000000..11ec7c28 --- /dev/null +++ b/website/src/website/switch/logic.ts @@ -0,0 +1,71 @@ +import { get, getAllByIndex, put, del, getSetting, setSetting, type RepoRecord, type BranchRecord, type CommitRecord, type IssueRecord } from "./db"; +import { nanoid } from "./uid"; + +const curBranchKey = (repoId: string) => `repo:${repoId}:currentBranchId`; + +export async function getCurrentBranchId(repoId: string): Promise { + return getSetting(curBranchKey(repoId)); +} + +export async function setCurrentBranchId(repoId: string, branchId: string): Promise { + await setSetting(curBranchKey(repoId), branchId); +} + +export async function getBranch(repoId: string, name: string): Promise { + const branches = await getAllByIndex("branches", "by_repo", repoId); + return branches.find(b => b.name === name); +} + +export async function listCommitsReachable(repoId: string, headCommitId?: string): Promise { + const all = await getAllByIndex("commits", "by_repo", repoId); + if (!headCommitId) return []; + const map = new Map(all.map(c => [c.id, c] as const)); + const visited = new Set(); + const ordered: CommitRecord[] = []; + const stack = [headCommitId]; + while (stack.length) { + const id = stack.pop()!; + if (visited.has(id)) continue; + visited.add(id); + const c = map.get(id); + if (!c) continue; + ordered.push(c); + for (const p of c.parentIds) stack.push(p); + } + ordered.sort((a,b)=>b.timestamp - a.timestamp); + return ordered; +} + +export async function commitOnBranch(repoId: string, branchId: string, message: string): Promise { + const branch = await get("branches", branchId); + if (!branch) throw new Error("Branch not found"); + const parent = branch.headCommitId ? [branch.headCommitId] : []; + const commit: CommitRecord = { id: nanoid(), repoId, message, parentIds: parent, timestamp: Date.now() }; + await put("commits", commit); + branch.headCommitId = commit.id; + await put("branches", branch); + return commit; +} + +export async function createIssue(repoId: string, data: { title: string; body?: string; labels?: string[]; branchId?: string; filePath?: string }): Promise { + const issue: IssueRecord = { + id: nanoid(), repoId, + title: data.title, body: data.body || "", + labels: data.labels || [], status: "open", + createdAt: Date.now(), updatedAt: Date.now(), + branchId: data.branchId, filePath: data.filePath + }; + await put("issues", issue); + return issue; +} + +export async function listIssues(repoId: string): Promise { + return getAllByIndex("issues", "by_repo", repoId); +} + +export async function updateIssue(issue: IssueRecord): Promise { + issue.updatedAt = Date.now(); + await put("issues", issue); +} + +export async function deleteIssue(id: string): Promise { await del("issues", id); }