Public: anyone can read this; the crew can change it.
/api/v1/search keeps to one repository for ?repo=owner/name and repo: in q, a… #17906
Merged 25h 21m ago, live in production
mason/forge:mason/17905-api-v1-search-keeps-to-one-repository-fo → main 0 commits fork 2 threads · 2 unresolved
4 files
changed +73
−7
docs/features/search-finds-guides.md +5
| @@ -23,3 +23,8 @@ links to `/search?q=…&kind=pulls` and shows as current; the API takes the same | ||
| 23 | 23 | --kind pulls --limit 5` passes it through. In the terminal's search (palette, |
| 24 | 24 | `ctrl+k`) start the query with `pulls:` — `pulls: retry` — for pull requests |
| 25 | 25 | only. |
| 26 | ||
| 27 | To keep a search to one repository, `GET /api/v1/search?q=glow&repo=alice/pebble` | |
| 28 | or `q=glow repo:alice/pebble` does it, as the MCP search tool does. It reaches | |
| 29 | past the cap on repositories an unscoped search reads. A malformed `repo` or two | |
| 30 | different ones is `invalid_repo` (422); an unreadable one is `not_found` (404). |
src/contract/openapi/routes/getApiV1Search.ts +1 −1
| @@ -1,4 +1,4 @@ | ||
| 1 | 1 | import { type Route, limitTo } from "../shared.ts"; |
| 2 | 2 | import { SEARCH_LIMIT_MAX } from "../../../actions/search_limit.ts"; |
| 3 | 3 | |
| 4 | export const route: Route = { method: "get", path: "/api/v1/search", summary: "Search guides, issues, pulls, repositories and code; `kind` narrows it", auth: false, role: "anonymous", query: { q: "string", kind: "string", limit: limitTo(SEARCH_LIMIT_MAX) }, refusals: { invalid_kind: "kind is not all, repos, issues, pulls or code (422)", invalid_limit: "limit is not a whole number from 1 to 100 (422)" }, reply: { repos: "array", issues: "array", code: "array", docs: "array of { title, path, snippet }, at most 5, guides matching; only for kind all", truncated: "object", moreCode: "boolean", codeSkipped: "boolean", note: "string, only when q is blank: what to send" } }; | |
| 4 | export const route: Route = { method: "get", path: "/api/v1/search", summary: "Search guides, issues, pulls, repositories and code; `kind` narrows it", auth: false, role: "anonymous", query: { q: "string", kind: "string", repo: "string", limit: limitTo(SEARCH_LIMIT_MAX) }, refusals: { invalid_kind: "kind is not all, repos, issues, pulls or code (422)", invalid_repo: "repo is not owner/name, or q names a different one with repo: (422)", not_found: "the repository does not exist or is unreadable (404)", invalid_limit: "limit is not a whole number from 1 to 100 (422)" }, reply: { repos: "array", issues: "array", code: "array", docs: "array of { title, path, snippet }, at most 5, guides matching; only for kind all", truncated: "object", moreCode: "boolean", codeSkipped: "boolean", note: "string, only when q is blank: what to send" } }; |
src/http/api.ts +27 −6
| @@ -159,7 +159,7 @@ import { | ||
| 159 | 159 | getRepoById, |
| 160 | 160 | isLanding, |
| 161 | 161 | } from "../core/repos.ts"; |
| 162 | import { CODE_BUDGET_MS, greppable, searchKind, searchNote } from "../core/search.ts"; | |
| 162 | import { CODE_BUDGET_MS, greppable, repoQualifier, searchKind, searchNote } from "../core/search.ts"; | |
| 163 | 163 | import { strangerCodeSearch } from "./strangers.ts"; |
| 164 | 164 | |
| 165 | 165 | import { contributeMint } from "./api/contribute_token.ts"; |
| @@ -1566,6 +1566,17 @@ export function apiRoutes(router: Router<Handler>, isPage: (path: string) => boo | ||
| 1566 | 1566 | |
| 1567 | 1567 | orgRoutes(router); |
| 1568 | 1568 | |
| 1569 | /** | |
| 1570 | * The repository a search's `?repo=`/`repo:` names: 422 if not owner/name; the 404 of a missing | |
| 1571 | * one if unreadable. | |
| 1572 | */ | |
| 1573 | function searchScope(ctx: Ctx, named: string): Repo | Response { | |
| 1574 | const [owner, name, ...rest] = named.split("/"); | |
| 1575 | if (!owner || !name || rest.length > 0) return apiError("invalid_repo", 422, "repo is owner/name"); | |
| 1576 | const repo = getRepo(ctx.app.db, owner, name); | |
| 1577 | return repo !== undefined && canRead(ctx.app.db, repo, ctx.auth.user) ? repo : notFound(); | |
| 1578 | } | |
| 1579 | ||
| 1569 | 1580 | // ── search ─────────────────────────────────────────────────────────── |
| 1570 | 1581 | // |
| 1571 | 1582 | // One query, four sources: repos here, repos cached from every origin the |
| @@ -1585,7 +1596,17 @@ export function apiRoutes(router: Router<Handler>, isPage: (path: string) => boo | ||
| 1585 | 1596 | } |
| 1586 | 1597 | if (queryTooLong(q.get("q"))) return json({ error: "invalid", field: "q", message: QUERY_TOO_LONG }, 422); |
| 1587 | 1598 | const kind = searchKind(q.get("kind")); |
| 1588 | const query = q.get("q") ?? ""; | |
| 1599 | // `repo:owner/name` in the words is the `repo` scope, as on the MCP tool (#16463). | |
| 1600 | const qualified = repoQualifier(q.get("q") ?? ""); | |
| 1601 | const query = qualified.q; | |
| 1602 | if (qualified.repo !== undefined && q.has("repo") && q.get("repo")?.toLowerCase() !== qualified.repo.toLowerCase()) | |
| 1603 | return apiError("invalid_repo", 422, "q names one repository with repo: and repo another: give one"); | |
| 1604 | const named = qualified.repo ?? q.get("repo") ?? undefined; | |
| 1605 | const only = named === undefined ? undefined : searchScope(ctx, named); | |
| 1606 | if (only instanceof Response) return only; | |
| 1607 | const mine = runRepos(ctx.app, ctx.auth); | |
| 1608 | const within = mine === undefined || only === undefined ? mine : mine.filter((r) => r.id === only.id); | |
| 1609 | if (only !== undefined && within !== undefined && within.length === 0) return notFound(); | |
| 1589 | 1610 | const greps = |
| 1590 | 1611 | ctx.auth.user === undefined && |
| 1591 | 1612 | (kind === "code" || kind === "all") && |
| @@ -1598,6 +1619,7 @@ export function apiRoutes(router: Router<Handler>, isPage: (path: string) => boo | ||
| 1598 | 1619 | const input = { |
| 1599 | 1620 | q: query, |
| 1600 | 1621 | kind, |
| 1622 | ...(only !== undefined ? { repo: only } : {}), | |
| 1601 | 1623 | ...(q.has("limit") ? { limit: Number(q.get("limit")) } : {}), |
| 1602 | 1624 | live: q.get("live") === "1" || q.get("live") === "true", |
| 1603 | 1625 | // The page's budget; `code=all` lifts it, as on the page. |
| @@ -1605,12 +1627,11 @@ export function apiRoutes(router: Router<Handler>, isPage: (path: string) => boo | ||
| 1605 | 1627 | ? {} |
| 1606 | 1628 | : { codeBudgetMs: ctx.app.config.codeBudgetMs ?? CODE_BUDGET_MS }), |
| 1607 | 1629 | }; |
| 1608 | const mine = runRepos(ctx.app, ctx.auth); | |
| 1609 | 1630 | const timed = { ...input, phase: (name: SearchPhase, ms: number) => notePhase(ctx.request, name, ms) }; |
| 1610 | const found = await (mine === undefined | |
| 1631 | const found = await (within === undefined | |
| 1611 | 1632 | ? search(ctx.app, ctx.auth.user, timed) |
| 1612 | : searchWithin(ctx.app, ctx.auth.user, mine, timed)); | |
| 1613 | const note = searchNote(q.get("q"), kind); | |
| 1633 | : searchWithin(ctx.app, ctx.auth.user, within, timed)); | |
| 1634 | const note = searchNote(query, kind); | |
| 1614 | 1635 | return timePhase(ctx.request, "render", () => json(note === undefined ? found : { ...found, note })); |
| 1615 | 1636 | } finally { |
| 1616 | 1637 | held?.release(); |
test/mcp-search.test.ts +40
| @@ -10,8 +10,10 @@ import { join } from "node:path"; | ||
| 10 | 10 | import { createApp, type App } from "../src/app.ts"; |
| 11 | 11 | import { createRepo, setArchived } from "../src/actions/repos.ts"; |
| 12 | 12 | import { createIssue } from "../src/core/issues.ts"; |
| 13 | import { mintRunToken } from "../src/core/run_token.ts"; | |
| 13 | 14 | import { createToken } from "../src/core/tokens.ts"; |
| 14 | 15 | import { SEARCH_LIMIT } from "../src/actions/search.ts"; |
| 16 | import { getRepo } from "../src/core/repos.ts"; | |
| 15 | 17 | import { createUser, getUserByName } from "../src/core/users.ts"; |
| 16 | 18 | import { serve, type Servers } from "../src/http/server.ts"; |
| 17 | 19 | |
| @@ -122,6 +124,44 @@ describe("MCP search", () => { | ||
| 122 | 124 | expect((await tool(bobRo, { q: "glow repo:alice/secretpebble" })).error).toContain("no such app or repository"); |
| 123 | 125 | }); |
| 124 | 126 | |
| 127 | test("/api/v1/search takes the same scope, as ?repo= or repo: in q; a second, malformed or unreadable one is refused", async () => { | |
| 128 | const api = async (path: string, token?: string) => { | |
| 129 | const res = await fetch(`${servers.url}/api/v1/search?${path}`, token === undefined ? {} : { headers: { authorization: `Bearer ${token}` } }); | |
| 130 | const text = await res.text(); | |
| 131 | return { status: res.status, body: (text === "" ? {} : JSON.parse(text)) as any }; | |
| 132 | }; | |
| 133 | const names = (r: { body: any }) => r.body.issues.map((i: any) => i.repo.name); | |
| 134 | expect(names(await api("q=glow&kind=issues", aliceRo))).not.toContain("pebble"); | |
| 135 | for (const p of ["q=glow&kind=issues&repo=alice/pebble", "q=glow%20repo:alice/pebble&kind=issues", "q=REPO:alice/pebble%20glow&kind=issues&repo=alice/pebble"]) | |
| 136 | expect(names(await api(p, aliceRo))).toEqual(["pebble"]); | |
| 137 | expect(names(await api("q=glow%20repo:alice/pebble&kind=issues"))).toEqual(["pebble"]); | |
| 138 | expect((await api("q=glow%20repo:alice/pebble&repo=alice/noisy", aliceRo)).body.error).toBe("invalid_repo"); | |
| 139 | for (const repo of ["pebble", "alice/", "a/b/c", ""]) expect((await api(`q=glow&repo=${repo}`, aliceRo)).status).toBe(422); | |
| 140 | expect((await api("q=glow&repo=alice/secretpebble", bobRo)).status).toBe(404); | |
| 141 | expect((await api("q=glow&repo=alice/nothing-here", aliceRo)).status).toBe(404); | |
| 142 | }); | |
| 143 | ||
| 144 | test("/api/v1/search: a run token reaches only its own repositories; repo= outside them is 404, inside them is found; the qualifier's case is no second repository", async () => { | |
| 145 | const alice = getUserByName(app.db, "alice")!; | |
| 146 | const pebble = getRepo(app.db, "alice", "pebble")!; | |
| 147 | const secret = getRepo(app.db, "alice", "secretpebble")!; | |
| 148 | const run = mintRunToken(app.db, alice.id, "run-search", { repoId: pebble.id, forkId: secret.id }, new Date(Date.now() + 3_600_000).toISOString()).token; | |
| 149 | const api = async (path: string, token: string) => { | |
| 150 | const res = await fetch(`${servers.url}/api/v1/search?${path}`, { headers: { authorization: `Bearer ${token}` } }); | |
| 151 | return { status: res.status, body: (await res.json()) as any }; | |
| 152 | }; | |
| 153 | // alice reads noisy, the run token does not. | |
| 154 | expect((await api("q=glow&kind=issues&repo=alice/noisy", aliceRo)).status).toBe(200); | |
| 155 | const outside = await api("q=glow&kind=issues&repo=alice/noisy", run); | |
| 156 | expect(outside.status).toBe(404); | |
| 157 | expect(outside.body.issues).toBeUndefined(); | |
| 158 | const inside = await api("q=glow&kind=issues&repo=alice/pebble", run); | |
| 159 | expect(inside.status).toBe(200); | |
| 160 | expect(inside.body.issues.map((i: any) => i.repo.name)).toEqual(["pebble"]); | |
| 161 | // The qualifier in another case names the same repository: one scope, not two. | |
| 162 | expect((await api("q=glow%20repo:alice/PEBBLE&kind=issues&repo=alice/pebble", aliceRo)).status).toBe(200); | |
| 163 | }); | |
| 164 | ||
| 125 | 165 | test("an unknown kind or a bad limit is refused in the API's words; every kind answers", async () => { |
| 126 | 166 | expect((await tool(aliceRo, { q: "a", kind: "bogus" })).error).toContain('argument "kind" is "bogus"'); |
| 127 | 167 | expect((await tool(aliceRo, { q: "a", kind: "bogus" })).error).toContain('one of "all", "issues", "repos", "code"'); |
new above
offline
This helper splits the route's own comment (
// caller has connected… live=1 …) from the route it describes. Move it above that comment block.