import { DOCS_GUIDES_URL_PREFIX } from './resolve-docs-scope.ts' // The sitemap's formatter can wrap a long tag as `url` const LOC_RE = /\s*([^<]+?)\s*<\/loc\s*>/g const SITEMAP_TIMEOUT_MS = 30_000 // More pages than this missing from production points to a broken sitemap, not a deploy lag const MAX_NOT_ON_PRODUCTION_SHARE = 0.1 export function sitemapGuidePaths(xml: string): Set { const paths = new Set() for (const match of xml.matchAll(LOC_RE)) { let pathname: string try { pathname = new URL(match[1]).pathname } catch { continue } if (pathname.startsWith(DOCS_GUIDES_URL_PREFIX)) paths.add(pathname.replace(/\/+$/, '')) } return paths } export type ProductionSplit = { live: string[]; notOnProduction: string[] } export function splitByProduction(pages: string[], production: Set): ProductionSplit { const live = pages.filter((page) => production.has(page)) const notOnProduction = pages.filter((page) => !production.has(page)) if (notOnProduction.length > pages.length * MAX_NOT_ON_PRODUCTION_SHARE) { throw new Error( `${notOnProduction.length} of ${pages.length} guides are missing from the ` + `sitemap, more than a deploy lag explains. Check that the sitemap is complete.` ) } return { live, notOnProduction } } // Keeps the pages production serves, so a guide merged since the last deploy isn't scanned yet export async function onlyProductionPages( pages: string[], sitemapUrl: string ): Promise { const response = await fetch(sitemapUrl, { signal: AbortSignal.timeout(SITEMAP_TIMEOUT_MS), }).catch((error: unknown) => { throw new Error( `Couldn't read ${sitemapUrl}: ${error instanceof Error ? error.message : error}` ) }) if (!response.ok) throw new Error(`${sitemapUrl} returned ${response.status}.`) const production = sitemapGuidePaths(await response.text()) if (production.size === 0) throw new Error(`${sitemapUrl} lists no docs guides.`) const split = splitByProduction(pages, production) // A GitHub Actions warning shows on the run page even when the scan passes const prefix = process.env.GITHUB_ACTIONS ? '::warning::' : '' for (const page of split.notOnProduction) { console.log(`${prefix}Skipping ${page}: not in ${sitemapUrl}.`) } return split }