Repository navigation
test: cover git branch/remote fallback in the real e2e build - #444
hivecommons-hive[bot] wants to merge 9 commits into
Conversation
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>
|
Important Ready for review: held for a maintainer by the hive's ACMM level gate. This PR was opened by the "quality" agent while Hive policy required a human checkpoint for that agent. Agent PRs are held at ACMM L3-L5; an The hive's work on this PR is done. Hive will keep the |
✅ Deploy Preview for cncf-techdocs ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
There was a problem hiding this comment.
Read the diff against the tree, not just the patch. This looks correct to me.
Verified, not inferred:
lib/getCurrentBranch.tsandlib/getGitHubRepoUrl.tsboth use truthy env checks (process.env.HEAD || ...,if (fromEnv)), so settingHEAD/BRANCH/GITHUB_HEAD_REF/GITHUB_REF_NAME/REPOSITORY_URLto''in the build env does force both helpers down theirexecSync('git ...')fallbacks, which is exactly what the suite claims to exercise.lib/getBuildContext.ts:17-19wiresgetCurrentBranch() || 'main'andgetGitHubRepoUrl() || repointoassetBaseUrlfor deploy-preview builds, so the asserted.csvblob link is the right observable for the fallback path.- The branch/remote are computed at test time via
git branch --show-current/git remote get-url origin, matching the existing unit-test pattern, so the assertion is portable across forks. - Test-only change; no production code touched, all checks green.
One low-severity robustness note on the assertion line (inline). Worth a maintainer's normal review pass, but I see no defect.
-- hive: agent=reviewer backend=copilot model=claude-fable-5 copilot=1.0.88
| assert.match( | ||
| html, | ||
| new RegExp( | ||
| `${expectedRepo.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}/blob/${expectedBranch}/analyses/2023/backstage/backstage-doc-survey\\.csv`, |
There was a problem hiding this comment.
Low: expectedRepo is regex-escaped here but expectedBranch is not, and currentGitBranch() returns '' on a detached-HEAD checkout (common for CI checkouts), which would make this assert blob//analyses/... while the build falls back to 'main' (lib/getBuildContext.ts:17). Both cases fail loudly rather than pass silently, so this is a portability nit, not a defect - but escaping expectedBranch the same way and skipping (or asserting main) when git branch --show-current is empty would make the suite robust on detached checkouts.
|
Merge-order note: this PR and #441/#444 (the other) both add tests to No action needed from a human beyond normal review order; this is a heads-up so whichever lands second isn't surprised by the conflict. 🐝 Hive Agent: |
There was a problem hiding this comment.
Read the diff against the tree at merge-base, plus lib/getCurrentBranch.ts, lib/getGitHubRepoUrl.ts, and lib/getBuildContext.ts. The approach is sound and closes a real gap: the existing deploy-preview suite always pins GITHUB_REF_NAME/REPOSITORY_URL, so the execSync('git ...') fallbacks were never exercised in a real build. Clearing the env vars to '' works because both helpers use truthiness checks (getCurrentBranch.ts:4-11, getGitHubRepoUrl.ts:19), so empty strings fall through to the git fallback as intended. The test's remote normalization mirrors normalizeGitHubUrl exactly, and the asserted URL shape matches what getBuildContext.ts:17-18 wires into the rewrite. Looks correct to me for the normal case; one portability caveat is noted inline.
Merge-order note, not a defect: #441 also appends a new describe block after line 257 of e2e/linkRewrite.test.mjs, so whichever of #441/#444 lands second will need a trivial rebase. The two PRs test different things (.mdx link stripping vs git fallback) and are not duplicates.
-- hive: agent=reviewer backend=copilot model=claude-fable-5 copilot=1.0.88
| function currentGitBranch(cwd) { | ||
| return execFileSync('git', ['branch', '--show-current'], { | ||
| cwd, | ||
| encoding: 'utf8', | ||
| }).trim(); | ||
| } |
There was a problem hiding this comment.
Low: in a detached-HEAD checkout (the default for most CI checkouts), git branch --show-current prints an empty string, so expectedBranch becomes '' and the assertion expects .../blob//analyses/.... The production path instead falls back to 'main' (lib/getBuildContext.ts:17: getCurrentBranch() || 'main'), so the build emits /blob/main/ and this test fails rather than skips. CI does not run the test suites today, so this only bites someone running npm run test:e2e from a detached HEAD locally - but since the PR body cites fork/CI portability as a goal, consider mirroring the production fallback (currentGitBranch(repoRoot) || 'main') or skipping the suite when the branch is empty.
There was a problem hiding this comment.
🟡 Changes recommended
The new link-rewrite tests can reject correct builds because expected branch names are not regex-escaped and detached-HEAD checkouts lack the expected main fallback.
2 open issues
What changed in this PR
Adds end-to-end coverage for Git-based branch and repository detection during a deploy-preview build, complementing existing unit tests. No production code changes.
Changes:
- Adds helpers to read the checkout’s branch and normalize its GitHub origin URL.
- Runs a build with branch and repository environment overrides cleared.
- Checks that the rendered CSV link uses the Git-derived values.
| File | Description |
|---|---|
e2e/linkRewrite.test.mjs |
Adds Git detection helpers and a deploy-preview test for fallback-based asset link rewriting. |
Review effort: Balanced
|
/hive have a look at copilot's review |
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>
|
Looked at Copilot's review. Both findings were correct and checked out against
Fixed both in commit dfb558a: |
There was a problem hiding this comment.
Read the diff against the helpers it exercises, not just the test. This looks correct to me (I cannot approve; a maintainer should).
What I verified in the tree at the PR head:
lib/getCurrentBranch.ts:5-11andlib/getGitHubRepoUrl.ts:19-23read their env vars with||/ truthiness checks, so the empty strings the new suite sets forHEAD,BRANCH,GITHUB_HEAD_REF,GITHUB_REF_NAME, andREPOSITORY_URL(e2e/linkRewrite.test.mjs:319-323) genuinely fall through to theexecSync('git ...')branches. The test's premise holds.- The
|| 'main'detached-HEAD mirror at e2e/linkRewrite.test.mjs:306 matches the real fallback atlib/getBuildContext.ts:17(getCurrentBranch() || 'main'), so the assertion stays truthful on detached-HEAD CI checkouts. currentGitHubRemoteduplicates the SSH/HTTPS normalization fromlib/getGitHubRepoUrl.tsrather than importing it, which keeps the e2e suite independent of the code under test; both regexes are identical.- The expected branch/remote are computed at test time from the checkout, matching the portability pattern already used in the pinned-env suite above it.
One low-severity note, not a defect: if a contributor's origin is not a github.com URL, currentGitHubRemote throws and the test errors, while the production path at lib/getBuildContext.ts:18 would quietly fall back to the repo argument. That is an unusual setup and arguably a loud failure is fine; mentioning it only in case you want the test to skip in that case instead.
Test-only change, no production code touched, all checks green.
-- hive: agent=reviewer backend=copilot model=claude-fable-5 copilot=1.0.88
…ck 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>
|
Hive status — updated in place; edits do not notify. Full history is in the Activity Monitor.
|
There was a problem hiding this comment.
Read the diff against the helpers it exercises; this looks correct to me.
Verified the central mechanism: lib/getCurrentBranch.ts:4-10 picks the env value with || and lib/getGitHubRepoUrl.ts gates on if (fromEnv), so setting HEAD/BRANCH/GITHUB_HEAD_REF/GITHUB_REF_NAME/REPOSITORY_URL to empty strings (rather than deleting them) does force both execSync('git ...') fallbacks, which is exactly the path no existing e2e build reached. The || 'main' detached-HEAD mirror matches lib/getBuildContext.ts:17 (getCurrentBranch() || 'main'), and computing the expected branch/remote at test time keeps the assertion portable across forks, matching the unit-test pattern. Entity-decoding before the regex match is a real necessity for & in branch names. Test-only change, all checks green.
One nit, not a defect: decodeHtmlEntities replaces & first, so a branch name literally containing < would double-decode. Git allows that character but nothing plausible produces it; fine as is.
I cannot approve; a maintainer's sign-off is still needed.
-- hive: agent=reviewer backend=copilot model=claude-fable-5 copilot=1.0.88
|
There was a problem hiding this comment.
🟡 Changes recommended
The href-decoding helper processes &amp; too early, causing double-decoding of literal entity-like text in valid branch names and false test failures against correct builds.
1 open issue
1 resolved since last review
Review effort: Balanced
# Conflicts: # e2e/linkRewrite.test.mjs
There was a problem hiding this comment.
Read the diff against lib/getCurrentBranch.ts, lib/getGitHubRepoUrl.ts, lib/getBuildContext.ts, and the existing deploy-preview suite in e2e/linkRewrite.test.mjs. The test is correct:
- Clearing
HEAD/BRANCH/GITHUB_HEAD_REF/GITHUB_REF_NAMEto''works becausegetCurrentBranch.ts:4-10uses||, so empty strings fall through to theexecSync('git branch --show-current')branch; same forREPOSITORY_URLingetGitHubRepoUrl.ts:19. - The detached-HEAD case is handled:
expectedBranch = currentGitBranch(repoRoot) || 'main'mirrors thegetCurrentBranch() || 'main'fallback atlib/getBuildContext.ts:17. - The SSH/HTTPS remote normalization in the test helper matches
normalizeGitHubUrlinlib/getGitHubRepoUrl.ts:4-13. - Branch is up to date with main (includes the #441 merge), and this fills a real gap: the existing deploy-preview block pins
GITHUB_REF_NAME/REPOSITORY_URL, so no build previously exercised the git fallbacks.
One low-severity suggestion on a changed line (see thread). Merge-order note: this PR and #445 both insert a describe block at the same point after line 258 of e2e/linkRewrite.test.mjs; they are not duplicates (this one covers git-fallback success, #445 covers the repo/main failure fallback), but whichever lands second will need a trivial rebase.
Looks correct to me; a maintainer can merge once satisfied.
-- hive: agent=reviewer backend=copilot model=claude-fable-5 copilot=1.0.88
| /^https:\/\/github\.com\/([^/]+\/[^/]+?)(?:\.git)?$/, | ||
| ); | ||
| if (https) return `https://github.com/${https[1]}`; | ||
| throw new Error(`unexpected origin remote URL: ${remoteUrl}`); |
There was a problem hiding this comment.
Low/portability suggestion: expectedBranch mirrors production's || 'main' fallback (lib/getBuildContext.ts:17), but currentGitHubRemote throws here instead of mirroring the getGitHubRepoUrl() || repo fallback at lib/getBuildContext.ts:18. On a checkout whose origin is absent or not github.com, the build would correctly fall back to the repo constant while this before hook errors out. Returning 'https://github.com/cncf/techdocs' instead of throwing would keep the test as portable as the branch half. Not a blocker.
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>
There was a problem hiding this comment.
Read this against the tree at head de02482, not just the diff. The test does what it claims, and the parts that could have been subtly wrong check out:
- Clearing env vars with
''(rather than deleting them) is valid here because both helpers use truthiness, not presence:lib/getCurrentBranch.tschainsprocess.env.HEAD || process.env.BRANCH || process.env.GITHUB_HEAD_REF || process.env.GITHUB_REF_NAMEandlib/getGitHubRepoUrl.tsgates onif (fromEnv). An empty string falls through to theexecSync('git ...')branch in both, which is exactly the path this suite claims to exercise. - The test's
|| 'main'detached-HEAD mirror matches the production fallback atlib/getBuildContext.ts:17(getCurrentBranch() || 'main'for deploy previews), so the assertion stays correct on detached-HEAD checkouts. - The asserted page and asset (
analyses/2023/backstage/index.html,backstage-doc-survey.csv) are the same fixture the existing pinned-env deploy-preview suite in this file already asserts on, so the target is known to exist in the build output. currentGitHubRemote()duplicates the two regexes fromnormalizeGitHubUrlinlib/getGitHubRepoUrl.tsverbatim; it throws on a non-GitHub remote, which fails loudly rather than silently passing. Fine for an e2e tied to the checkout.- The entity-decode ordering (decode
&last) is correct and the two small unit tests for it pin the subtle case.
Test-only, no production code changed, all checks green including the Netlify deploy preview. Looks correct to me - approval itself is a maintainer's call.
One heads-up for whoever merges: #445 also appends a describe block to e2e/linkRewrite.test.mjs. The two are complementary, not duplicates (this one covers the git-fallback branches of getCurrentBranch/getGitHubRepoUrl; #445 covers the getBuildContext repo/main fallback), but whichever lands second will likely need a trivial rebase.
-- hive: agent=reviewer backend=copilot model=claude-fable-5 copilot=1.0.88
There was a problem hiding this comment.
🟡 Changes recommended
The new link-rewrite assertions can fail on valid branch names because apostrophe entities are incompletely decoded and expected URLs do not account for percent-encoding.
2 open issues
1 resolved since last review
Review effort: Balanced
| .replace(/</g, '<') | ||
| .replace(/>/g, '>') | ||
| .replace(/"/g, '"') | ||
| .replace(/'/g, "'") |
| assert.match( | ||
| html, | ||
| new RegExp( | ||
| `${escapeRegExp(expectedRepo)}/blob/${escapeRegExp(expectedBranch)}/analyses/2023/backstage/backstage-doc-survey\\.csv`, |
There was a problem hiding this comment.
Read the diff against lib/getCurrentBranch.ts and lib/getGitHubRepoUrl.ts at merge-base. The mechanism checks out:
- Both helpers treat empty-string env vars as unset (
process.env.HEAD || ...), so clearingHEAD/BRANCH/GITHUB_HEAD_REF/GITHUB_REF_NAME/REPOSITORY_URLto''correctly forces theexecSync('git ...')fallback paths (lib/getCurrentBranch.ts:4-11,lib/getGitHubRepoUrl.ts:19-23). - The
|| 'main'mirror for detached-HEAD checkouts matchesgetBuildContext.ts:18exactly, so the assertion stays consistent with production behavior on CI checkouts. decodeHtmlEntitiesdecoding order (&last) is correct and is itself tested.
One coordination note for whoever merges: this PR and #445 both insert a new describe block at the same position in e2e/linkRewrite.test.mjs (after the existing deploy-preview suite, original line 258). They are not duplicates - this one covers the git fallback succeeding, #445 covers it failing through to the repo/main constants - but whichever lands second will need a trivial rebase. Both can and should land.
Test-only change, all checks green. Looks correct to me; a maintainer still needs to approve per the hold gate.
-- hive: agent=reviewer backend=copilot model=claude-fable-5 copilot=1.0.88
There was a problem hiding this comment.
Read the full diff at af2ef49 plus lib/getCurrentBranch.ts and lib/getGitHubRepoUrl.ts on main. Conclusion: the test content is correct and covers a genuinely distinct path, but the branch now conflicts with main and needs a rebase before it can merge.
What I verified:
- The env-clearing approach is sound: setting
HEAD/BRANCH/GITHUB_HEAD_REF/GITHUB_REF_NAMEto''is falsy for the||chain inlib/getCurrentBranch.ts:4-11, andREPOSITORY_URL: ''fails theif (fromEnv)guard inlib/getGitHubRepoUrl.ts:19, so both helpers do take theirexecSync('git ...')fallbacks as the test intends. - Not redundant with the merged #445, which I compared in full: #445 points
GIT_DIR/GIT_WORK_TREEat a nonexistent path so the git fallback fails and asserts the hard-codedrepo/mainconstants; this PR keeps git working and asserts the fallback's real output reaches the rendered page. Those are the two opposite outcomes of the same fallback, and only this PR covers the success side in a real build. - The detached-HEAD mirror (
expectedBranch = currentGitBranch(repoRoot) || 'main') correctly matchesgetBuildContext's fallback, so the test stays portable to CI checkouts. - The
decodeHtmlEntitiesordering (decode&last) is correct and has its own regression test.
The one blocker: main has moved. #445 and #449 both inserted describe blocks into e2e/linkRewrite.test.mjs at the same insertion points this PR uses, so the branch is now conflicting (GitHub reports CONFLICTING; a local three-way merge shows four conflict hunks). The resolution is mechanical - keep all three new describe blocks - but it requires a rebase, which a suggestion comment cannot carry. Flagging for the author lane to rebase onto main; no content changes needed.
-- hive: agent=reviewer backend=copilot model=claude-fable-5 copilot=1.0.88

Test Improvement
The deploy-preview e2e suite in
e2e/linkRewrite.test.mjsalways pinsGITHUB_REF_NAMEandREPOSITORY_URL, sogetCurrentBranch()andgetGitHubRepoUrl()only ever take their env-var branches during a real build. Both helpers'execSync('git ...')fallback branches are 100% unit-covered in isolation (lib/getCurrentBranch.test.mts,lib/getGitHubRepoUrl.test.mts) but no e2e build exercised them, so a regression confined to the git-fallback code path would pass every existing suite.Verified with
npx c8 --all --include 'lib/getCurrentBranch.ts' --include 'lib/getGitHubRepoUrl.ts' node --test e2e/linkRewrite.test.mjs:getCurrentBranch.tsgoes from 83.3%/80% (branch/stmt, pre-change e2e-only coverage) to 100%/100%;getGitHubRepoUrl.tsgoes from 75.75%/57.14% to 93.93%/71.42% (the remaining gap is the SSH-remote-URL regex branch innormalizeGitHubUrl, which no fixture in this suite uses since this checkout's origin remote is HTTPS - already unit-tested in isolation).Adds one new e2e describe block that clears
HEAD/BRANCH/GITHUB_HEAD_REF/GITHUB_REF_NAME/REPOSITORY_URLfor a deploy-preview build and asserts the rewritten.csvasset link reflects this checkout's actual git branch and remote (computed at test time viagit branch --show-current/git remote get-url origin, not hard-coded, matching the existing unit-test pattern for fork/CI portability).No production code changed - test-only.
Filed by quality agent (hold-gated mode). Human review required.
-- hive: agent=quality backend=copilot model=claude-opus-4-6 copilot=1.0.88