diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/index.ts | 6 | ||||
| -rw-r--r-- | src/scraper.test.ts | 13 | ||||
| -rw-r--r-- | src/scraper.ts | 93 |
3 files changed, 89 insertions, 23 deletions
diff --git a/src/index.ts b/src/index.ts index 2f8efef..59c98ee 100644 --- a/src/index.ts +++ b/src/index.ts @@ -9,6 +9,7 @@ import { getGitHubBlob, getGitHubCommits, getGitHubIssues, + getGitHubRelease, getGitHubReleases, getGitHubRepo, getGitHubTree, @@ -74,6 +75,11 @@ app.get("/:owner/:repo/commits/:branch/:path{.*}?", async (c) => { return tryRender(c, "commits.eta", getGitHubCommits(owner, repo, branch, path)); }); +app.get("/:owner/:repo/releases/tag/:tag", async (c) => { + const { owner, repo, tag } = c.req.param(); + return tryRender(c, "release.eta", getGitHubRelease(owner, repo, tag)); +}); + app.get("/:owner/:repo/releases", async (c) => { const { owner, repo } = c.req.param(); return tryRender(c, "releases.eta", getGitHubReleases(owner, repo)); diff --git a/src/scraper.test.ts b/src/scraper.test.ts index 33f00f1..0b91b6a 100644 --- a/src/scraper.test.ts +++ b/src/scraper.test.ts @@ -4,6 +4,7 @@ import { getGitHubBlob, getGitHubCommits, getGitHubIssues, + getGitHubRelease, getGitHubReleases, getGitHubRepo, getGitHubTree, @@ -174,6 +175,18 @@ describe("GitHub scraper", () => { assert.ok(firstRelease.publishedAt.length > 0); assert.ok(firstRelease.bodyHtml.length > 0); }); + + it("should fetch single release", async () => { + const data = await getGitHubRelease("actions", "deploy-pages", "v4.0.5"); + + assert.strictEqual(data.repo.owner, "actions"); + assert.strictEqual(data.repo.name, "deploy-pages"); + + assert.ok(data.release.tagName.length > 0); + assert.ok(data.release.title.length > 0); + assert.ok(data.release.publishedAt.length > 0); + assert.ok(data.release.bodyHtml.length > 0); + }); }); describe("rich file types", () => { diff --git a/src/scraper.ts b/src/scraper.ts index af39660..acfaac1 100644 --- a/src/scraper.ts +++ b/src/scraper.ts @@ -200,6 +200,10 @@ export interface GitHubReleases extends GitHubCommon { releases: Release[]; } +export interface GitHubRelease extends GitHubCommon { + release: Release; +} + async function fetchGitHubPage(path: string): Promise<string> { // GitHub throttles/blocks requests without realistic browser headers. // These headers make the request appear as a standard browser visit. @@ -420,6 +424,33 @@ export async function getGitHubCommits( }; } +function parseRelease(section: Element, title: string): Release { + // Extract tag name from link + const tagLink = cssSelect.selectOne('a[href*="/tree/"]', section); + let tagName = title; + if (tagLink) { + const href = getAttributeValue(tagLink, "href"); + if (href) { + tagName = href.split("/").pop() || title; + } + } + + // Extract published date + const relativeTime = cssSelect.selectOne("relative-time", section); + const publishedAt = relativeTime ? getAttributeValue(relativeTime, "datetime") || "" : ""; + + // Extract body HTML (markdown content) + const body = cssSelect.selectOne(".markdown-body", section); + const bodyHtml = body ? getInnerHTML(body).trim() : ""; + + return { + tagName, + title, + publishedAt, + bodyHtml, + }; +} + export async function getGitHubReleases(owner: string, repo: string): Promise<GitHubReleases> { const html = await fetchGitHubPage(`${owner}/${repo}/releases`); @@ -436,37 +467,53 @@ export async function getGitHubReleases(owner: string, repo: string): Promise<Gi const title = textContent(h2).trim(); if (!title.startsWith("v")) continue; - // Extract tag name from link - const tagLink = cssSelect.selectOne('a[href*="/tree/"]', section); - let tagName = title; - if (tagLink) { - const href = getAttributeValue(tagLink, "href"); - if (href) { - tagName = href.split("/").pop() || title; - } - } + releases.push(parseRelease(section, title)); + } + + return { + repo: { + owner, + name: repo, + }, + releases, + }; +} - // Extract published date - const relativeTime = cssSelect.selectOne("relative-time", section); - const publishedAt = relativeTime ? getAttributeValue(relativeTime, "datetime") || "" : ""; +export async function getGitHubRelease( + owner: string, + repo: string, + tag: string, +): Promise<GitHubRelease> { + const html = await fetchGitHubPage(`${owner}/${repo}/releases/tag/${tag}`); - // Extract body HTML (markdown content) - const body = cssSelect.selectOne(".markdown-body", section); - const bodyHtml = body ? getInnerHTML(body).trim() : ""; + // Find the h1 tag with the release title + const h1Match = html.match(/<h1[^>]*class="[^"]*d-inline[^"]*"[^>]*>([^<]*)<\/h1>/); + const title = h1Match?.[1]?.trim() || tag; - releases.push({ - tagName, - title, - publishedAt, - bodyHtml, - }); - } + // Extract tag name from tree link + const tagLinkMatch = html.match(/<a[^>]*href="[^"]*\/tree\/([^"]+)"[^>]*>/); + const tagName = tagLinkMatch?.[1] || tag; + + // Extract published date + const dateMatch = html.match(/<relative-time[^>]*datetime="([^"]*)"[^>]*>/); + const publishedAt = dateMatch?.[1] || ""; + + // Extract body HTML (markdown content) - find the markdown-body div + const bodyMatch = html.match( + /<div[^>]*class="[^"]*markdown-body[^"]*"[^>]*>([\s\S]*?)<\/div>\s*<div[^>]*class="mb-3"/, + ); + const bodyHtml = bodyMatch?.[1]?.trim() || ""; return { repo: { owner, name: repo, }, - releases, + release: { + tagName, + title, + publishedAt, + bodyHtml, + }, }; } |
