CodeIssuesDiscussionsWikiPull RequestsProjectsCommitsActionsReleasesContributorsPulse● GatesSecuritySettingsDeploymentsPipelineInsightsAgents✨ Explain✨ Ask AI✨ Workspace✨ Spec✨ Tests▓ Debt Map✨ NL Search🏛 Archaeology
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"; | |
| 829a046 | 6 | import { eq, and, isNull, ilike, sql } 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"; |
| 6cd2f0e | 12 | import { renderMarkdown } from "../lib/markdown"; |
| fc1817a | 13 | |
| 14 | const api = new Hono().basePath("/api"); | |
| 15 | ||
| 16 | // Create repository | |
| 17 | api.post("/repos", async (c) => { | |
| 63807e5 | 18 | let body: { |
| fc1817a | 19 | name: string; |
| 20 | owner: string; | |
| 7437605 | 21 | orgSlug?: string; |
| fc1817a | 22 | description?: string; |
| 23 | isPrivate?: boolean; | |
| 63807e5 | 24 | }; |
| 25 | try { | |
| 26 | body = await c.req.json(); | |
| 27 | } catch { | |
| 28 | return c.json({ error: "Invalid JSON body" }, 400); | |
| 29 | } | |
| fc1817a | 30 | |
| 31 | if (!body.name || !body.owner) { | |
| 32 | return c.json({ error: "name and owner are required" }, 400); | |
| 33 | } | |
| 34 | ||
| 35 | // Validate repo name | |
| 36 | if (!/^[a-zA-Z0-9._-]+$/.test(body.name)) { | |
| 37 | return c.json({ error: "Invalid repository name" }, 400); | |
| 38 | } | |
| 39 | ||
| 63807e5 | 40 | try { |
| 7437605 | 41 | // Find creator (user who is performing the action) |
| 63807e5 | 42 | const [owner] = await db |
| 43 | .select() | |
| 44 | .from(users) | |
| 45 | .where(eq(users.username, body.owner)); | |
| fc1817a | 46 | |
| 63807e5 | 47 | if (!owner) { |
| 48 | return c.json({ error: "Owner not found" }, 404); | |
| 49 | } | |
| fc1817a | 50 | |
| 7437605 | 51 | // B2: if orgSlug supplied, place the repo in the org namespace. |
| 52 | // Requires the creator to be an admin+ of the org. | |
| 53 | let orgId: string | null = null; | |
| 54 | let namespaceSlug = body.owner; | |
| 55 | if (body.orgSlug) { | |
| 56 | const [org] = await db | |
| 57 | .select({ id: organizations.id, slug: organizations.slug }) | |
| 58 | .from(organizations) | |
| 59 | .where(eq(organizations.slug, body.orgSlug)) | |
| 60 | .limit(1); | |
| 61 | if (!org) return c.json({ error: "Organization not found" }, 404); | |
| 62 | ||
| 63 | const [mem] = await db | |
| 64 | .select({ role: orgMembers.role }) | |
| 65 | .from(orgMembers) | |
| 66 | .where( | |
| 67 | and(eq(orgMembers.orgId, org.id), eq(orgMembers.userId, owner.id)) | |
| 68 | ) | |
| 69 | .limit(1); | |
| 70 | if (!mem || !orgRoleAtLeast(mem.role, "admin")) { | |
| 71 | return c.json({ error: "Admin rights required on org" }, 403); | |
| 72 | } | |
| 73 | orgId = org.id; | |
| 74 | namespaceSlug = org.slug; | |
| 75 | } | |
| 76 | ||
| 77 | // Duplicate check: scoped to the right namespace. | |
| 78 | if (orgId) { | |
| 79 | const [existing] = await db | |
| 80 | .select({ id: repositories.id }) | |
| 81 | .from(repositories) | |
| 82 | .where( | |
| 83 | and(eq(repositories.orgId, orgId), eq(repositories.name, body.name)) | |
| 84 | ) | |
| 85 | .limit(1); | |
| 86 | if (existing) { | |
| 87 | return c.json({ error: "Repository already exists" }, 409); | |
| 88 | } | |
| 89 | } else { | |
| 90 | const [existing] = await db | |
| 91 | .select({ id: repositories.id }) | |
| 92 | .from(repositories) | |
| 93 | .where( | |
| 94 | and( | |
| 95 | eq(repositories.ownerId, owner.id), | |
| 96 | eq(repositories.name, body.name), | |
| 97 | isNull(repositories.orgId) | |
| 98 | ) | |
| 99 | ) | |
| 100 | .limit(1); | |
| 101 | if (existing) { | |
| 102 | return c.json({ error: "Repository already exists" }, 409); | |
| 103 | } | |
| 104 | } | |
| 105 | if (await repoExists(namespaceSlug, body.name)) { | |
| 63807e5 | 106 | return c.json({ error: "Repository already exists" }, 409); |
| 107 | } | |
| fc1817a | 108 | |
| 7437605 | 109 | // Init bare repo on disk, keyed by the namespace slug (user or org). |
| 110 | const diskPath = await initBareRepo(namespaceSlug, body.name); | |
| 63807e5 | 111 | |
| 112 | // Insert into DB | |
| 113 | const [repo] = await db | |
| 114 | .insert(repositories) | |
| 115 | .values({ | |
| 116 | name: body.name, | |
| 117 | ownerId: owner.id, | |
| 7437605 | 118 | orgId, |
| 63807e5 | 119 | description: body.description || null, |
| 120 | isPrivate: body.isPrivate || false, | |
| 121 | diskPath, | |
| 122 | }) | |
| 123 | .returning(); | |
| 3ef4c9d | 124 | |
| 63807e5 | 125 | // Green-ecosystem bootstrap: settings, protection, labels, welcome issue |
| 126 | if (repo) { | |
| cde9983 | 127 | const { bootstrapRepository, ensureAiBuildSeedIssue } = await import("../lib/repo-bootstrap"); |
| 63807e5 | 128 | await bootstrapRepository({ |
| 129 | repositoryId: repo.id, | |
| 130 | ownerUserId: owner.id, | |
| 131 | }); | |
| cde9983 | 132 | // Fire-and-forget — never block the response |
| 133 | ensureAiBuildSeedIssue(repo.id, owner.id).catch(err => | |
| 134 | console.warn('[ai-build-seed]', err?.message) | |
| 135 | ); | |
| 63807e5 | 136 | } |
| fc1817a | 137 | |
| 63807e5 | 138 | return c.json(repo, 201); |
| 139 | } catch (err) { | |
| 140 | console.error("[api] POST /repos:", err); | |
| 141 | return c.json({ error: "Service unavailable" }, 503); | |
| fc1817a | 142 | } |
| 143 | }); | |
| 144 | ||
| 145 | // List user's repositories | |
| 146 | api.get("/users/:username/repos", async (c) => { | |
| 147 | const { username } = c.req.param(); | |
| 63807e5 | 148 | try { |
| 149 | const [owner] = await db | |
| 150 | .select() | |
| 151 | .from(users) | |
| 152 | .where(eq(users.username, username)); | |
| 153 | if (!owner) return c.json({ error: "User not found" }, 404); | |
| 154 | ||
| 155 | const repos = await db | |
| 156 | .select() | |
| 157 | .from(repositories) | |
| 158 | .where(eq(repositories.ownerId, owner.id)); | |
| 159 | ||
| 160 | return c.json(repos); | |
| 161 | } catch (err) { | |
| 162 | console.error("[api] /users/:username/repos:", err); | |
| 163 | return c.json({ error: "Service unavailable" }, 503); | |
| 164 | } | |
| fc1817a | 165 | }); |
| 166 | ||
| 7437605 | 167 | // Get single repository (resolves both user- and org-owned namespaces) |
| fc1817a | 168 | api.get("/repos/:owner/:name", async (c) => { |
| 169 | const { owner: ownerName, name } = c.req.param(); | |
| 63807e5 | 170 | try { |
| 7437605 | 171 | const { loadRepoByPath } = await import("../lib/namespace"); |
| 172 | const repo = await loadRepoByPath(ownerName, name); | |
| 63807e5 | 173 | if (!repo) return c.json({ error: "Not found" }, 404); |
| 174 | return c.json(repo); | |
| 175 | } catch (err) { | |
| 176 | console.error("[api] /repos/:owner/:name:", err); | |
| 177 | return c.json({ error: "Service unavailable" }, 503); | |
| 178 | } | |
| fc1817a | 179 | }); |
| 180 | ||
| 181 | // Quick-setup: create user + repo in one call (dev convenience) | |
| 182 | api.post("/setup", async (c) => { | |
| 63807e5 | 183 | let body: { |
| fc1817a | 184 | username: string; |
| 185 | email: string; | |
| 186 | repoName: string; | |
| 187 | description?: string; | |
| 63807e5 | 188 | }; |
| 189 | try { | |
| 190 | body = await c.req.json(); | |
| 191 | } catch { | |
| 192 | return c.json({ error: "Invalid JSON body" }, 400); | |
| 193 | } | |
| 194 | if (!body.username || !body.email || !body.repoName) { | |
| 195 | return c.json( | |
| 196 | { error: "username, email, and repoName are required" }, | |
| 197 | 400 | |
| 198 | ); | |
| fc1817a | 199 | } |
| 200 | ||
| 63807e5 | 201 | try { |
| 202 | // Upsert user | |
| 203 | let [user] = await db | |
| 204 | .select() | |
| 205 | .from(users) | |
| 206 | .where(eq(users.username, body.username)); | |
| fc1817a | 207 | |
| 63807e5 | 208 | if (!user) { |
| 209 | [user] = await db | |
| 210 | .insert(users) | |
| 211 | .values({ | |
| 212 | username: body.username, | |
| 213 | email: body.email, | |
| 214 | passwordHash: await hashPassword("changeme"), | |
| 215 | }) | |
| 216 | .returning(); | |
| 217 | } | |
| fc1817a | 218 | |
| 63807e5 | 219 | // Create repo if not exists |
| 220 | if (!(await repoExists(body.username, body.repoName))) { | |
| 221 | const diskPath = await initBareRepo(body.username, body.repoName); | |
| 222 | const [repo] = await db | |
| 223 | .insert(repositories) | |
| 224 | .values({ | |
| 225 | name: body.repoName, | |
| 226 | ownerId: user.id, | |
| 227 | description: body.description || null, | |
| 228 | diskPath, | |
| 229 | }) | |
| 230 | .returning(); | |
| 231 | if (repo) { | |
| 232 | const { bootstrapRepository } = await import("../lib/repo-bootstrap"); | |
| 233 | await bootstrapRepository({ | |
| 234 | repositoryId: repo.id, | |
| 235 | ownerUserId: user.id, | |
| 236 | }); | |
| 237 | } | |
| 3ef4c9d | 238 | } |
| fc1817a | 239 | |
| 63807e5 | 240 | return c.json({ |
| 241 | user: user.username, | |
| 242 | repo: body.repoName, | |
| 243 | status: "ready", | |
| fc1817a | 244 | }); |
| 63807e5 | 245 | } catch (err) { |
| 246 | console.error("[api] POST /setup:", err); | |
| 247 | return c.json({ error: "Service unavailable" }, 503); | |
| fc1817a | 248 | } |
| 249 | }); | |
| 250 | ||
| 6cd2f0e | 251 | // Markdown preview — render markdown to HTML for the live preview tab. |
| 252 | // Body: { text: string }. Returns { html: string }. | |
| 253 | // Rate-limited by the global limiter in middleware. No auth required. | |
| 254 | api.post("/markdown/preview", async (c) => { | |
| 255 | let text = ""; | |
| 256 | try { | |
| 257 | const body = await c.req.json(); | |
| 258 | text = String(body.text || "").slice(0, 64_000); | |
| 259 | } catch { | |
| 260 | return c.json({ html: "" }, 400); | |
| 261 | } | |
| 262 | const html = renderMarkdown(text); | |
| 263 | return c.json({ html }); | |
| 264 | }); | |
| 265 | ||
| 829a046 | 266 | // User mention autocomplete — returns up to 8 matching usernames for `@` suggestions. |
| 267 | // Public endpoint (no auth required) since usernames are public. | |
| 268 | api.get("/users/suggest", async (c) => { | |
| 269 | const q = String(c.req.query("q") || "").trim().replace(/^@/, "").slice(0, 39); | |
| 270 | if (!q) return c.json({ users: [] }); | |
| 271 | try { | |
| 272 | const rows = await db | |
| 273 | .select({ username: users.username, displayName: users.displayName }) | |
| 274 | .from(users) | |
| 275 | .where(ilike(users.username, `${q}%`)) | |
| 276 | .orderBy(sql`lower(${users.username})`) | |
| 277 | .limit(8); | |
| 278 | return c.json({ users: rows }); | |
| 279 | } catch { | |
| 280 | return c.json({ users: [] }); | |
| 281 | } | |
| 282 | }); | |
| 283 | ||
| fc1817a | 284 | export default api; |