From 057de2297a3d992b3649afad51cb02e1dc6972bd Mon Sep 17 00:00:00 2001 From: quality <280983584+hivecommons-hive[bot]@users.noreply.github.com> Date: Sun, 22 Nov 2026 12:13:42 -0500 Subject: [PATCH 1/6] test: cover git branch/remote fallback in the real e2e build The deploy-preview e2e suite always pins GITHUB_REF_NAME and REPOSITORY_URL, so getCurrentBranch() and getGitHubRepoUrl() only ever take their env-var branches during a real build. Both helpers' execSync('git ...') fallbacks are 100% unit-covered in isolation but no build asserted that fallback output actually wires into a rendered page. This adds an e2e suite that clears every env var the two helpers check and asserts the rewritten .csv link reflects this checkout's real git branch and remote. Signed-off-by: quality <280983584+hivecommons-hive[bot]@users.noreply.github.com> --- e2e/linkRewrite.test.mjs | 78 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) diff --git a/e2e/linkRewrite.test.mjs b/e2e/linkRewrite.test.mjs index c7bff49c..bad185fb 100644 --- a/e2e/linkRewrite.test.mjs +++ b/e2e/linkRewrite.test.mjs @@ -7,6 +7,27 @@ import { tmpdir } from 'node:os'; import path from 'node:path'; import url from 'node:url'; +function currentGitBranch(cwd) { + return execFileSync('git', ['branch', '--show-current'], { + cwd, + encoding: 'utf8', + }).trim(); +} + +function currentGitHubRemote(cwd) { + const remoteUrl = execFileSync('git', ['remote', 'get-url', 'origin'], { + cwd, + encoding: 'utf8', + }).trim(); + const ssh = remoteUrl.match(/^git@github\.com:([^/]+\/[^/]+?)(?:\.git)?$/); + if (ssh) return `https://github.com/${ssh[1]}`; + const https = remoteUrl.match( + /^https:\/\/github\.com\/([^/]+\/[^/]+?)(?:\.git)?$/, + ); + if (https) return `https://github.com/${https[1]}`; + throw new Error(`unexpected origin remote URL: ${remoteUrl}`); +} + const __dirname = path.dirname(url.fileURLToPath(import.meta.url)); const repoRoot = path.join(__dirname, '..'); @@ -257,3 +278,60 @@ describe('deploy-preview build wires branch/repo detection into the rewritten .c assert.doesNotMatch(html, /\/blob\/main\//); }); }); + +// The deploy-preview suite above always pins GITHUB_REF_NAME and +// REPOSITORY_URL, so getCurrentBranch() and getGitHubRepoUrl() only ever +// take their env-var branches during a real build. Both helpers' +// `execSync('git ...')` fallbacks (reached only when every relevant env var +// is unset) are 100% unit-covered in isolation +// (lib/getCurrentBranch.test.mts, lib/getGitHubRepoUrl.test.mts), but no +// build in this suite ever wires that fallback output into a rendered page. +// This suite clears every env var the two helpers check and asserts the +// rewritten .csv link reflects this checkout's real branch and remote, so a +// regression confined to the git-fallback branches fails here even when +// the env-var unit tests still pass in isolation. +describe('deploy-preview build falls back to real git branch/remote detection when no env vars are set', () => { + let outDir; + let expectedBranch; + let expectedRepo; + + before(async () => { + expectedBranch = currentGitBranch(repoRoot); + expectedRepo = currentGitHubRemote(repoRoot); + outDir = await mkdtemp(path.join(tmpdir(), 'techdocs-e2e-git-fallback-')); + execFileSync('npx', ['docusaurus', 'build', '--out-dir', outDir], { + cwd: repoRoot, + env: { + ...process.env, + BUILD_ENV: 'deploy-preview', + DEPLOY_PRIME_URL: 'https://deploy-preview-1--cncf-techdocs.netlify.app', + // Clear every env var getCurrentBranch()/getGitHubRepoUrl() check so + // both fall through to their `execSync('git ...')` branches instead + // of an env-provided value. + HEAD: '', + BRANCH: '', + GITHUB_HEAD_REF: '', + GITHUB_REF_NAME: '', + REPOSITORY_URL: '', + }, + stdio: 'inherit', + }); + }); + + after(async () => { + if (outDir) await rm(outDir, { recursive: true, force: true }); + }); + + it("rewrites the .csv asset link to this checkout's real git branch and remote", async () => { + const html = await readFile( + path.join(outDir, 'analyses/2023/backstage/index.html'), + 'utf-8', + ); + assert.match( + html, + new RegExp( + `${expectedRepo.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}/blob/${expectedBranch}/analyses/2023/backstage/backstage-doc-survey\\.csv`, + ), + ); + }); +}); From dfb558a6bdf13b19aeacdbc774d49555c2bf4399 Mon Sep 17 00:00:00 2001 From: quality <280983584+hivecommons-hive[bot]@users.noreply.github.com> Date: Mon, 23 Nov 2026 11:53:50 -0500 Subject: [PATCH 2/6] test: handle detached-HEAD fallback and escape branch name in regex Copilot's review correctly flagged two portability bugs in the new deploy-preview git-fallback e2e test: - On a detached-HEAD checkout, `git branch --show-current` prints an empty string, but getBuildContext.ts falls back to 'main' in that case. The test asserted the raw (empty) branch, so it would fail against a correct build. - expectedBranch was interpolated into the assertion regex without escaping, while expectedRepo was. Valid git branch names can contain regex metacharacters (e.g. '+'), which would make the assertion fail against a correctly rendered URL. Add an expectedBranch || 'main' fallback mirroring getBuildContext.ts, and reuse the same escaping for both expectedRepo and expectedBranch via a shared escapeRegExp helper. Signed-off-by: quality <280983584+hivecommons-hive[bot]@users.noreply.github.com> --- e2e/linkRewrite.test.mjs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/e2e/linkRewrite.test.mjs b/e2e/linkRewrite.test.mjs index bad185fb..2b3b10a4 100644 --- a/e2e/linkRewrite.test.mjs +++ b/e2e/linkRewrite.test.mjs @@ -14,6 +14,10 @@ function currentGitBranch(cwd) { }).trim(); } +function escapeRegExp(value) { + return value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); +} + function currentGitHubRemote(cwd) { const remoteUrl = execFileSync('git', ['remote', 'get-url', 'origin'], { cwd, @@ -296,7 +300,11 @@ describe('deploy-preview build falls back to real git branch/remote detection wh let expectedRepo; before(async () => { - expectedBranch = currentGitBranch(repoRoot); + // getBuildContext.ts falls back to 'main' when getCurrentBranch() + // returns undefined, which is what happens on a detached-HEAD checkout + // (git branch --show-current prints ''). Mirror that fallback here so + // this test also passes on detached-HEAD CI checkouts. + expectedBranch = currentGitBranch(repoRoot) || 'main'; expectedRepo = currentGitHubRemote(repoRoot); outDir = await mkdtemp(path.join(tmpdir(), 'techdocs-e2e-git-fallback-')); execFileSync('npx', ['docusaurus', 'build', '--out-dir', outDir], { @@ -330,7 +338,7 @@ describe('deploy-preview build falls back to real git branch/remote detection wh assert.match( html, new RegExp( - `${expectedRepo.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}/blob/${expectedBranch}/analyses/2023/backstage/backstage-doc-survey\\.csv`, + `${escapeRegExp(expectedRepo)}/blob/${escapeRegExp(expectedBranch)}/analyses/2023/backstage/backstage-doc-survey\\.csv`, ), ); }); From 1518686dec93f105e6bdb914fb12d8f4e9f370f1 Mon Sep 17 00:00:00 2001 From: quality <280983584+hivecommons-hive[bot]@users.noreply.github.com> Date: Mon, 23 Nov 2026 15:52:11 -0500 Subject: [PATCH 3/6] test: decode HTML entities before comparing branch URLs in e2e fallback test Branch names containing characters HTML entity-encodes (e.g. &) were rendered in href attributes as their encoded form (&), so the raw branch name comparison could fail a correct build. Decode the handful of attribute entities Docusaurus's serializer can produce before matching. Addresses review feedback on PR #444. Signed-off-by: quality <280983584+hivecommons-hive[bot]@users.noreply.github.com> --- e2e/linkRewrite.test.mjs | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/e2e/linkRewrite.test.mjs b/e2e/linkRewrite.test.mjs index 2b3b10a4..f6782159 100644 --- a/e2e/linkRewrite.test.mjs +++ b/e2e/linkRewrite.test.mjs @@ -18,6 +18,20 @@ function escapeRegExp(value) { return value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); } +// Docusaurus serializes href attributes as HTML, so characters valid in a +// branch name (e.g. `&`) are entity-encoded in the rendered output (`&`). +// Decode the handful of entities HTML attribute serialization can produce +// before comparing against the raw branch name, otherwise a correct build +// fails the assertion for any branch containing one of these characters. +function decodeHtmlEntities(value) { + return value + .replace(/&/g, '&') + .replace(/</g, '<') + .replace(/>/g, '>') + .replace(/"/g, '"') + .replace(/'/g, "'"); +} + function currentGitHubRemote(cwd) { const remoteUrl = execFileSync('git', ['remote', 'get-url', 'origin'], { cwd, @@ -331,9 +345,11 @@ describe('deploy-preview build falls back to real git branch/remote detection wh }); it("rewrites the .csv asset link to this checkout's real git branch and remote", async () => { - const html = await readFile( - path.join(outDir, 'analyses/2023/backstage/index.html'), - 'utf-8', + const html = decodeHtmlEntities( + await readFile( + path.join(outDir, 'analyses/2023/backstage/index.html'), + 'utf-8', + ), ); assert.match( html, From de0248249918bac121ae7706be1c906edaa8ead9 Mon Sep 17 00:00:00 2001 From: quality <280983584+hivecommons-hive[bot]@users.noreply.github.com> Date: Tue, 24 Nov 2026 00:49:22 -0500 Subject: [PATCH 4/6] test: decode & last to avoid double-decoding literal entity text Addresses review feedback on this PR: decodeHtmlEntities previously replaced & before the other named entities, so a branch literally containing the text "<" (serialized as "&lt;") would be decoded to "<" and then incorrectly decoded a second time by the < replacement into "<". Decoding & last avoids the double-decode, and new tests assert both the literal-text case and a genuine & still decode correctly. Signed-off-by: quality <280983584+hivecommons-hive[bot]@users.noreply.github.com> --- e2e/linkRewrite.test.mjs | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/e2e/linkRewrite.test.mjs b/e2e/linkRewrite.test.mjs index 30fff610..6a336c64 100644 --- a/e2e/linkRewrite.test.mjs +++ b/e2e/linkRewrite.test.mjs @@ -24,12 +24,15 @@ function escapeRegExp(value) { // before comparing against the raw branch name, otherwise a correct build // fails the assertion for any branch containing one of these characters. function decodeHtmlEntities(value) { + // Decode & last: decoding it first would turn a literal "<" (encoded + // as "&lt;") into "<", which the < replacement below would then + // incorrectly decode a second time into "<". return value - .replace(/&/g, '&') .replace(/</g, '<') .replace(/>/g, '>') .replace(/"/g, '"') - .replace(/'/g, "'"); + .replace(/'/g, "'") + .replace(/&/g, '&'); } function currentGitHubRemote(cwd) { @@ -49,6 +52,19 @@ function currentGitHubRemote(cwd) { const __dirname = path.dirname(url.fileURLToPath(import.meta.url)); const repoRoot = path.join(__dirname, '..'); +describe('decodeHtmlEntities', () => { + it('decodes & last so a literal "<" sequence is not double-decoded', () => { + // A branch literally named "<foo" is serialized by HTML attribute + // encoding as "&lt;foo". Decoding & before < would turn that + // into "<foo" and then incorrectly decode it again into " { + assert.equal(decodeHtmlEntities('foo & bar'), 'foo & bar'); + }); +}); + // Unit tests (lib/docusaurusConfigPreprocessor.test.mts, // lib/remarkRewriteLinks/index.test.mts) cover the pure rewrite functions in // isolation. Neither one proves the rewrites actually survive being wired From 20a3ce15156a67307bbf9ae0b09d1add9ab28fa5 Mon Sep 17 00:00:00 2001 From: quality <280983584+hivecommons-hive[bot]@users.noreply.github.com> Date: Wed, 25 Nov 2026 01:36:55 -0500 Subject: [PATCH 5/6] test: harden e2e git-fallback assertions per review feedback - decodeHtmlEntities now also decodes the hexadecimal apostrophe entity ('), which React's server renderer emits, in addition to the decimal form (') - currentGitHubRemote falls back to the repo constant instead of throwing when origin is absent or not a github.com URL, mirroring getBuildContext.ts's getGitHubRepoUrl() || repo fallback - the expected branch name is now percent-encoded the same way the Markdown-to-HTML pipeline encodes unsafe URI characters (e.g. <) before being regex-escaped, so a branch containing one does not fail a correct build Signed-off-by: quality <280983584+hivecommons-hive[bot]@users.noreply.github.com> --- e2e/linkRewrite.test.mjs | 64 +++++++++++++++++++++++++++++++++++----- 1 file changed, 57 insertions(+), 7 deletions(-) diff --git a/e2e/linkRewrite.test.mjs b/e2e/linkRewrite.test.mjs index 6a336c64..4cd25453 100644 --- a/e2e/linkRewrite.test.mjs +++ b/e2e/linkRewrite.test.mjs @@ -18,6 +18,15 @@ function escapeRegExp(value) { return value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); } +// The Markdown-to-HTML pipeline percent-encodes characters that are unsafe +// in a URI (e.g. `<` becomes `%3C`), the same normalization `encodeURI` +// applies, while leaving `/` and already-escaped `%XX` sequences alone. A +// branch name containing such a character would otherwise produce an +// expected regex that never matches the real rendered href. +function encodeHrefSegment(value) { + return encodeURI(value); +} + // Docusaurus serializes href attributes as HTML, so characters valid in a // branch name (e.g. `&`) are entity-encoded in the rendered output (`&`). // Decode the handful of entities HTML attribute serialization can produce @@ -32,21 +41,31 @@ function decodeHtmlEntities(value) { .replace(/>/g, '>') .replace(/"/g, '"') .replace(/'/g, "'") + .replace(/'/gi, "'") .replace(/&/g, '&'); } function currentGitHubRemote(cwd) { - const remoteUrl = execFileSync('git', ['remote', 'get-url', 'origin'], { - cwd, - encoding: 'utf8', - }).trim(); + let remoteUrl; + try { + remoteUrl = execFileSync('git', ['remote', 'get-url', 'origin'], { + cwd, + encoding: 'utf8', + }).trim(); + } catch { + // No `origin` remote (e.g. a shallow clone without one configured): + // mirror getBuildContext.ts's `getGitHubRepoUrl() || repo` fallback + // instead of failing the suite outright. + return null; + } const ssh = remoteUrl.match(/^git@github\.com:([^/]+\/[^/]+?)(?:\.git)?$/); if (ssh) return `https://github.com/${ssh[1]}`; const https = remoteUrl.match( /^https:\/\/github\.com\/([^/]+\/[^/]+?)(?:\.git)?$/, ); if (https) return `https://github.com/${https[1]}`; - throw new Error(`unexpected origin remote URL: ${remoteUrl}`); + // Origin exists but isn't a recognized github.com URL: same fallback. + return null; } const __dirname = path.dirname(url.fileURLToPath(import.meta.url)); @@ -63,6 +82,33 @@ describe('decodeHtmlEntities', () => { it('still decodes a genuine & to a literal ampersand', () => { assert.equal(decodeHtmlEntities('foo & bar'), 'foo & bar'); }); + + it('decodes both the decimal and hexadecimal apostrophe entities', () => { + // React's server renderer uses the hexadecimal form ('); some other + // encoders use the decimal form ('). A branch like "feature/o'brien" + // must round-trip through either. + assert.equal(decodeHtmlEntities("feature/o'brien"), "feature/o'brien"); + assert.equal( + decodeHtmlEntities("feature/o'brien"), + "feature/o'brien", + ); + assert.equal( + decodeHtmlEntities('feature/o'brien'), + "feature/o'brien", + ); + }); +}); + +describe('encodeHrefSegment', () => { + it('percent-encodes characters the Markdown-to-HTML pipeline treats as unsafe in a URI', () => { + // `<` is not in encodeURI's unescaped set, matching the percent-encoding + // micromark's URI normalization applies to link destinations. + assert.equal(encodeHrefSegment('feature/a { + assert.equal(encodeHrefSegment('feature/foo-bar_1'), 'feature/foo-bar_1'); + }); }); // Unit tests (lib/docusaurusConfigPreprocessor.test.mts, @@ -335,7 +381,11 @@ describe('deploy-preview build falls back to real git branch/remote detection wh // (git branch --show-current prints ''). Mirror that fallback here so // this test also passes on detached-HEAD CI checkouts. expectedBranch = currentGitBranch(repoRoot) || 'main'; - expectedRepo = currentGitHubRemote(repoRoot); + // getBuildContext.ts falls back to the `repo` constant when + // getGitHubRepoUrl() returns undefined (no origin, or origin isn't a + // github.com URL). Mirror that fallback here too. + expectedRepo = + currentGitHubRemote(repoRoot) || 'https://github.com/cncf/techdocs'; outDir = await mkdtemp(path.join(tmpdir(), 'techdocs-e2e-git-fallback-')); execFileSync('npx', ['docusaurus', 'build', '--out-dir', outDir], { cwd: repoRoot, @@ -370,7 +420,7 @@ describe('deploy-preview build falls back to real git branch/remote detection wh assert.match( html, new RegExp( - `${escapeRegExp(expectedRepo)}/blob/${escapeRegExp(expectedBranch)}/analyses/2023/backstage/backstage-doc-survey\\.csv`, + `${escapeRegExp(expectedRepo)}/blob/${escapeRegExp(encodeHrefSegment(expectedBranch))}/analyses/2023/backstage/backstage-doc-survey\\.csv`, ), ); }); From d943cf5fd044e8d0c4f8b86432707262ad49e103 Mon Sep 17 00:00:00 2001 From: quality <280983584+hivecommons-hive[bot]@users.noreply.github.com> Date: Wed, 25 Nov 2026 07:58:46 -0500 Subject: [PATCH 6/6] test: fix prettier formatting in linkRewrite.test.mjs Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Signed-off-by: quality <280983584+hivecommons-hive[bot]@users.noreply.github.com> --- e2e/linkRewrite.test.mjs | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/e2e/linkRewrite.test.mjs b/e2e/linkRewrite.test.mjs index 252ab6c0..262f3f3f 100644 --- a/e2e/linkRewrite.test.mjs +++ b/e2e/linkRewrite.test.mjs @@ -87,15 +87,9 @@ describe('decodeHtmlEntities', () => { // React's server renderer uses the hexadecimal form ('); some other // encoders use the decimal form ('). A branch like "feature/o'brien" // must round-trip through either. - assert.equal(decodeHtmlEntities("feature/o'brien"), "feature/o'brien"); - assert.equal( - decodeHtmlEntities("feature/o'brien"), - "feature/o'brien", - ); - assert.equal( - decodeHtmlEntities('feature/o'brien'), - "feature/o'brien", - ); + assert.equal(decodeHtmlEntities('feature/o'brien'), "feature/o'brien"); + assert.equal(decodeHtmlEntities('feature/o'brien'), "feature/o'brien"); + assert.equal(decodeHtmlEntities('feature/o'brien'), "feature/o'brien"); }); });