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"; | |
| 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) { | |
| 126 | const { bootstrapRepository } = await import("../lib/repo-bootstrap"); | |
| 127 | await bootstrapRepository({ | |
| 128 | repositoryId: repo.id, | |
| 129 | ownerUserId: owner.id, | |
| 130 | }); | |
| 131 | } | |
| fc1817a | 132 | |
| 63807e5 | 133 | return c.json(repo, 201); |
| 134 | } catch (err) { | |
| 135 | console.error("[api] POST /repos:", err); | |
| 136 | return c.json({ error: "Service unavailable" }, 503); | |
| fc1817a | 137 | } |
| 138 | }); | |
| 139 | ||
| 140 | // List user's repositories | |
| 141 | api.get("/users/:username/repos", async (c) => { | |
| 142 | const { username } = c.req.param(); | |
| 63807e5 | 143 | try { |
| 144 | const [owner] = await db | |
| 145 | .select() | |
| 146 | .from(users) | |
| 147 | .where(eq(users.username, username)); | |
| 148 | if (!owner) return c.json({ error: "User not found" }, 404); | |
| 149 | ||
| 150 | const repos = await db | |
| 151 | .select() | |
| 152 | .from(repositories) | |
| 153 | .where(eq(repositories.ownerId, owner.id)); | |
| 154 | ||
| 155 | return c.json(repos); | |
| 156 | } catch (err) { | |
| 157 | console.error("[api] /users/:username/repos:", err); | |
| 158 | return c.json({ error: "Service unavailable" }, 503); | |
| 159 | } | |
| fc1817a | 160 | }); |
| 161 | ||
| 7437605 | 162 | // Get single repository (resolves both user- and org-owned namespaces) |
| fc1817a | 163 | api.get("/repos/:owner/:name", async (c) => { |
| 164 | const { owner: ownerName, name } = c.req.param(); | |
| 63807e5 | 165 | try { |
| 7437605 | 166 | const { loadRepoByPath } = await import("../lib/namespace"); |
| 167 | const repo = await loadRepoByPath(ownerName, name); | |
| 63807e5 | 168 | if (!repo) return c.json({ error: "Not found" }, 404); |
| 169 | return c.json(repo); | |
| 170 | } catch (err) { | |
| 171 | console.error("[api] /repos/:owner/:name:", err); | |
| 172 | return c.json({ error: "Service unavailable" }, 503); | |
| 173 | } | |
| fc1817a | 174 | }); |
| 175 | ||
| 176 | // Quick-setup: create user + repo in one call (dev convenience) | |
| 177 | api.post("/setup", async (c) => { | |
| 63807e5 | 178 | let body: { |
| fc1817a | 179 | username: string; |
| 180 | email: string; | |
| 181 | repoName: string; | |
| 182 | description?: string; | |
| 63807e5 | 183 | }; |
| 184 | try { | |
| 185 | body = await c.req.json(); | |
| 186 | } catch { | |
| 187 | return c.json({ error: "Invalid JSON body" }, 400); | |
| 188 | } | |
| 189 | if (!body.username || !body.email || !body.repoName) { | |
| 190 | return c.json( | |
| 191 | { error: "username, email, and repoName are required" }, | |
| 192 | 400 | |
| 193 | ); | |
| fc1817a | 194 | } |
| 195 | ||
| 63807e5 | 196 | try { |
| 197 | // Upsert user | |
| 198 | let [user] = await db | |
| 199 | .select() | |
| 200 | .from(users) | |
| 201 | .where(eq(users.username, body.username)); | |
| fc1817a | 202 | |
| 63807e5 | 203 | if (!user) { |
| 204 | [user] = await db | |
| 205 | .insert(users) | |
| 206 | .values({ | |
| 207 | username: body.username, | |
| 208 | email: body.email, | |
| 209 | passwordHash: await hashPassword("changeme"), | |
| 210 | }) | |
| 211 | .returning(); | |
| 212 | } | |
| fc1817a | 213 | |
| 63807e5 | 214 | // Create repo if not exists |
| 215 | if (!(await repoExists(body.username, body.repoName))) { | |
| 216 | const diskPath = await initBareRepo(body.username, body.repoName); | |
| 217 | const [repo] = await db | |
| 218 | .insert(repositories) | |
| 219 | .values({ | |
| 220 | name: body.repoName, | |
| 221 | ownerId: user.id, | |
| 222 | description: body.description || null, | |
| 223 | diskPath, | |
| 224 | }) | |
| 225 | .returning(); | |
| 226 | if (repo) { | |
| 227 | const { bootstrapRepository } = await import("../lib/repo-bootstrap"); | |
| 228 | await bootstrapRepository({ | |
| 229 | repositoryId: repo.id, | |
| 230 | ownerUserId: user.id, | |
| 231 | }); | |
| 232 | } | |
| 3ef4c9d | 233 | } |
| fc1817a | 234 | |
| 63807e5 | 235 | return c.json({ |
| 236 | user: user.username, | |
| 237 | repo: body.repoName, | |
| 238 | status: "ready", | |
| fc1817a | 239 | }); |
| 63807e5 | 240 | } catch (err) { |
| 241 | console.error("[api] POST /setup:", err); | |
| 242 | return c.json({ error: "Service unavailable" }, 503); | |
| fc1817a | 243 | } |
| 244 | }); | |
| 245 | ||
| 246 | export default api; |