Skip to content

Commit efcf614

Browse files
committed
Refactor assembling Authorization header value into its own function
1 parent a8eeef9 commit efcf614

File tree

7 files changed

+100
-28
lines changed

7 files changed

+100
-28
lines changed

lib/analyze-action.js

Lines changed: 13 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/init-action-post.js

Lines changed: 13 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/init-action.js

Lines changed: 13 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/upload-lib.js

Lines changed: 13 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/upload-sarif-action.js

Lines changed: 13 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/api-client.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import * as retry from "@octokit/plugin-retry";
44
import consoleLogLevel from "console-log-level";
55

66
import { getActionVersion, getRequiredInput } from "./actions-util";
7+
import { Logger } from "./logging";
78
import { getRepositoryNwo, RepositoryNwo } from "./repository";
89
import {
910
ConfigurationError,
@@ -54,7 +55,7 @@ function createApiClientWithDetails(
5455
);
5556
}
5657

57-
export function getApiDetails() {
58+
export function getApiDetails(): GitHubApiDetails {
5859
return {
5960
auth: getRequiredInput("token"),
6061
url: getRequiredEnvParam("GITHUB_SERVER_URL"),
@@ -72,6 +73,34 @@ export function getApiClientWithExternalAuth(
7273
return createApiClientWithDetails(apiDetails, { allowExternal: true });
7374
}
7475

76+
/**
77+
* Gets a value for the `Authorization` header to download `url` or `undefined` if the
78+
* `Authorization` header should not be set for `url`.
79+
*
80+
* @param logger The logger to use for debugging messages.
81+
* @param apiDetails Details of the GitHub API we are using.
82+
* @param url The URL for which we want to add an `Authorization` header.
83+
* @param purpose A description of what we want to download, for debug messages.
84+
* @returns The value for the `Authorization` header or `undefined` if it shouldn't be populated.
85+
*/
86+
export function getAuthorizationHeaderFor(
87+
logger: Logger,
88+
apiDetails: GitHubApiDetails,
89+
url: string,
90+
purpose: string = "CodeQL tools",
91+
): string | undefined {
92+
if (
93+
url.startsWith(`${apiDetails.url}/`) ||
94+
(apiDetails.apiURL && url.startsWith(`${apiDetails.apiURL}/`))
95+
) {
96+
logger.debug(`Providing an authorization token to download ${purpose}.`);
97+
return `token ${apiDetails.auth}`;
98+
}
99+
100+
logger.debug(`Downloading ${purpose} without an authorization token.`);
101+
return undefined;
102+
}
103+
75104
let cachedGitHubVersion: GitHubVersion | undefined = undefined;
76105

77106
export async function getGitHubVersionFromApi(

src/setup-codeql.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -574,14 +574,12 @@ export const downloadCodeQL = async function (
574574
let authorization: string | undefined = undefined;
575575
if (searchParams.has("token")) {
576576
logger.debug("CodeQL tools URL contains an authorization token.");
577-
} else if (
578-
codeqlURL.startsWith(`${apiDetails.url}/`) ||
579-
(apiDetails.apiURL && codeqlURL.startsWith(`${apiDetails.apiURL}/`))
580-
) {
581-
logger.debug("Providing an authorization token to download CodeQL tools.");
582-
authorization = `token ${apiDetails.auth}`;
583577
} else {
584-
logger.debug("Downloading CodeQL tools without an authorization token.");
578+
authorization = api.getAuthorizationHeaderFor(
579+
logger,
580+
apiDetails,
581+
codeqlURL,
582+
);
585583
}
586584

587585
const toolcacheInfo = getToolcacheDestinationInfo(

0 commit comments

Comments
 (0)