Skip to content

refactor(github): build request headers once - #492

Merged
Vivswan merged 1 commit into
mainfrom
refactor/github-api-headers-once
Oct 6, 2026
Merged

Vivswan merged 1 commit into
mainfrom
refactor/github-api-headers-once

Conversation

@Vivswan

@Vivswan Vivswan commented Oct 6, 2026 •

Copy link
Copy Markdown
Owner

Before

src/github/api.ts, three request sites, each with its own header literal:
  tryList()     headers: { accept: "application/vnd.github+json", "x-github-api-version": this.apiVersion }
  request()     headers: { accept: options?.accept ?? "application/vnd.github+json", "x-github-api-version": this.apiVersion }
  tryGraphql()  headers: { accept: "application/vnd.github+json", "x-github-api-version": this.apiVersion }

After

constructor   this.octokit.request = this.octokit.request.defaults({ headers: { accept, "x-github-api-version" } })
  tryList()     (no headers)
  request()     ...(options?.accept === undefined ? {} : { headers: { accept: options.accept } })
  tryGraphql()  (no headers)

How

  • Octokit's request defaults carry the two headers every request sends, set once after construction.
  • The raw media-type read still passes its own accept, which the defaults merge lets override.
  • The plugins' hooks stay: defaults() keeps the wrapped request hook, so retry, throttling, and the request log see every call.
  • Nothing on the wire changes: the same two headers with the same values leave each path.

Proof

  • Census: grep -c '"x-github-api-version"' src/github/api.ts, 3 before, 1 after.
  • E2e: bun test/e2e/run.ts --sections labels,branches, 98/98; the mock refuses any request without the version header, on the list, request, and GraphQL paths alike.
  • Tests: bun run test test/github, 123 pass, none added.
  • Gates: typecheck and biome clean.
Technical details
  • Library: @octokit/core 7.0.8 (already a dependency), request.defaults({ headers }); the constructor takes no header option beyond userAgent and timeZone, so the defaults are set right after construction.
  • Accounting: 1 file, +8 -14.
  • The paginate iterator's first page inherits the defaults too: the walk's request wrapper reuses the reassigned request's endpoint and defaults.
  • No new test: a pin of the defaults would restate the source; the e2e mock's refusal is the wire-level pin.

Copilot AI balanced review requested due to automatic review settings October 6, 2026 10:09

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟢 Approval recommended

The small extraction preserves supported request behavior, and no blocking issues remain.

Review effort: Balanced
Findings: None

What changed in this PR

Centralizes request-header construction in GitHubApi without changing supported header values.

Changes:

  • Adds a private builder for the accept and API-version headers.
  • Uses it for REST requests, pagination, and GraphQL, preserving custom accept values.
File Description
src/​github/​api.ts Replaces three inline header objects with one shared builder.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

GitHubApi built the accept and x-github-api-version pair inline at three request sites (tryList, request, tryGraphql). The pair now rides on Octokit's request defaults, set once after construction; the raw media-type read passes its own accept, which the defaults merge lets override, and the plugins' wrapped hook stays. Nothing on the wire changes.
Copilot AI balanced review requested due to automatic review settings October 6, 2026 10:42
@Vivswan
Vivswan force-pushed the refactor/github-api-headers-once branch from 79a27e9 to 680fa50 Compare October 6, 2026 10:42

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟢 Approval recommended

The focused refactor preserves header values and request hooks, with no unresolved findings.

Review effort: Balanced
Findings: None

@Vivswan
Vivswan marked this pull request as ready for review October 6, 2026 10:49
@Vivswan
Vivswan enabled auto-merge (squash) October 6, 2026 20:36
@Vivswan Vivswan added the merge-when-green Owner approved: merge once every gate is green label Oct 6, 2026
@Vivswan
Vivswan merged commit 5842a9e into main Oct 6, 2026
32 checks passed
@Vivswan
Vivswan deleted the refactor/github-api-headers-once branch October 6, 2026 20:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

merge-when-green Owner approved: merge once every gate is green

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants