Blame · Line-by-line history
api.ts
Each line is annotated with the commit that last touched it. Click any SHA to jump to that commit and see the surrounding change.
| fc1817a | 1 | /** |
| 2 | * REST API routes for repository management. | |
| 3 | */ | |
| 4 | ||
| 5 | import { Hono } from "hono"; | |
| 7437605 | 6 | import { eq, and, isNull } from "drizzle-orm"; |
| fc1817a | 7 | import { db } from "../db"; |
| 7437605 | 8 | import { users, repositories, organizations, orgMembers } from "../db/schema"; |
| fc1817a | 9 | import { initBareRepo, repoExists } from "../git/repository"; |
| 06d5ffe | 10 | import { hashPassword } from "../lib/auth"; |
| 7437605 | 11 | import { orgRoleAtLeast } from "../lib/orgs"; |
| fc1817a | 12 | |
| 13 | const api = new Hono().basePath("/api"); | |
| 14 | ||
| 15 | // Create repository | |
| 16 | api.post("/repos", async (c) => { | |
| 63807e5 | 17 | let body: { |
| fc1817a | 18 | name: string; |
| 19 | owner: string; | |
| 7437605 | 20 | orgSlug?: string; |
| fc1817a | 21 | description?: string; |
| 22 | isPrivate?: boolean; | |
| 63807e5 | 23 | }; |
| 24 | try { | |
| 25 | body = await c.req.json(); | |
| 26 | } catch { | |
| 27 | return c.json({ error: "Invalid JSON body" }, 400); | |
| 28 | } | |
| fc1817a | 29 | |
| 30 | if (!body.name || !body.owner) { | |
| 31 | return c.json({ error: "name and owner are required" }, 400); | |
| 32 | } | |
| 33 | ||
| 34 | // Validate repo name | |
| 35 | if (!/^[a-zA-Z0-9._-]+$/.test(body.name)) { | |
| 36 | return c.json({ error: "Invalid repository name" }, 400); | |
| 37 | } | |
| 38 | ||
| 63807e5 | 39 | try { |
| 7437605 | 40 | // Find creator (user who is performing the action) |
| 63807e5 | 41 | const [owner] = await db |
| 42 | .select() | |
| 43 | .from(users) | |
| 44 | .where(eq(users.username, body.owner)); | |
| fc1817a | 45 | |
| 63807e5 | 46 | if (!owner) { |
| 47 | return c.json({ error: "Owner not found" }, 404); | |
| 48 | } | |
| fc1817a | 49 | |
| 7437605 | 50 | // B2: if orgSlug supplied, place the repo in the org namespace. |
| 51 | // Requires the creator to be an admin+ of the org. | |
| 52 | let orgId: string | null = null; | |
| 53 | let namespaceSlug = body.owner; | |
| 54 | if (body.orgSlug) { | |
| 55 | const [org] = await db | |
| 56 | .select({ id: organizations.id, slug: organizations.slug }) | |
| 57 | .from(organizations) | |
| 58 | .where(eq(organizations.slug, body.orgSlug)) | |
| 59 | .limit(1); | |
| 60 | if (!org) return c.json({ error: "Organization not found" }, 404); | |
| 61 | ||
| 62 | const [mem] = await db | |
| 63 | .select({ role: orgMembers.role }) | |
| 64 | .from(orgMembers) | |
| 65 | .where( | |
| 66 | and(eq(orgMembers.orgId, org.id), eq(orgMembers.userId, owner.id)) | |
| 67 | ) | |
| 68 | .limit(1); | |
| 69 | if (!mem || !orgRoleAtLeast(mem.role, "admin")) { | |
| 70 | return c.json({ error: "Admin rights required on org" }, 403); | |
| 71 | } | |
| 72 | orgId = org.id; | |
| 73 | namespaceSlug = org.slug; | |
| 74 | } | |
| 75 | ||
| 76 | // Duplicate check: scoped to the right namespace. | |
| 77 | if (orgId) { | |
| 78 | const [existing] = await db | |
| 79 | .select({ id: repositories.id }) | |
| 80 | .from(repositories) | |
| 81 | .where( | |
| 82 | and(eq(repositories.orgId, orgId), eq(repositories.name, body.name)) | |
| 83 | ) | |
| 84 | .limit(1); | |
| 85 | if (existing) { | |
| 86 | return c.json({ error: "Repository already exists" }, 409); | |
| 87 | } | |
| 88 | } else { | |
| 89 | const [existing] = await db | |
| 90 | .select({ id: repositories.id }) | |
| 91 | .from(repositories) | |
| 92 | .where( | |
| 93 | and( | |
| 94 | eq(repositories.ownerId, owner.id), | |
| 95 | eq(repositories.name, body.name), | |
| 96 | isNull(repositories.orgId) | |
| 97 | ) | |
| 98 | ) | |
| 99 | .limit(1); | |
| 100 | if (existing) { | |
| 101 | return c.json({ error: "Repository already exists" }, 409); | |
| 102 | } | |
| 103 | } | |
| 104 | if (await repoExists(namespaceSlug, body.name)) { | |
| 63807e5 | 105 | return c.json({ error: "Repository already exists" }, 409); |
| 106 | } | |
| fc1817a | 107 | |
| 7437605 | 108 | // Init bare repo on disk, keyed by the namespace slug (user or org). |
| 109 | const diskPath = await initBareRepo(namespaceSlug, body.name); | |
| 63807e5 | 110 | |
| 111 | // Insert into DB | |
| 112 | const [repo] = await db | |
| 113 | .insert(repositories) | |
| 114 | .values({ | |
| 115 | name: body.name, | |
| 116 | ownerId: owner.id, | |
| 7437605 | 117 | orgId, |
| 63807e5 | 118 | description: body.description || null, |
| 119 | isPrivate: body.isPrivate || false, | |
| 120 | diskPath, | |
| 121 | }) | |
| 122 | .returning(); | |
| 3ef4c9d | 123 | |
| 63807e5 | 124 | // Green-ecosystem bootstrap: settings, protection, labels, welcome issue |
| 125 | if (repo) { | |
| cde9983 | 126 | const { bootstrapRepository, ensureAiBuildSeedIssue } = await import("../lib/repo-bootstrap"); |
| 63807e5 | 127 | await bootstrapRepository({ |
| 128 | repositoryId: repo.id, | |
| 129 | ownerUserId: owner.id, | |
| 130 | }); | |
| cde9983 | 131 | // Fire-and-forget — never block the response |
| 132 | ensureAiBuildSeedIssue(repo.id, owner.id).catch(err => | |
| 133 | console.warn('[ai-build-seed]', err?.message) | |
| 134 | ); | |
| 63807e5 | 135 | } |
| fc1817a | 136 | |
| 63807e5 | 137 | return c.json(repo, 201); |
| 138 | } catch (err) { | |
| 139 | console.error("[api] POST /repos:", err); | |
| 140 | return c.json({ error: "Service unavailable" }, 503); | |
| fc1817a | 141 | } |
| 142 | }); | |
| 143 | ||
| 144 | // List user's repositories | |
| 145 | api.get("/users/:username/repos", async (c) => { | |
| 146 | const { username } = c.req.param(); | |
| 63807e5 | 147 | try { |
| 148 | const [owner] = await db | |
| 149 | .select() | |
| 150 | .from(users) | |
| 151 | .where(eq(users.username, username)); | |
| 152 | if (!owner) return c.json({ error: "User not found" }, 404); | |
| 153 | ||
| 154 | const repos = await db | |
| 155 | .select() | |
| 156 | .from(repositories) | |
| 157 | .where(eq(repositories.ownerId, owner.id)); | |
| 158 | ||
| 159 | return c.json(repos); | |
| 160 | } catch (err) { | |
| 161 | console.error("[api] /users/:username/repos:", err); | |
| 162 | return c.json({ error: "Service unavailable" }, 503); | |
| 163 | } | |
| fc1817a | 164 | }); |
| 165 | ||
| 7437605 | 166 | // Get single repository (resolves both user- and org-owned namespaces) |
| fc1817a | 167 | api.get("/repos/:owner/:name", async (c) => { |
| 168 | const { owner: ownerName, name } = c.req.param(); | |
| 63807e5 | 169 | try { |
| 7437605 | 170 | const { loadRepoByPath } = await import("../lib/namespace"); |
| 171 | const repo = await loadRepoByPath(ownerName, name); | |
| 63807e5 | 172 | if (!repo) return c.json({ error: "Not found" }, 404); |
| 173 | return c.json(repo); | |
| 174 | } catch (err) { | |
| 175 | console.error("[api] /repos/:owner/:name:", err); | |
| 176 | return c.json({ error: "Service unavailable" }, 503); | |
| 177 | } | |
| fc1817a | 178 | }); |
| 179 | ||
| 180 | // Quick-setup: create user + repo in one call (dev convenience) | |
| 181 | api.post("/setup", async (c) => { | |
| 63807e5 | 182 | let body: { |
| fc1817a | 183 | username: string; |
| 184 | email: string; | |
| 185 | repoName: string; | |
| 186 | description?: string; | |
| 63807e5 | 187 | }; |
| 188 | try { | |
| 189 | body = await c.req.json(); | |
| 190 | } catch { | |
| 191 | return c.json({ error: "Invalid JSON body" }, 400); | |
| 192 | } | |
| 193 | if (!body.username || !body.email || !body.repoName) { | |
| 194 | return c.json( | |
| 195 | { error: "username, email, and repoName are required" }, | |
| 196 | 400 | |
| 197 | ); | |
| fc1817a | 198 | } |
| 199 | ||
| 63807e5 | 200 | try { |
| 201 | // Upsert user | |
| 202 | let [user] = await db | |
| 203 | .select() | |
| 204 | .from(users) | |
| 205 | .where(eq(users.username, body.username)); | |
| fc1817a | 206 | |
| 63807e5 | 207 | if (!user) { |
| 208 | [user] = await db | |
| 209 | .insert(users) | |
| 210 | .values({ | |
| 211 | username: body.username, | |
| 212 | email: body.email, | |
| 213 | passwordHash: await hashPassword("changeme"), | |
| 214 | }) | |
| 215 | .returning(); | |
| 216 | } | |
| fc1817a | 217 | |
| 63807e5 | 218 | // Create repo if not exists |
| 219 | if (!(await repoExists(body.username, body.repoName))) { | |
| 220 | const diskPath = await initBareRepo(body.username, body.repoName); | |
| 221 | const [repo] = await db | |
| 222 | .insert(repositories) | |
| 223 | .values({ | |
| 224 | name: body.repoName, | |
| 225 | ownerId: user.id, | |
| 226 | description: body.description || null, | |
| 227 | diskPath, | |
| 228 | }) | |
| 229 | .returning(); | |
| 230 | if (repo) { | |
| 231 | const { bootstrapRepository } = await import("../lib/repo-bootstrap"); | |
| 232 | await bootstrapRepository({ | |
| 233 | repositoryId: repo.id, | |
| 234 | ownerUserId: user.id, | |
| 235 | }); | |
| 236 | } | |
| 3ef4c9d | 237 | } |
| fc1817a | 238 | |
| 63807e5 | 239 | return c.json({ |
| 240 | user: user.username, | |
| 241 | repo: body.repoName, | |
| 242 | status: "ready", | |
| fc1817a | 243 | }); |
| 63807e5 | 244 | } catch (err) { |
| 245 | console.error("[api] POST /setup:", err); | |
| 246 | return c.json({ error: "Service unavailable" }, 503); | |
| fc1817a | 247 | } |
| 248 | }); | |
| 249 | ||
| 250 | export default api; |