Skip to content

fix(bin): harden README generator link handling#7305

Open
akgitrepos wants to merge 1 commit intogoogleapis:mainfrom
akgitrepos:fix/readme-generator-hardening
Open

fix(bin): harden README generator link handling#7305
akgitrepos wants to merge 1 commit intogoogleapis:mainfrom
akgitrepos:fix/readme-generator-hardening

Conversation

@akgitrepos
Copy link

Summary

  • hardens support-documentation fallback logic in bin/generate-readme.mjs so 404 responses correctly fall back to product_documentation
  • guards pagination parsing in getRepos() when parse-link-header returns null for malformed/unexpected Link headers
  • adds focused unit tests for both edge cases to make regressions easy to catch

Problem

The generator currently uses validateStatus: () => true for support docs HEAD checks. In that mode, HTTP 404 does not throw, so the fallback branch inside catch is skipped and broken support links may be retained.

getRepos() also assumes parseLinkHeader(...) always returns an object. If it returns null, link.next access can throw and stop README generation.

Root Cause

  1. 404 fallback mismatch
    • request config intentionally disables throw-on-404
    • fallback logic relied primarily on exception path for 404
  2. Link parser nullability
    • missing null guard before dereferencing next

Changes

1) 404 fallback correctness

  • in processMetadata(), after HEAD request, explicitly check res.status === 404 and mark remoteUrlExists=false
  • preserve existing behavior for thrown 404s
  • rethrow non-404 request errors (avoid swallowing unexpected failures)

2) Link-header null safety

  • in getRepos(), guard with if (link && link.next && link.next.url) before reading next page URL

3) Testability improvements (no behavior change for CLI usage)

  • export processMetadata and getRepos so they can be unit-tested directly
  • allow dependency injection for request/parser/client in tests
  • gate CLI execution with an isCliExecution check so importing the module does not auto-run main()
  • keep token warning only for actual CLI invocation

4) New tests

Added bin/generate-readme.test.mjs:

  • processMetadata falls back to product_documentation when support URL HEAD responds 404
  • getRepos handles parseLinkHeader returning null without throwing and still returns filtered repos

Validation

  • node --test bin/generate-readme.test.mjs
  • node --check bin/generate-readme.mjs
  • node bin/generate-readme.mjs --use-cache

Risk Assessment

Low.

  • changes are isolated to bin/generate-readme.mjs
  • default call paths are preserved (downloadRepoMetadata -> getRepos(), main -> processMetadata())
  • tests cover the newly hardened edge cases

@akgitrepos akgitrepos requested a review from a team as a code owner February 25, 2026 19:13
@akgitrepos
Copy link
Author

@pearigee Would appreciate a review on this too. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant