Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Refactor assembling Authorization header value into its own function
  • Loading branch information
mbg committed Sep 24, 2025
commit efcf614b5d8a11ca5489349120abc6d31c9e16a4
17 changes: 13 additions & 4 deletions lib/analyze-action.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 13 additions & 4 deletions lib/init-action-post.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 13 additions & 4 deletions lib/init-action.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 13 additions & 4 deletions lib/upload-lib.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 13 additions & 4 deletions lib/upload-sarif-action.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 30 additions & 1 deletion src/api-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as retry from "@octokit/plugin-retry";
import consoleLogLevel from "console-log-level";

import { getActionVersion, getRequiredInput } from "./actions-util";
import { Logger } from "./logging";
import { getRepositoryNwo, RepositoryNwo } from "./repository";
import {
ConfigurationError,
Expand Down Expand Up @@ -54,7 +55,7 @@ function createApiClientWithDetails(
);
}

export function getApiDetails() {
export function getApiDetails(): GitHubApiDetails {
return {
auth: getRequiredInput("token"),
url: getRequiredEnvParam("GITHUB_SERVER_URL"),
Expand All @@ -72,6 +73,34 @@ export function getApiClientWithExternalAuth(
return createApiClientWithDetails(apiDetails, { allowExternal: true });
}

/**
* Gets a value for the `Authorization` header to download `url` or `undefined` if the
* `Authorization` header should not be set for `url`.
*
* @param logger The logger to use for debugging messages.
* @param apiDetails Details of the GitHub API we are using.
* @param url The URL for which we want to add an `Authorization` header.
* @param purpose A description of what we want to download, for debug messages.
* @returns The value for the `Authorization` header or `undefined` if it shouldn't be populated.
*/
export function getAuthorizationHeaderFor(
logger: Logger,
apiDetails: GitHubApiDetails,
url: string,
purpose: string = "CodeQL tools",
): string | undefined {
if (
url.startsWith(`${apiDetails.url}/`) ||
(apiDetails.apiURL && url.startsWith(`${apiDetails.apiURL}/`))
) {
logger.debug(`Providing an authorization token to download ${purpose}.`);
return `token ${apiDetails.auth}`;
}

logger.debug(`Downloading ${purpose} without an authorization token.`);
Comment thread
mbg marked this conversation as resolved.
Outdated
return undefined;
}

let cachedGitHubVersion: GitHubVersion | undefined = undefined;

export async function getGitHubVersionFromApi(
Expand Down
12 changes: 5 additions & 7 deletions src/setup-codeql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -574,14 +574,12 @@ export const downloadCodeQL = async function (
let authorization: string | undefined = undefined;
if (searchParams.has("token")) {
logger.debug("CodeQL tools URL contains an authorization token.");
} else if (
codeqlURL.startsWith(`${apiDetails.url}/`) ||
(apiDetails.apiURL && codeqlURL.startsWith(`${apiDetails.apiURL}/`))
) {
logger.debug("Providing an authorization token to download CodeQL tools.");
authorization = `token ${apiDetails.auth}`;
} else {
logger.debug("Downloading CodeQL tools without an authorization token.");
authorization = api.getAuthorizationHeaderFor(
logger,
apiDetails,
codeqlURL,
);
}

const toolcacheInfo = getToolcacheDestinationInfo(
Expand Down