diff --git a/apps/gittensory-extension/content.js b/apps/gittensory-extension/content.js index 7d0df77575..1f39e4fb51 100644 --- a/apps/gittensory-extension/content.js +++ b/apps/gittensory-extension/content.js @@ -1,14 +1,21 @@ -const target = matchPullRequestTarget(location.pathname); +const target = matchGitHubPageTarget(location.pathname); -if (target) { +if (target?.kind === "pull_request") { mountOverlay(target); } -function matchPullRequestTarget(pathname) { - const match = String(pathname ?? "").match(/^\/([^/]+)\/([^/]+)\/pull\/(\d+)(?:\/|$)/); +function matchGitHubPageTarget(pathname) { + const match = String(pathname ?? "").match(/^\/([^/]+)\/([^/]+)\/(pull|issues)\/(\d+)(?:\/|$)/); if (!match) return null; - const [, owner, repo, pullNumber] = match; - return { owner, repo, pullNumber: Number(pullNumber) }; + const [, owner, repo, surface, number] = match; + if (surface === "pull") return { kind: "pull_request", owner, repo, pullNumber: Number(number) }; + return { kind: "issue", owner, repo, issueNumber: Number(number) }; +} + +function matchPullRequestTarget(pathname) { + const target = matchGitHubPageTarget(pathname); + if (target?.kind !== "pull_request") return null; + return { owner: target.owner, repo: target.repo, pullNumber: target.pullNumber }; } function mountOverlay(target) { @@ -176,6 +183,7 @@ function renderActions(body, actions) { if (globalThis.__GITTENSORY_EXTENSION_TEST__) { globalThis.__gittensoryContentInternals = { + matchGitHubPageTarget, matchPullRequestTarget, renderPullContext, renderSection, diff --git a/apps/gittensory-extension/manifest.json b/apps/gittensory-extension/manifest.json index cfaaf86efd..bcd429f9af 100644 --- a/apps/gittensory-extension/manifest.json +++ b/apps/gittensory-extension/manifest.json @@ -11,7 +11,7 @@ }, "content_scripts": [ { - "matches": ["https://github.com/*/*/pull/*"], + "matches": ["https://github.com/*/*/pull/*", "https://github.com/*/*/issues/*"], "js": ["content.js"], "css": ["styles.css"], "run_at": "document_idle" diff --git a/test/unit/extension-content.test.ts b/test/unit/extension-content.test.ts index 563cad626a..42bc8fd084 100644 --- a/test/unit/extension-content.test.ts +++ b/test/unit/extension-content.test.ts @@ -5,9 +5,22 @@ import { describe, expect, it, vi } from "vitest"; const contentScript = readFileSync("apps/gittensory-extension/content.js", "utf8"); describe("extension content script", () => { - it("matches only GitHub pull request routes", () => { + it("detects GitHub pull request and issue routes while only mounting pull overlays", () => { const internals = loadContentInternals(); + expect(internals.matchGitHubPageTarget("/JSONbored/gittensory/pull/146")).toEqual({ + kind: "pull_request", + owner: "JSONbored", + repo: "gittensory", + pullNumber: 146, + }); + expect(internals.matchGitHubPageTarget("/JSONbored/gittensory/issues/145")).toEqual({ + kind: "issue", + owner: "JSONbored", + repo: "gittensory", + issueNumber: 145, + }); + expect(internals.matchGitHubPageTarget("/JSONbored/gittensory/pulls")).toBeNull(); expect(internals.matchPullRequestTarget("/JSONbored/gittensory/pull/146")).toEqual({ owner: "JSONbored", repo: "gittensory", @@ -76,6 +89,9 @@ function loadContentInternals() { const vmContext = createContext(context); new Script(contentScript).runInContext(vmContext); return vmContext.__gittensoryContentInternals as { + matchGitHubPageTarget: ( + pathname: string, + ) => { kind: "pull_request"; owner: string; repo: string; pullNumber: number } | { kind: "issue"; owner: string; repo: string; issueNumber: number } | null; matchPullRequestTarget: (pathname: string) => { owner: string; repo: string; pullNumber: number } | null; renderPullContext: (payload: unknown) => string; };