Skip to content

Commit 680fa50

Browse files
committed
refactor(github): build request headers once
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.
1 parent e6a9867 commit 680fa50

1 file changed

Lines changed: 8 additions & 14 deletions

File tree

‎src/github/api.ts‎

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -182,10 +182,8 @@ export class GitHubApi implements GitHubClient {
182182
private readonly octokit: InstanceType<typeof ActionOctokit>;
183183
private readonly trace: TraceRedaction;
184184
private readonly baseUrl: string;
185-
private readonly apiVersion: string;
186185
constructor(options: GitHubApiOptions) {
187186
this.baseUrl = options.baseUrl ?? process.env.GITHUB_API_URL ?? "https://api.github.com";
188-
this.apiVersion = options.apiVersion ?? DEFAULT_API_VERSION;
189187
this.trace = new TraceRedaction(options.io ?? SILENT_TRACE);
190188
const envKnob = envRetryBaseMs();
191189
const retryBaseMs = options.retryBaseMs ?? envKnob ?? 1000;
@@ -219,6 +217,13 @@ export class GitHubApi implements GitHubClient {
219217
onSecondaryRateLimit: throttleCallback("secondary rate limit", this.trace),
220218
},
221219
});
220+
// A per-request accept (the raw media-type read) overrides the default; the hooks the plugins wrapped stay.
221+
this.octokit.request = this.octokit.request.defaults({
222+
headers: {
223+
accept: "application/vnd.github+json",
224+
"x-github-api-version": options.apiVersion ?? DEFAULT_API_VERSION,
225+
},
226+
});
222227
}
223228

224229
async tryRequest(
@@ -295,10 +300,6 @@ export class GitHubApi implements GitHubClient {
295300
url: path,
296301
per_page: options?.perPage ?? PAGE_SIZE,
297302
page: 1,
298-
headers: {
299-
accept: "application/vnd.github+json",
300-
"x-github-api-version": this.apiVersion,
301-
},
302303
} as unknown as Parameters<RequestInterface>[0];
303304
try {
304305
for await (const _page of this.octokit.paginate.iterator(request, firstPage)) {
@@ -357,10 +358,7 @@ export class GitHubApi implements GitHubClient {
357358
const response = await this.octokit.request({
358359
method,
359360
url: path,
360-
headers: {
361-
accept: options?.accept ?? "application/vnd.github+json",
362-
"x-github-api-version": this.apiVersion,
363-
},
361+
...(options?.accept === undefined ? {} : { headers: { accept: options.accept } }),
364362
// The body is the tree the scan inspected, so octokit never reshapes the payload and the wire carries exactly what was scanned.
365363
...(payload === undefined ? {} : { data: secretScan.value.payload }),
366364
} as unknown as Parameters<InstanceType<typeof ActionOctokit>["request"]>[0]);
@@ -437,10 +435,6 @@ export class GitHubApi implements GitHubClient {
437435
response = (await this.octokit.request({
438436
method: "POST",
439437
url: "/graphql",
440-
headers: {
441-
accept: "application/vnd.github+json",
442-
"x-github-api-version": this.apiVersion,
443-
},
444438
// operationName makes the request self-describing on the wire (the mock dispatches on it).
445439
data: { query: op.query, operationName: op.name, variables: scan.value.payload },
446440
} as unknown as Parameters<InstanceType<typeof ActionOctokit>["request"]>[0])) as {

0 commit comments

Comments
 (0)