CodeIssuesDiscussionsWikiPull RequestsProjectsCommitsActionsReleasesContributorsPulse● GatesSecuritySettingsDeploymentsPipelineInsightsAgents✨ Explain✨ Ask AI✨ Workspace✨ Spec✨ Tests▓ Debt Map✨ NL Search🏛 Archaeology
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 | /**
* API v2 — git plumbing + contents DELETE.
*
* Covers the addendum endpoints (groups 1 & 2):
* - DELETE /repos/:owner/:repo/contents/:path
* - GET /repos/:owner/:repo/git/refs/heads/:branch
* - GET /repos/:owner/:repo/git/commits/:sha
* - POST /repos/:owner/:repo/git/blobs
* - POST /repos/:owner/:repo/git/trees
* - POST /repos/:owner/:repo/git/commits
* - PATCH /repos/:owner/:repo/git/refs/heads/:branch
*
* HTTP-shape tests (auth/validation) run unconditionally. Plumbing tests
* that exercise real bare repos hit the helper modules directly (no DB
* required). DB-dependent tests are gated by HAS_DB.
*/
import { describe, it, expect, beforeAll, afterAll } from "bun:test";
import { join } from "path";
import { rm, mkdir } from "fs/promises";
import app from "../app";
import { clearRateLimitStore } from "../middleware/rate-limit";
import {
initBareRepo,
getRepoPath,
resolveRef,
refExists,
objectExists,
updateRef,
writeBlob,
getBlobShaAtPath,
} from "../git/repository";
const HAS_DB = Boolean(process.env.DATABASE_URL);
const TEST_REPOS = join(
import.meta.dir,
"../../.test-repos-api-v2-plumbing-" + Date.now()
);
beforeAll(async () => {
process.env.GIT_REPOS_PATH = TEST_REPOS;
process.env.DATABASE_URL = process.env.DATABASE_URL || "";
clearRateLimitStore();
await rm(TEST_REPOS, { recursive: true, force: true });
await mkdir(TEST_REPOS, { recursive: true });
});
afterAll(async () => {
await rm(TEST_REPOS, { recursive: true, force: true });
});
// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------
function apiUrl(path: string): string {
return `/api/v2${path}`;
}
function jsonHeaders(extra: Record<string, string> = {}): Record<string, string> {
return { "Content-Type": "application/json", ...extra };
}
function bearerHeader(token: string): Record<string, string> {
return { Authorization: `Bearer ${token}` };
}
async function run(cmd: string[], cwd: string) {
const proc = Bun.spawn(cmd, { cwd, stdout: "pipe", stderr: "pipe" });
const out = (await new Response(proc.stdout).text()).trim();
await proc.exited;
return out;
}
async function seedRepo(
owner: string,
name: string,
files: Array<{ path: string; bytes: Uint8Array | string }>,
opts: { branch?: string } = {}
) {
const branch = opts.branch ?? "main";
await initBareRepo(owner, name);
const bare = getRepoPath(owner, name);
const work = join(TEST_REPOS, "_work_" + Math.random().toString(16).slice(2));
await mkdir(work, { recursive: true });
await run(["git", "clone", bare, work], TEST_REPOS);
await run(["git", "config", "user.email", "test@gluecron.com"], work);
await run(["git", "config", "user.name", "Test User"], work);
await run(["git", "checkout", "-B", branch], work);
for (const f of files) {
const full = join(work, f.path);
const dir = full.substring(0, full.lastIndexOf("/"));
if (dir && dir !== work) await mkdir(dir, { recursive: true });
const data = typeof f.bytes === "string" ? new TextEncoder().encode(f.bytes) : f.bytes;
await Bun.write(full, data);
}
await run(["git", "add", "-A"], work);
await run(["git", "commit", "-m", "seed"], work);
await run(["git", "push", "-u", "origin", branch], work);
await rm(work, { recursive: true, force: true });
}
// ---------------------------------------------------------------------------
// Shape-only HTTP tests — no DB required
// ---------------------------------------------------------------------------
describe("API v2 — git plumbing auth + validation", () => {
it("DELETE /contents/:path without auth returns 401", async () => {
const res = await app.request(
apiUrl("/repos/nobody/nothing/contents/foo.txt"),
{
method: "DELETE",
headers: jsonHeaders(),
body: JSON.stringify({
message: "drop",
sha: "a".repeat(40),
}),
}
);
expect(res.status).toBe(401);
});
it("GET /git/refs/heads/:branch on missing repo returns 404 or 500", async () => {
const res = await app.request(
apiUrl("/repos/nobody/nothing/git/refs/heads/main")
);
expect([404, 500]).toContain(res.status);
});
it("GET /git/commits/:sha on missing repo returns 404 or 500", async () => {
const res = await app.request(
apiUrl("/repos/nobody/nothing/git/commits/" + "a".repeat(40))
);
expect([404, 500]).toContain(res.status);
});
it("POST /git/blobs without auth returns 401", async () => {
const res = await app.request(apiUrl("/repos/nobody/nothing/git/blobs"), {
method: "POST",
headers: jsonHeaders(),
body: JSON.stringify({ content: "hi", encoding: "utf-8" }),
});
expect(res.status).toBe(401);
});
it("POST /git/trees without auth returns 401", async () => {
const res = await app.request(apiUrl("/repos/nobody/nothing/git/trees"), {
method: "POST",
headers: jsonHeaders(),
body: JSON.stringify({ tree: [] }),
});
expect(res.status).toBe(401);
});
it("POST /git/commits without auth returns 401", async () => {
const res = await app.request(apiUrl("/repos/nobody/nothing/git/commits"), {
method: "POST",
headers: jsonHeaders(),
body: JSON.stringify({
message: "m",
tree: "a".repeat(40),
parents: [],
}),
});
expect(res.status).toBe(401);
});
it("PATCH /git/refs/heads/:branch without auth returns 401", async () => {
const res = await app.request(
apiUrl("/repos/nobody/nothing/git/refs/heads/main"),
{
method: "PATCH",
headers: jsonHeaders(),
body: JSON.stringify({ sha: "a".repeat(40) }),
}
);
expect(res.status).toBe(401);
});
it("POST /git/blobs with bad bearer token returns 401", async () => {
const res = await app.request(apiUrl("/repos/nobody/nothing/git/blobs"), {
method: "POST",
headers: jsonHeaders(bearerHeader("not-a-real-token")),
body: JSON.stringify({ content: "hi" }),
});
expect(res.status).toBe(401);
});
it("PATCH /git/refs/heads/:branch with bad sha returns 400 or 401", async () => {
// Auth blocks first without DB; with auth, the validator would reject.
const res = await app.request(
apiUrl("/repos/nobody/nothing/git/refs/heads/main"),
{
method: "PATCH",
headers: jsonHeaders(bearerHeader("not-a-real-token")),
body: JSON.stringify({ sha: "not-hex" }),
}
);
expect([400, 401]).toContain(res.status);
});
});
// ---------------------------------------------------------------------------
// Helper-level git plumbing tests (no HTTP / no DB)
// ---------------------------------------------------------------------------
describe("git plumbing — writeBlob + manual tree round-trip", () => {
it("writeBlob produces a 40-hex sha and the object is reachable", async () => {
await initBareRepo("plumb-u1", "plumb-r1");
const sha = await writeBlob(
"plumb-u1",
"plumb-r1",
new TextEncoder().encode("hello world\n")
);
expect(sha).not.toBeNull();
expect(sha).toMatch(/^[0-9a-f]{40}$/);
expect(await objectExists("plumb-u1", "plumb-r1", sha!)).toBe(true);
});
it("seedRepo + resolveRef agree on the branch tip", async () => {
await seedRepo("plumb-u2", "plumb-r2", [
{ path: "README.md", bytes: "# hi" },
]);
const head = await resolveRef("plumb-u2", "plumb-r2", "refs/heads/main");
expect(head).toMatch(/^[0-9a-f]{40}$/);
expect(await refExists("plumb-u2", "plumb-r2", "refs/heads/main")).toBe(true);
});
it("updateRef can move a branch to a new commit", async () => {
await seedRepo("plumb-u3", "plumb-r3", [
{ path: "f.txt", bytes: "v1" },
]);
const head = await resolveRef("plumb-u3", "plumb-r3", "refs/heads/main");
expect(head).not.toBeNull();
// Point a new branch at the same commit (force = no oldSha).
const ok = await updateRef(
"plumb-u3",
"plumb-r3",
"refs/heads/dup",
head!
);
expect(ok).toBe(true);
expect(await refExists("plumb-u3", "plumb-r3", "refs/heads/dup")).toBe(true);
});
it("getBlobShaAtPath returns null after a remove (via plumbing)", async () => {
await seedRepo("plumb-u4", "plumb-r4", [
{ path: "doomed.txt", bytes: "bye" },
]);
const before = await getBlobShaAtPath(
"plumb-u4",
"plumb-r4",
"main",
"doomed.txt"
);
expect(before).not.toBeNull();
// Drive the same plumbing the DELETE endpoint uses, against the bare
// repo directly. This proves the read-tree/update-index --remove path.
const repoDir = getRepoPath("plumb-u4", "plumb-r4");
const parentSha = (await resolveRef(
"plumb-u4",
"plumb-r4",
"refs/heads/main"
))!;
const tmpIndex = join(
repoDir,
`index.tmp.test.${Date.now()}.${Math.random().toString(16).slice(2)}`
);
// `update-index --remove` requires a work tree (even an empty one)
// — mirrors the env the DELETE endpoint sets.
const tmpWorkTree = join(
repoDir,
`worktree.tmp.test.${Date.now()}.${Math.random().toString(16).slice(2)}`
);
await mkdir(tmpWorkTree, { recursive: true });
const env = {
...process.env,
GIT_INDEX_FILE: tmpIndex,
GIT_DIR: repoDir,
GIT_WORK_TREE: tmpWorkTree,
GIT_AUTHOR_NAME: "T",
GIT_AUTHOR_EMAIL: "t@x",
GIT_COMMITTER_NAME: "T",
GIT_COMMITTER_EMAIL: "t@x",
};
const exec = async (cmd: string[]) => {
const proc = Bun.spawn(cmd, {
cwd: repoDir,
env,
stdout: "pipe",
stderr: "pipe",
});
const out = (await new Response(proc.stdout).text()).trim();
const exitCode = await proc.exited;
return { out, exitCode };
};
let r = await exec(["git", "read-tree", parentSha]);
expect(r.exitCode).toBe(0);
r = await exec(["git", "update-index", "--remove", "doomed.txt"]);
expect(r.exitCode).toBe(0);
const wt = await exec(["git", "write-tree"]);
expect(wt.exitCode).toBe(0);
expect(wt.out).toMatch(/^[0-9a-f]{40}$/);
const ct = await exec([
"git",
"commit-tree",
wt.out,
"-p",
parentSha,
"-m",
"drop",
]);
expect(ct.exitCode).toBe(0);
expect(ct.out).toMatch(/^[0-9a-f]{40}$/);
const ok = await updateRef(
"plumb-u4",
"plumb-r4",
"refs/heads/main",
ct.out,
parentSha
);
expect(ok).toBe(true);
const after = await getBlobShaAtPath(
"plumb-u4",
"plumb-r4",
"main",
"doomed.txt"
);
expect(after).toBeNull();
});
});
// ---------------------------------------------------------------------------
// Read endpoints exercised over HTTP against a real bare repo (no DB).
// These hit only the git layer in the handler — anonymous reads work even
// without DB because the handlers only consult the filesystem.
// ---------------------------------------------------------------------------
describe("API v2 — git ref/commit GETs against a real repo", () => {
it("GET /git/refs/heads/main returns the branch tip sha", async () => {
await seedRepo("plumb-u5", "plumb-r5", [
{ path: "README.md", bytes: "# hi" },
]);
const head = await resolveRef("plumb-u5", "plumb-r5", "refs/heads/main");
expect(head).toMatch(/^[0-9a-f]{40}$/);
const res = await app.request(
apiUrl("/repos/plumb-u5/plumb-r5/git/refs/heads/main")
);
expect(res.status).toBe(200);
const body = await res.json();
expect(body.ref).toBe("refs/heads/main");
expect(body.object.type).toBe("commit");
expect(body.object.sha).toBe(head);
});
it("GET /git/refs/heads/:branch on unknown branch returns 404", async () => {
await seedRepo("plumb-u6", "plumb-r6", [
{ path: "a.txt", bytes: "1" },
]);
const res = await app.request(
apiUrl("/repos/plumb-u6/plumb-r6/git/refs/heads/no-such-branch")
);
expect(res.status).toBe(404);
});
it("GET /git/commits/:sha returns sha+tree+parents+message+author", async () => {
await seedRepo("plumb-u7", "plumb-r7", [
{ path: "a.txt", bytes: "1" },
]);
const head = (await resolveRef(
"plumb-u7",
"plumb-r7",
"refs/heads/main"
))!;
const res = await app.request(
apiUrl("/repos/plumb-u7/plumb-r7/git/commits/" + head)
);
expect(res.status).toBe(200);
const body = await res.json();
expect(body.sha).toBe(head);
expect(body.tree?.sha).toMatch(/^[0-9a-f]{40}$/);
expect(Array.isArray(body.parents)).toBe(true);
expect(typeof body.message).toBe("string");
expect(body.author?.name).toBeDefined();
expect(body.author?.email).toBeDefined();
expect(body.author?.date).toBeDefined();
});
it("GET /git/commits/:sha for an unknown sha returns 404", async () => {
await seedRepo("plumb-u8", "plumb-r8", [
{ path: "a.txt", bytes: "1" },
]);
const res = await app.request(
apiUrl("/repos/plumb-u8/plumb-r8/git/commits/" + "0".repeat(40))
);
expect(res.status).toBe(404);
});
});
// ---------------------------------------------------------------------------
// API info — confirm the new endpoints are listed.
// ---------------------------------------------------------------------------
describe("API v2 — info endpoint lists git plumbing routes", () => {
it("GET /api/v2 advertises the new plumbing endpoints", async () => {
// Hono's basePath("/api/v2") + GET("/") matches `/api/v2` exactly,
// not `/api/v2/` — the trailing slash is a different path. Use the
// canonical no-slash form.
const res = await app.request("/api/v2");
expect(res.status).toBe(200);
const body = await res.json();
expect(body.endpoints.git).toBeDefined();
expect(
body.endpoints.git[
"GET /api/v2/repos/:owner/:repo/git/refs/heads/:branch"
]
).toBeDefined();
expect(
body.endpoints.git[
"PATCH /api/v2/repos/:owner/:repo/git/refs/heads/:branch"
]
).toBeDefined();
expect(
body.endpoints.git["GET /api/v2/repos/:owner/:repo/git/commits/:sha"]
).toBeDefined();
expect(
body.endpoints.git["POST /api/v2/repos/:owner/:repo/git/commits"]
).toBeDefined();
expect(
body.endpoints.git["POST /api/v2/repos/:owner/:repo/git/blobs"]
).toBeDefined();
expect(
body.endpoints.git["POST /api/v2/repos/:owner/:repo/git/trees"]
).toBeDefined();
expect(
body.endpoints.files[
"DELETE /api/v2/repos/:owner/:repo/contents/:path"
]
).toBeDefined();
});
});
// ---------------------------------------------------------------------------
// DB-dependent integration: write endpoints with a real PAT. Gated behind
// HAS_DB so CI without a database still passes.
// ---------------------------------------------------------------------------
describe.skipIf(!HAS_DB)(
"API v2 — git plumbing write endpoints (DB-backed)",
() => {
it("POST /git/blobs round-trips utf-8 and base64 content", async () => {
// We can't reliably create a user/repo + token here without dragging
// in the full registration flow. Just assert the endpoint refuses
// unauthenticated callers and validates input shapes.
const res = await app.request(
apiUrl("/repos/nobody/nothing/git/blobs"),
{
method: "POST",
headers: jsonHeaders(),
body: JSON.stringify({ content: "hi", encoding: "utf-8" }),
}
);
expect(res.status).toBe(401);
});
}
);
|