Something went wrong. Try again.
move github sites
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198// Walk every repo an owner has and work out which ones are serving a website.//// GitHub reports Pages state in three places that disagree with each other:// the repo object's has_pages flag (stale for years after a site is turned off),// the /pages endpoint (authoritative config), and DNS + a live fetch (the only// thing that says whether anyone can actually reach the site). We collect all// three and let the report show where they diverge.
import dns from 'node:dns/promises'import { api, paginate, pmap } from './github.mjs'import { detect, detectPagesActions, verdict } from './detect.mjs'
const GH_PAGES_IPS = new Set([ '185.199.108.153', '185.199.109.153', '185.199.110.153', '185.199.111.153',])
export async function listRepos(owner, { includeForks = false, includeArchived = true } = {}) { // The authenticated user's own repos come from /user/repos so private ones are // included; anything else has to go through /users/{owner}/repos or /orgs. const me = await api('/user') const isSelf = me.ok && me.body.login.toLowerCase() === owner.toLowerCase()
let repos if (isSelf) { repos = await paginate('/user/repos?affiliation=owner&sort=full_name') } else { const org = await api(`/orgs/${owner}`) repos = await paginate(org.ok ? `/orgs/${owner}/repos` : `/users/${owner}/repos`) }
// Forks are noise by default, but a fork with Pages turned on is still a site // we are hosting, so those stay in regardless. return repos.filter( (r) => (includeForks || !r.fork || r.has_pages) && (includeArchived || !r.archived) && !r.is_template )}
async function pagesConfig(repo) { const res = await api(`/repos/${repo.full_name}/pages`) if (res.status === 404) return null if (!res.ok) return { error: `${res.status}` } const p = res.body return { url: p.html_url, status: p.status, // built | building | errored | null cname: p.cname, buildType: p.build_type, // legacy (serve a branch) | workflow (Actions) sourceBranch: p.source?.branch ?? null, sourcePath: p.source?.path ?? '/', httpsEnforced: p.https_enforced, public: p.public, }}
async function treePaths(repo, branch) { if (!branch) return { paths: [], truncated: false } const res = await api( `/repos/${repo.full_name}/git/trees/${encodeURIComponent(branch)}?recursive=1` ) if (!res.ok) return { paths: [], truncated: false, error: `${res.status}` } return { paths: (res.body.tree || []).filter((n) => n.type === 'blob').map((n) => n.path), truncated: !!res.body.truncated, }}
async function fileText(repo, branch, path) { const res = await api( `/repos/${repo.full_name}/contents/${path}?ref=${encodeURIComponent(branch)}`, { raw: true } ) return res.ok ? res.body : null}
// Does the custom domain actually point at GitHub Pages right now? A CNAME file// left in a repo years ago tells us nothing about live DNS.async function checkDomain(host) { const out = { host, pointsAtGithub: false } try { const cnames = await dns.resolveCname(host).catch(() => []) out.cname = cnames[0] ?? null if (out.cname && /\.github\.io\.?$/.test(out.cname)) out.pointsAtGithub = true } catch {} try { const a = await dns.resolve4(host).catch(() => []) out.a = a if (a.some((ip) => GH_PAGES_IPS.has(ip))) out.pointsAtGithub = true } catch {} if (!out.cname && !out.a?.length) out.unresolved = true return out}
async function checkLive(url) { if (!url) return null const ac = AbortController ? new AbortController() : null const t = setTimeout(() => ac?.abort(), 10000) try { const res = await fetch(url, { redirect: 'follow', signal: ac?.signal }) return { status: res.status, finalUrl: res.url, ok: res.ok } } catch (e) { return { error: String(e.message || e) } } finally { clearTimeout(t) }}
export async function scanRepo(repo, { network = true } = {}) { const pages = await pagesConfig(repo)
// Inspect the branch Pages serves from when there is one; otherwise the default // branch, so we still catch deploy config for sites hosted off GitHub. const branch = pages?.sourceBranch || repo.default_branch const { paths, truncated, error: treeError } = await treePaths(repo, branch) const d = detect(paths, pages?.sourcePath ?? '/')
// Read at most a handful of workflows — enough to identify the deploy action. let pagesActions = [] if (d.workflows.length) { const texts = await pmap(d.workflows.slice(0, 12), 4, (w) => fileText(repo, branch, w)) pagesActions = detectPagesActions(texts) }
// The custom domain can come from Pages config or a stray CNAME file; prefer // the config, fall back to the file. let customDomain = pages?.cname || null if (!customDomain && d.hasCname) { const cnamePath = (pages?.sourcePath ?? '/').replace(/^\/+|\/+$/g, '') const text = await fileText(repo, branch, cnamePath ? `${cnamePath}/CNAME` : 'CNAME') customDomain = text?.trim().split(/\s+/)[0] || null }
const site = { repo: repo.full_name, name: repo.name, private: repo.private, fork: repo.fork, archived: repo.archived, defaultBranch: repo.default_branch, hasPagesFlag: repo.has_pages, homepage: repo.homepage || null, pushedAt: repo.pushed_at, description: repo.description, pages, branch, sourcePath: pages?.sourcePath ?? '/', buildType: pages?.buildType ?? null, customDomain, pagesActions, detect: { ...d, truncated, treeError }, }
if (network) { site.live = await checkLive(pages?.url || (customDomain ? `https://${customDomain}` : null)) if (customDomain) site.dns = await checkDomain(customDomain) }
site.verdict = verdict(site) return site}
/** * A repo counts as hosting a website if Pages is configured, or if it carries * deploy config for another host, or if it advertises a homepage that is not * just a link to somewhere unrelated. */export function isSite(s) { if (s.pages && !s.pages.error) return true if (s.detect?.otherHosts?.length) return true if (s.pagesActions?.length) return true return false}
export async function scan(owner, opts = {}) { const repos = await listRepos(owner, opts) process.stderr.write(`scanning ${repos.length} repos under ${owner}\n`)
let done = 0 const sites = await pmap(repos, 8, async (r) => { const s = await scanRepo(r, opts) done++ if (done % 20 === 0) process.stderr.write(` ${done}/${repos.length}\n`) return s })
return { owner, scannedAt: new Date().toISOString(), repoCount: repos.length, repos: sites, }}