From 21310d0366c0f21e73c1f4f78d38370edfb59445 Mon Sep 17 00:00:00 2001 From: Joe Mou Date: Tue, 11 Aug 2026 17:41:01 -0400 Subject: Read the About box and latest release from JSON GitHub rebuilt the repository sidebar, so the selectors parseRepoInfo used matched nothing and the About box rendered empty. Everything but the latest release is in the page's embedded payload as sidebarAbout; the latest release is only in the _sidebar JSON already fetched for languages, so it arrives with that fragment and is moved beside the release count. --- cgithub/public/static/refs.js | 10 ++++ cgithub/src/app.test.ts | 34 ++++++++++++ cgithub/src/app.ts | 6 +- cgithub/src/scraper.test.ts | 15 ++--- cgithub/src/scraper.ts | 126 ++++++++++++++---------------------------- cgithub/views/_languages.eta | 17 ------ cgithub/views/_sidebar.eta | 22 ++++++++ cgithub/views/repo.eta | 10 +--- 8 files changed, 122 insertions(+), 118 deletions(-) delete mode 100644 cgithub/views/_languages.eta create mode 100644 cgithub/views/_sidebar.eta (limited to 'cgithub') diff --git a/cgithub/public/static/refs.js b/cgithub/public/static/refs.js index f3e07e8..1035593 100644 --- a/cgithub/public/static/refs.js +++ b/cgithub/public/static/refs.js @@ -52,10 +52,20 @@ document.addEventListener("DOMContentLoaded", () => { .then((html) => { if (html.trim()) { placeholder.outerHTML = html; + relocate(); } else { placeholder.remove(); } }) .catch(() => placeholder.remove()); } + + // A fragment may carry a node that belongs elsewhere on the page, where an + // empty element of that id holds its spot: the sidebar's latest release + // reads as part of the release count, far from the languages it arrives with. + function relocate() { + for (const node of document.querySelectorAll("[data-target]")) { + document.getElementById(node.dataset.target)?.replaceWith(node); + } + } }); diff --git a/cgithub/src/app.test.ts b/cgithub/src/app.test.ts index 6544f03..5474f8d 100644 --- a/cgithub/src/app.test.ts +++ b/cgithub/src/app.test.ts @@ -58,6 +58,40 @@ describe("branch picker", () => { }); }); +describe("sidebar fragment", () => { + const repo = { owner: "actions", name: "deploy-pages" }; + const languages = [{ name: "JavaScript", percentage: 100, color: "#f1e05a" }]; + const latestRelease = { tag: "v5.0.0", publishedAt: "2026-03-25T16:59:14Z" }; + + // The release replaces the empty element it names, beside the release count. + it("should render the latest release and the languages", () => { + const html = eta.render("_sidebar.eta", { repo, languages, latestRelease }); + + assert.match(html, /\(2026-03-25<\/time>/); + assert.match(html, /
/); + }); + + it("should name a spot the repo page holds open", () => { + const html = eta.render("repo.eta", { + repo, + branch: "main", + path: "/", + items: [], + info: { description: null, website: null, stars: null, forks: null, numReleases: 38 }, + }); + + assert.match(html, /<\/span>/); + }); + + it("should render nothing when the repository has neither", () => { + const html = eta.render("_sidebar.eta", { repo, languages: [], latestRelease: null }); + + assert.strictEqual(html.trim(), ""); + }); +}); + describe("redirects to GitHub", () => { // An HTTP redirect would be caught by the extension's declarativeNetRequest // rules and bounced straight back here. diff --git a/cgithub/src/app.ts b/cgithub/src/app.ts index 81e47fd..e27db59 100644 --- a/cgithub/src/app.ts +++ b/cgithub/src/app.ts @@ -6,7 +6,7 @@ import { getGitHubBlob, getGitHubCommits, getGitHubIssues, - getGitHubLanguages, + getGitHubSidebar, getGitHubLatestCommit, getGitHubOwner, getGitHubPulls, @@ -178,9 +178,9 @@ export function createApp(eta: Eta) { ); }); - app.get("/api/:owner/:repo/languages", async (c) => { + app.get("/api/:owner/:repo/sidebar", async (c) => { const { owner, repo } = c.req.param(); - return tryRenderFragment(c, "_languages.eta", getGitHubLanguages(owner, repo)); + return tryRenderFragment(c, "_sidebar.eta", getGitHubSidebar(owner, repo)); }); app.get("/:owner/:repo/releases/tag/:tag", async (c) => { diff --git a/cgithub/src/scraper.test.ts b/cgithub/src/scraper.test.ts index 5b5e78b..24fdfc0 100644 --- a/cgithub/src/scraper.test.ts +++ b/cgithub/src/scraper.test.ts @@ -4,7 +4,7 @@ import { getGitHubBlob, getGitHubCommits, getGitHubIssues, - getGitHubLanguages, + getGitHubSidebar, getGitHubLatestCommit, getGitHubPulls, getGitHubRefs, @@ -35,8 +35,6 @@ describe("GitHub scraper", () => { assert.strictEqual(data.info.stars?.length, 3); assert.strictEqual(data.info.forks?.length, 3); assert.strictEqual(data.info.numReleases, 38); - assert.strictEqual(data.info.latestReleaseTag, "v5.0.0"); - assert.ok(data.info.latestReleaseDate?.startsWith("2026-03-25")); assert.ok(data.items.length > 0); assert.ok(typeof data.overviewHtml?.["README.md"] === "string"); @@ -220,8 +218,8 @@ describe("GitHub scraper", () => { assert.ok(data.commit.authors.length > 0); }); - it("should fetch languages", async () => { - const data = await getGitHubLanguages("actions", "deploy-pages"); + it("should fetch the sidebar", async () => { + const data = await getGitHubSidebar("actions", "deploy-pages"); assert.strictEqual(data.repo.owner, "actions"); assert.strictEqual(data.repo.name, "deploy-pages"); @@ -229,10 +227,13 @@ describe("GitHub scraper", () => { assert.deepStrictEqual(data.languages, [ { name: "JavaScript", percentage: 100, color: "#f1e05a" }, ]); + + assert.strictEqual(data.latestRelease?.tag, "v5.0.0"); + assert.ok(data.latestRelease?.publishedAt?.startsWith("2026-03-25")); }); - it("should fetch languages for a multi-language repository", async () => { - const data = await getGitHubLanguages("rust-lang", "rust"); + it("should fetch the sidebar for a multi-language repository", async () => { + const data = await getGitHubSidebar("rust-lang", "rust"); const rust = data.languages.find((language) => language.name === "Rust"); assert.ok(rust); diff --git a/cgithub/src/scraper.ts b/cgithub/src/scraper.ts index 223839f..4220dc4 100644 --- a/cgithub/src/scraper.ts +++ b/cgithub/src/scraper.ts @@ -143,7 +143,17 @@ interface CodeViewBlobRoute { richText: string | null; } +interface SidebarAbout { + description?: string | null; + website?: string | null; + stargazerCount?: number; + forksCount?: number; + // A section is false when the repository has none of that thing. + sections?: { releases?: { releaseCount?: number } | false }; +} + interface AppPayload { + sidebarAbout?: SidebarAbout; codeViewLayoutRoute?: CodeViewLayoutRoute; codeViewRepoRoute?: CodeViewRepoRoute; codeViewTreeRoute?: CodeViewTreeRoute; @@ -230,8 +240,6 @@ export interface RepoInfo { stars: string | null; forks: string | null; numReleases?: number; - latestReleaseTag?: string; - latestReleaseDate?: string; } export interface GitHubRepo extends GitHubTree { @@ -292,8 +300,14 @@ export interface Language { color: string | null; } -export interface GitHubLanguages extends GitHubCommon { +export interface LatestRelease { + tag: string; + publishedAt: string | null; +} + +export interface GitHubSidebar extends GitHubCommon { languages: Language[]; + latestRelease: LatestRelease | null; } export interface Ref { @@ -425,81 +439,6 @@ function parsePayload(html: string): AppPayload | null { return parseEmbeddedPayload(html); } -function parseRepoInfo(html: string): RepoInfo { - const document = parseDocument(html); - - const aboutSection = cssSelect.selectOne(".BorderGrid-cell h2:contains('About')", document); - let description: string | null = null; - let website: string | null = null; - - if (aboutSection) { - const parent = aboutSection.parent; - if (parent) { - const descElem = cssSelect.selectOne("p.f4", parent); - if (descElem) { - description = textContent(descElem).trim(); - } - const websiteElem = cssSelect.selectOne( - "a[rel='noopener noreferrer nofollow']", - parent, - ) as Element | null; - if (websiteElem) { - website = getAttributeValue(websiteElem, "href") || null; - } - } - } - - const starsElem = cssSelect.selectOne("#repo-stars-counter-star", document) as Element | null; - const stars = starsElem - ? getAttributeValue(starsElem, "title") || textContent(starsElem).trim() - : null; - - const forksElem = cssSelect.selectOne("#repo-network-counter", document) as Element | null; - const forks = forksElem - ? getAttributeValue(forksElem, "title") || textContent(forksElem).trim() - : null; - - const releaseElem = cssSelect.selectOne( - "a[href$='/releases'] .Counter", - document, - ) as Element | null; - let numReleases: number | undefined; - let latestReleaseTag: string | undefined; - let latestReleaseDate: string | undefined; - - if (releaseElem) { - const releaseText = getAttributeValue(releaseElem, "title") || textContent(releaseElem); - numReleases = parseInt(releaseText.replace(/,/g, ""), 10); - - // Find the latest release tag and date if available - const cell = (releaseElem.parent as Element)?.parent?.parent as Element; // span -> a -> h2 -> cell - if (cell) { - const latestLink = cssSelect.selectOne("a[href*='/releases/tag/']", cell) as Element; - if (latestLink) { - const tagElem = cssSelect.selectOne(".css-truncate-target", latestLink); - if (tagElem) { - latestReleaseTag = textContent(tagElem).trim(); - } - - const timeElem = cssSelect.selectOne("relative-time", latestLink); - if (timeElem) { - latestReleaseDate = getAttributeValue(timeElem, "datetime") || undefined; - } - } - } - } - - return { - description, - website, - stars, - forks, - numReleases, - latestReleaseTag, - latestReleaseDate, - }; -} - function extractOverviewHtml( overviewFiles: OverviewFile[] = [], ): Record | undefined { @@ -539,7 +478,15 @@ export async function getGitHubRepo(owner: string, repo: string): Promise { +// The repository sidebar (languages, releases, contributors, ...) is not +// rendered into the repo page at all; GitHub's own UI lazily fetches it as +// JSON from here. +export async function getGitHubSidebar(owner: string, repo: string): Promise { const payload = await fetchGitHubJson(`${owner}/${repo}/_sidebar`); const languages: Language[] = []; @@ -758,7 +710,15 @@ export async function getGitHubLanguages(owner: string, repo: string): Promise 0) { %> -
-
- <% for (const lang of it.languages) { %> - " title="<%= lang.name %> <%= lang.percentage %>%"> - <% } %> -
-
    - <% for (const lang of it.languages) { %> -
  • - "> - <%= lang.name %> <%= lang.percentage %>% -
  • - <% } %> -
-
-<% } %> diff --git a/cgithub/views/_sidebar.eta b/cgithub/views/_sidebar.eta new file mode 100644 index 0000000..bceef7c --- /dev/null +++ b/cgithub/views/_sidebar.eta @@ -0,0 +1,22 @@ +<% if (it.latestRelease) { %> + (
<%= it.latestRelease.tag %><% + if (it.latestRelease.publishedAt) { %> <% + } %>) +<% } %> +<% if (it.languages.length > 0) { %> +
+
+ <% for (const lang of it.languages) { %> + " title="<%= lang.name %> <%= lang.percentage %>%"> + <% } %> +
+
    + <% for (const lang of it.languages) { %> +
  • + "> + <%= lang.name %> <%= lang.percentage %>% +
  • + <% } %> +
+
+<% } %> diff --git a/cgithub/views/repo.eta b/cgithub/views/repo.eta index 1bff97f..5dc6beb 100644 --- a/cgithub/views/repo.eta +++ b/cgithub/views/repo.eta @@ -17,19 +17,13 @@ <% if (it.info.numReleases) { %>
<%= it.info.numReleases %> releases - <% if (it.info.latestReleaseTag) { %> - (<%= it.info.latestReleaseTag %> - <% if (it.info.latestReleaseDate) { %> - - <% } %> - ) - <% } %> +
<% } %>
<% } %> -
+
  • …
-- cgit v1.3.1