diff --git a/pm-stack/aggregator/src/config.ts b/pm-stack/aggregator/src/config.ts index f1b0a2094a..bcaaa0e08e 100644 --- a/pm-stack/aggregator/src/config.ts +++ b/pm-stack/aggregator/src/config.ts @@ -69,6 +69,9 @@ export interface Config { origin: string; email: string | null; password: string | null; + /** Authoritative project slug -> repo registry (#1201): + * pm-stack/kaneo-projects.json. Unknown slugs stay unmapped. */ + projectsFile: string; }; grist: { baseUrl: string; @@ -136,6 +139,9 @@ export function loadConfig(env: Record = process.env origin: s(env.KANEO_ORIGIN, s(env.KANEO_CLIENT_URL, "https://kaneo.security.cab")), email: env.KANEO_ADMIN_EMAIL ?? null, password: env.KANEO_ADMIN_PASSWORD ?? null, + // The registry is authoritative for slug -> repo (#1201); the reader + // runs with cwd=pm-stack/aggregator, hence the sibling path. + projectsFile: s(env.PM_KANEO_PROJECTS_FILE, join(process.cwd(), "..", "kaneo-projects.json")), }, grist: { baseUrl: s(env.GRIST_URL, "http://127.0.0.1:8484"), diff --git a/pm-stack/aggregator/src/sources/live.ts b/pm-stack/aggregator/src/sources/live.ts index d4e4e0cdbd..6e7b65d227 100644 --- a/pm-stack/aggregator/src/sources/live.ts +++ b/pm-stack/aggregator/src/sources/live.ts @@ -114,13 +114,25 @@ interface KaneoColumn { tasks: KaneoTask[]; } -/** Kaneo project slug -> GitHub repo (pm-stack/kaneo-projects.json, Session H). */ -const KANEO_PROJECT_REPOS: Record = { - code: "developing-today/code", - opencode: "developing-today/opencode", - openchamber: "developing-today/openchamber", - nix: "dezren39/nix", -}; +/** Kaneo project slug -> GitHub repo, read authoritatively from + * pm-stack/kaneo-projects.json (#1201). Unknown slugs stay unmapped + * (repo "") — never guessed — so no invented repo-based refs appear. + * An unreadable registry degrades to the same empty map. */ +async function kaneoProjectRepos(cfg: Config): Promise> { + try { + const raw = await readFile(cfg.kaneo.projectsFile, "utf8"); + const doc = JSON.parse(raw) as { projects?: { slug?: unknown; repository?: unknown }[] }; + const map: Record = {}; + for (const p of doc.projects ?? []) { + if (typeof p?.slug === "string" && typeof p?.repository === "string" && p.repository.includes("/")) { + map[p.slug] = p.repository; + } + } + return map; + } catch { + return {}; + } +} async function kaneoProjects( cfg: Config, @@ -133,11 +145,14 @@ async function kaneoProjects( if (!workspaceId) throw new Error("kaneo workspace empty"); const proj = await getJson(`${cfg.kaneo.baseUrl}/api/project?workspaceId=${workspaceId}`, { Cookie: cookie }); if (!proj.ok) throw new Error("kaneo project read failed"); + // #1201: the registry is the only source of slug -> repo. Unknown + // slugs stay unmapped (repo "") instead of guessing a repo name. + const repos = await kaneoProjectRepos(cfg); return (proj.json as { id: string; slug: string; name: string }[]).map((p) => ({ id: p.id, slug: p.slug, name: p.name, - repo: KANEO_PROJECT_REPOS[p.slug] ?? `developing-today/${p.slug}`, + repo: repos[p.slug] ?? "", workspaceId, })); } @@ -176,10 +191,13 @@ async function kaneoBoard(cfg: Config, cookie: string): Promise { if (isRetiredDoneTableTitle(t.title)) continue; const links: Link[] = (t.externalLinks ?? []) .filter((l) => l.resourceType === "issue" || l.resourceType === "pull_request") - .map((l) => { + .flatMap((l) => { + // #1201: only a GitHub URL names a repo. A non-GitHub URL — + // or an unmapped project — yields no repo-based ref at all; + // refs are never invented from a guessed repo. const m = /github\.com\/([\w.-]+\/[\w.-]+)\//.exec(l.url); - const repo = m ? m[1] : p.repo; - return { ref: `${repo}#${l.externalId}`, relation: "tracks" as const }; + if (!m) return []; + return [{ ref: `${m[1]}#${l.externalId}`, relation: "tracks" as const }]; }); out.push({ // Registry subject: kaneo:/ (Session J). diff --git a/pm-stack/aggregator/test/kaneo-project-repos-1201.test.ts b/pm-stack/aggregator/test/kaneo-project-repos-1201.test.ts new file mode 100644 index 0000000000..3c35445533 --- /dev/null +++ b/pm-stack/aggregator/test/kaneo-project-repos-1201.test.ts @@ -0,0 +1,149 @@ +import { describe, expect, test } from "bun:test"; +import { mkdtemp, writeFile } from "node:fs/promises"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { loadConfig } from "../src/config.ts"; +import { liveSource } from "../src/sources/live.ts"; + +// #1201: the Kaneo reader must use pm-stack/kaneo-projects.json as the +// authoritative project-repo map. Unknown slugs stay unmapped (repo "") +// instead of guessing `developing-today/`, and non-GitHub external +// URLs never produce invented repo-based refs — while GitHub-URL links +// and the Kaneo card link keep working. + +const FEED = { + version: 1, + generatedAt: new Date().toISOString(), + quota: { pools: [], burn: [], choices: { hasHeadroom: [], burnBeforeReset: [], doNotStart: [] } }, + attention: [], +}; + +async function driveBoard(registry: unknown, projects: { id: string; slug: string; name: string }[]) { + const dir = await mkdtemp(join(tmpdir(), "pm-kaneo-repos-")); + const feedFile = join(dir, "fleet-feed.json"); + await writeFile(feedFile, JSON.stringify(FEED)); + const projectsFile = join(dir, "kaneo-projects.json"); + await writeFile(projectsFile, JSON.stringify(registry)); + const realFetch = globalThis.fetch; + globalThis.fetch = (async (input: unknown) => { + const url = String(input); + const json = (body: unknown, headers: Record = {}) => + new Response(JSON.stringify(body), { status: 200, headers: { "Content-Type": "application/json", ...headers } }); + if (url.includes("/api/auth/sign-in/email")) return json({}, { "set-cookie": "session=test; Path=/" }); + if (url.includes("/api/workspace")) return json([{ id: "ws1" }]); + if (url.includes("/api/project?")) return json(projects); + if (url.includes("/api/task/tasks/")) { + const pid = url.split("/api/task/tasks/")[1]!.split("?")[0]; + return json({ + data: { + columns: [ + { + id: "in-progress", + name: "In Progress", + tasks: [ + { + id: `t-${pid}`, + number: 7, + title: `Work in ${pid}`, + status: "in-progress", + externalLinks: [ + { resourceType: "issue", externalId: "42", url: "https://github.com/example-org/example-repo/issues/42" }, + { resourceType: "issue", externalId: "9", url: "https://linear.app/team/issue/XYZ-9" }, + ], + }, + ], + }, + ], + }, + pagination: { totalPages: 1 }, + }); + } + return new Response("not found", { status: 404 }); + }) as typeof fetch; + try { + const cfg = loadConfig({ + ...Bun.env, + PM_AGG_SOURCE: "live", + PM_AGG_FEED_FILE: feedFile, + PM_PROJECTOR_URL: "off", + PM_KANEO_PROJECTS_FILE: projectsFile, + KANEO_URL: "http://kaneo.test", + KANEO_ADMIN_EMAIL: "test@example.com", + KANEO_ADMIN_PASSWORD: "test-password", + }); + return (await liveSource(cfg).board()).filter((b) => b.source === "kaneo"); + } finally { + globalThis.fetch = realFetch; + } +} + +describe("live board: Kaneo project-repo registry is authoritative (#1201)", () => { + test("mapped slug uses the registry repo; unknown slug stays unmapped", async () => { + const board = await driveBoard( + { projects: [{ slug: "code", projectId: "p1", repository: "example-org/example-repo", installationId: 1 }] }, + [ + { id: "p1", slug: "code", name: "Code" }, + { id: "pX", slug: "mystery", name: "Mystery" }, + ], + ); + const known = board.find((b) => b.title === "Work in p1"); + const unknown = board.find((b) => b.title === "Work in pX"); + expect(known?.repo).toBe("example-org/example-repo"); + // Unmapped — never a guessed `developing-today/mystery`. + expect(unknown?.repo).toBe(""); + expect(unknown?.repo).not.toContain("mystery"); + }); + + test("GitHub-URL links survive; non-GitHub URLs invent no refs; card link intact", async () => { + const board = await driveBoard( + { projects: [{ slug: "code", projectId: "p1", repository: "example-org/example-repo", installationId: 1 }] }, + [{ id: "p1", slug: "code", name: "Code" }], + ); + const card = board.find((b) => b.title === "Work in p1"); + expect(card).toBeDefined(); + // The GitHub-URL external link keeps its exact ref. + expect(card?.links.map((l) => l.ref)).toContain("example-org/example-repo#42"); + // The Linear URL produces no invented `example-org/example-repo#9` ref. + expect(card?.links.map((l) => l.ref)).not.toContain("example-org/example-repo#9"); + expect(card?.links).toHaveLength(1); + // The Kaneo card deep link is untouched. + expect(card?.kaneoUrl).toContain("kaneo"); + }); + + test("unreadable registry degrades to unmapped, not guessed", async () => { + const dir = await mkdtemp(join(tmpdir(), "pm-kaneo-repos-")); + const feedFile = join(dir, "fleet-feed.json"); + await writeFile(feedFile, JSON.stringify(FEED)); + const realFetch = globalThis.fetch; + globalThis.fetch = (async (input: unknown) => { + const url = String(input); + const json = (body: unknown, headers: Record = {}) => + new Response(JSON.stringify(body), { status: 200, headers: { "Content-Type": "application/json", ...headers } }); + if (url.includes("/api/auth/sign-in/email")) return json({}, { "set-cookie": "session=test; Path=/" }); + if (url.includes("/api/workspace")) return json([{ id: "ws1" }]); + if (url.includes("/api/project?")) return json([{ id: "p1", slug: "code", name: "Code" }]); + if (url.includes("/api/task/tasks/")) + return json({ + data: { columns: [{ id: "in-progress", name: "In Progress", tasks: [{ id: "t1", number: 1, title: "T", status: "in-progress" }] }] }, + pagination: { totalPages: 1 }, + }); + return new Response("not found", { status: 404 }); + }) as typeof fetch; + try { + const cfg = loadConfig({ + ...Bun.env, + PM_AGG_SOURCE: "live", + PM_AGG_FEED_FILE: feedFile, + PM_PROJECTOR_URL: "off", + PM_KANEO_PROJECTS_FILE: join(dir, "does-not-exist.json"), + KANEO_URL: "http://kaneo.test", + KANEO_ADMIN_EMAIL: "test@example.com", + KANEO_ADMIN_PASSWORD: "test-password", + }); + const board = (await liveSource(cfg).board()).filter((b) => b.source === "kaneo"); + expect(board[0]?.repo).toBe(""); + } finally { + globalThis.fetch = realFetch; + } + }); +});