Repository navigation
fix: harden documentation publishing #66
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
c16afc9
fix: repair API reference links and metadata
ohbus 3ab7600
fix: remove bodies from status-only API responses
ohbus a135e63
fix: contain documentation tables on mobile
ohbus 4ac2407
ci: make documentation check always present
ohbus df16ac4
test: guard documentation mobile layout
ohbus a479145
fix: retry Chrome profile cleanup
ohbus 0f07d33
fix: wait for Chrome mobile audit
ohbus b6f7607
fix: make mobile documentation checks deterministic
ohbus 0f1015e
fix: detect only pull request changes
ohbus cd753df
test: require documented OpenAPI responses
ohbus 83e1764
fix: publish static API reference routes
ohbus c487bda
fix: trigger docs builds for Maven inputs
ohbus 1f4f302
fix: bound mobile page-load waits
ohbus 1db6c66
fix: discover Chrome in documentation CI
ohbus 9279628
fix: detect renamed documentation paths
ohbus b41a313
fix: handle mobile page reload failures
ohbus 398054f
fix: allow Chrome more startup time
ohbus 51ac496
fix: fail on documentation diff errors
ohbus b4d7748
fix: reject pending DevTools calls
ohbus d6afd09
fix: detect non-ASCII documentation paths
ohbus File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| @media screen and (max-width: 44.984375rem) { | ||
| .md-typeset__scrollwrap, | ||
| .md-typeset details .md-typeset__scrollwrap { | ||
| margin-inline: 0; | ||
| } | ||
|
|
||
| .md-typeset__table { | ||
| width: 100%; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,160 @@ | ||
| import { execFileSync, spawn } from "node:child_process"; | ||
| import { mkdtemp, readFile, rm } from "node:fs/promises"; | ||
| import { tmpdir } from "node:os"; | ||
| import { join } from "node:path"; | ||
|
|
||
| const chrome = process.env.CHROME_PATH ?? findChrome(); | ||
|
|
||
| function findChrome() { | ||
| if (process.platform === "darwin") { | ||
| return "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"; | ||
| } | ||
| for (const command of ["google-chrome", "google-chrome-stable", "chromium", "chromium-browser"]) { | ||
| try { | ||
| return execFileSync("which", [command], { encoding: "utf8" }).trim(); | ||
| } catch {} | ||
| } | ||
| throw new Error("No supported Chrome or Chromium executable was found"); | ||
| } | ||
| const profile = await mkdtemp(join(tmpdir(), "moo-docs-chrome-")); | ||
| const server = spawn("python3", ["-m", "http.server", "4173", "-d", "../../site"], { | ||
| cwd: import.meta.dirname, | ||
| stdio: "ignore", | ||
| }); | ||
| for (let attempt = 0; attempt < 50; attempt++) { | ||
| try { | ||
| const response = await fetch("http://127.0.0.1:4173/"); | ||
| if (response.ok) break; | ||
| } catch {} | ||
| await new Promise((resolve) => setTimeout(resolve, 100)); | ||
| } | ||
| const browser = spawn( | ||
| chrome, | ||
| [ | ||
| "--headless=new", | ||
| "--no-sandbox", | ||
| "--disable-dev-shm-usage", | ||
| "--remote-debugging-port=0", | ||
| `--user-data-dir=${profile}`, | ||
| "http://127.0.0.1:4173/", | ||
| ], | ||
| { stdio: ["ignore", "ignore", "pipe"] }, | ||
| ); | ||
| let browserError = ""; | ||
| browser.stderr.on("data", (data) => { | ||
| browserError += data.toString(); | ||
| }); | ||
| browser.on("error", (error) => { | ||
| browserError += `${error.message}\n`; | ||
| }); | ||
| const browserExit = new Promise((resolve) => browser.once("close", resolve)); | ||
| const serverExit = new Promise((resolve) => server.once("close", resolve)); | ||
|
|
||
| const waitForTabs = async () => { | ||
| for (let attempt = 0; attempt < 300; attempt++) { | ||
| if (browser.exitCode !== null || browser.signalCode !== null) { | ||
| throw new Error(`Chrome did not start${browserError ? `: ${browserError.trim()}` : ""}`); | ||
| } | ||
| try { | ||
| const [port] = (await readFile(join(profile, "DevToolsActivePort"), "utf8")).split("\n"); | ||
| const response = await fetch(`http://127.0.0.1:${port}/json`); | ||
| if (response.ok) return response.json(); | ||
| } catch {} | ||
| await new Promise((resolve) => setTimeout(resolve, 100)); | ||
| } | ||
| throw new Error(`Chrome did not start${browserError ? `: ${browserError.trim()}` : ""}`); | ||
| }; | ||
|
|
||
| try { | ||
| const tabs = await waitForTabs(); | ||
| const tab = tabs.find(({ type }) => type === "page"); | ||
| const socket = new WebSocket(tab.webSocketDebuggerUrl); | ||
| const pending = new Map(); | ||
| let requestId = 0; | ||
| let loadReject; | ||
| socket.onmessage = ({ data }) => { | ||
| const message = JSON.parse(data); | ||
| if (message.method === "Page.loadEventFired") { | ||
| pending.get("load")?.(); | ||
| pending.delete("load"); | ||
| } | ||
| pending.get(message.id)?.resolve(message); | ||
| pending.delete(message.id); | ||
| }; | ||
| socket.addEventListener("close", () => { | ||
| const error = new Error("Chrome DevTools connection closed"); | ||
| loadReject?.(error); | ||
| for (const pendingCall of pending.values()) pendingCall.reject?.(error); | ||
| pending.clear(); | ||
| }); | ||
| await new Promise((resolve) => (socket.onopen = resolve)); | ||
| const call = (method, params = {}) => | ||
| new Promise((resolve, reject) => { | ||
| const id = ++requestId; | ||
| pending.set(id, { resolve, reject }); | ||
| socket.send(JSON.stringify({ id, method, params })); | ||
| }); | ||
| await call("Page.enable"); | ||
|
|
||
| for (const width of [320, 375]) { | ||
| await call("Emulation.setDeviceMetricsOverride", { | ||
| width, | ||
| height: 812, | ||
| deviceScaleFactor: 1, | ||
| mobile: true, | ||
| }); | ||
| const loaded = new Promise((resolve, reject) => { | ||
| const timeout = setTimeout(() => { | ||
| loadReject = undefined; | ||
| pending.delete("load"); | ||
| reject(new Error(`Page did not load at ${width}px`)); | ||
| }, 10_000); | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
| loadReject = (error) => { | ||
| clearTimeout(timeout); | ||
| loadReject = undefined; | ||
| pending.delete("load"); | ||
| reject(error); | ||
| }; | ||
| pending.set("load", () => { | ||
| clearTimeout(timeout); | ||
| loadReject = undefined; | ||
| resolve(); | ||
| }); | ||
| }); | ||
| await Promise.race([call("Page.reload"), loaded]); | ||
| await loaded; | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
| let layout; | ||
| for (let attempt = 0; attempt < 50 && !layout; attempt++) { | ||
| const result = await call("Runtime.evaluate", { | ||
| expression: `(() => { | ||
| const root = document.documentElement; | ||
| const article = document.querySelector("article"); | ||
| if (!article) return null; | ||
| const bounds = article.getBoundingClientRect(); | ||
| const clipped = [...article.querySelectorAll("*")] | ||
| .filter((element) => { | ||
| const rect = element.getBoundingClientRect(); | ||
| return !element.closest("pre") && rect.width > 0 && (rect.left < bounds.left - 1 || rect.right > bounds.right + 1); | ||
| }) | ||
| .map((element) => element.tagName + "." + element.className) | ||
| .slice(0, 5); | ||
| return { rootFits: root.scrollWidth <= root.clientWidth, clipped }; | ||
| })()`, | ||
| returnByValue: true, | ||
| }); | ||
| layout = result.result.result.value; | ||
| if (!layout) await new Promise((resolve) => setTimeout(resolve, 100)); | ||
| } | ||
| if (!layout) throw new Error(`${width}px layout did not load`); | ||
| if (!layout.rootFits || layout.clipped.length) { | ||
| throw new Error(`${width}px layout overflows: ${layout.clipped.join(", ")}`); | ||
| } | ||
| } | ||
| socket.close(); | ||
| console.log("Mobile layout fits at 320px and 375px"); | ||
| } finally { | ||
| browser.kill(); | ||
| server.kill(); | ||
| await Promise.all([browserExit, serverExit]); | ||
| await rm(profile, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 }); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| import { copyFile, mkdir, readFile, writeFile } from "node:fs/promises"; | ||
|
|
||
| const output = new URL("../../site/api/reference/index.html", import.meta.url); | ||
| const basePath = "/twitter-backend-java/api/reference"; | ||
| const canonicalUrl = `https://subho.xyz${basePath}/`; | ||
| const description = "Interactive OpenAPI reference for the Moo Twitter Backend."; | ||
| const routes = ["authentication", "hashtags", "models", "posts", "search", "users"]; | ||
| let html = await readFile(output, "utf8"); | ||
| const scriptStart = html.indexOf('<script type="module">'); | ||
|
|
||
| if (scriptStart < 0) throw new Error("Redoc module script was not found"); | ||
|
|
||
| const document = html.slice(0, scriptStart).replaceAll('href="/', `href="${basePath}/`); | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
| html = `${document}${html.slice(scriptStart)}`.replace( | ||
| "</title>", | ||
| `</title>\n <meta name="description" content="${description}">\n <meta property="og:title" content="Moo Twitter Backend API">\n <meta property="og:description" content="${description}">\n <link rel="canonical" href="${canonicalUrl}">`, | ||
| ); | ||
|
|
||
| if (html.includes('href="/authentication"') || !html.includes(`href="${basePath}/authentication"`)) { | ||
| throw new Error("Redoc routes were not rebased"); | ||
| } | ||
| if (!html.includes(`rel="canonical" href="${canonicalUrl}"`)) { | ||
| throw new Error("API reference metadata was not added"); | ||
| } | ||
|
|
||
| await writeFile(output, html); | ||
|
|
||
| for (const route of routes) { | ||
| const directory = new URL(`./${route}/`, output); | ||
| await mkdir(directory, { recursive: true }); | ||
| await copyFile(output, new URL("index.html", directory)); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.