diff --git a/lib/entry-points.js b/lib/entry-points.js index 186a09106d..62b043f7b8 100644 --- a/lib/entry-points.js +++ b/lib/entry-points.js @@ -146756,15 +146756,19 @@ async function getGitHubVersionFromApi(apiClient, apiDetails) { if (parseGitHubUrl(apiDetails.url) === GITHUB_DOTCOM_URL) { return { type: "GitHub.com" /* DOTCOM */ }; } - const response = await apiClient.rest.meta.get(); - if (response.headers[GITHUB_ENTERPRISE_VERSION_HEADER] === void 0) { - return { type: "GitHub.com" /* DOTCOM */ }; - } - if (response.headers[GITHUB_ENTERPRISE_VERSION_HEADER] === "ghe.com") { - return { type: "GitHub Enterprise Cloud with data residency" /* GHEC_DR */ }; + try { + const response = await apiClient.rest.meta.get(); + if (response.headers[GITHUB_ENTERPRISE_VERSION_HEADER] === void 0) { + return { type: "GitHub.com" /* DOTCOM */ }; + } + if (response.headers[GITHUB_ENTERPRISE_VERSION_HEADER] === "ghe.com") { + return { type: "GitHub Enterprise Cloud with data residency" /* GHEC_DR */ }; + } + const version = response.headers[GITHUB_ENTERPRISE_VERSION_HEADER]; + return { type: "GitHub Enterprise Server" /* GHES */, version }; + } catch (err) { + throw wrapApiConfigurationError(err); } - const version = response.headers[GITHUB_ENTERPRISE_VERSION_HEADER]; - return { type: "GitHub Enterprise Server" /* GHES */, version }; } async function getGitHubVersion() { if (cachedGitHubVersion === void 0) { @@ -163041,7 +163045,8 @@ async function run4(startedAt) { try { const jobStatus2 = getOptionalInput("job-status"); restoreInputs(logger); - const gitHubVersion = await getGitHubVersion(); + config = await getConfig(getTemporaryDirectory(), logger); + const gitHubVersion = config?.gitHubVersion ?? await getGitHubVersion(); checkGitHubVersionInRange(gitHubVersion, logger); const repositoryNwo = getRepositoryNwo(); const features = initFeatures( @@ -163050,7 +163055,6 @@ async function run4(startedAt) { getTemporaryDirectory(), logger ); - config = await getConfig(getTemporaryDirectory(), logger); if (config === void 0) { logger.warning( "Debugging artifacts are unavailable since the 'init' Action failed before it could produce any." @@ -163073,7 +163077,7 @@ async function run4(startedAt) { } } } catch (unwrappedError) { - const error3 = wrapError(unwrappedError); + const error3 = wrapApiConfigurationError(wrapError(unwrappedError)); core23.setFailed(error3.message); const statusReportBase2 = await createStatusReportBase( "init-post" /* InitPost */, diff --git a/src/api-client.ts b/src/api-client.ts index e509f5eea0..de62f4db44 100644 --- a/src/api-client.ts +++ b/src/api-client.ts @@ -219,25 +219,31 @@ export async function getGitHubVersionFromApi( return { type: GitHubVariant.DOTCOM }; } - // Doesn't strictly have to be the meta endpoint as we're only - // using the response headers which are available on every request. - // - // See https://docs.github.com/en/rest/meta/meta#get-github-meta-information. - // eslint-disable-next-line @typescript-eslint/no-unsafe-call - const response = await apiClient.rest.meta.get(); - - // This happens on dotcom, although we expect to have already returned in that - // case. This can also serve as a fallback in cases we haven't foreseen. - if (response.headers[GITHUB_ENTERPRISE_VERSION_HEADER] === undefined) { - return { type: GitHubVariant.DOTCOM }; - } + try { + // Doesn't strictly have to be the meta endpoint as we're only + // using the response headers which are available on every request. + // + // See https://docs.github.com/en/rest/meta/meta#get-github-meta-information. + // eslint-disable-next-line @typescript-eslint/no-unsafe-call + const response = await apiClient.rest.meta.get(); + + // This happens on dotcom, although we expect to have already returned in that + // case. This can also serve as a fallback in cases we haven't foreseen. + if (response.headers[GITHUB_ENTERPRISE_VERSION_HEADER] === undefined) { + return { type: GitHubVariant.DOTCOM }; + } - if (response.headers[GITHUB_ENTERPRISE_VERSION_HEADER] === "ghe.com") { - return { type: GitHubVariant.GHEC_DR }; - } + if (response.headers[GITHUB_ENTERPRISE_VERSION_HEADER] === "ghe.com") { + return { type: GitHubVariant.GHEC_DR }; + } - const version = response.headers[GITHUB_ENTERPRISE_VERSION_HEADER] as string; - return { type: GitHubVariant.GHES, version }; + const version = response.headers[ + GITHUB_ENTERPRISE_VERSION_HEADER + ] as string; + return { type: GitHubVariant.GHES, version }; + } catch (err) { + throw wrapApiConfigurationError(err); + } } /** @@ -415,7 +421,14 @@ export function getFeatureEnablementError(message: string): string { return `Please verify that the necessary features are enabled: ${message}`; } -export function wrapApiConfigurationError(e: unknown) { +/** + * Decides whether `e` is a known error returned by the GitHub API that we should + * classify as a `ConfigurationError`. + * + * @param e The error to classify. + * @returns Either `e` or a corresponding `ConfigurationError`. + */ +export function wrapApiConfigurationError(e: T): T | ConfigurationError { const httpError = asHTTPError(e); if (httpError !== undefined) { if ( diff --git a/src/init-action-post.ts b/src/init-action-post.ts index e3a68b0e57..a2bba6d7a4 100644 --- a/src/init-action-post.ts +++ b/src/init-action-post.ts @@ -12,7 +12,7 @@ import { getTemporaryDirectory, printDebugLogs, } from "./actions-util"; -import { getGitHubVersion } from "./api-client"; +import { getGitHubVersion, wrapApiConfigurationError } from "./api-client"; import { CachingKind } from "./caching-utils"; import { getCodeQL } from "./codeql"; import { type Config, getConfig } from "./config-utils"; @@ -64,7 +64,9 @@ async function run(startedAt: Date) { // Restore inputs from `init` Action. restoreInputs(logger); - const gitHubVersion = await getGitHubVersion(); + config = await getConfig(getTemporaryDirectory(), logger); + + const gitHubVersion = config?.gitHubVersion ?? (await getGitHubVersion()); checkGitHubVersionInRange(gitHubVersion, logger); const repositoryNwo = getRepositoryNwo(); @@ -75,7 +77,6 @@ async function run(startedAt: Date) { logger, ); - config = await getConfig(getTemporaryDirectory(), logger); if (config === undefined) { logger.warning( "Debugging artifacts are unavailable since the 'init' Action failed before it could produce any.", @@ -107,7 +108,7 @@ async function run(startedAt: Date) { } } } catch (unwrappedError) { - const error = wrapError(unwrappedError); + const error = wrapApiConfigurationError(wrapError(unwrappedError)); core.setFailed(error.message); const statusReportBase = await createStatusReportBase(