Fall back to gzip if zstd fails

This commit is contained in:
Henry Mercer 2024-09-23 21:59:03 +01:00
parent 662c71aa9e
commit 4d015b8cba
6 changed files with 82 additions and 33 deletions

27
lib/setup-codeql.js generated
View file

@ -198,7 +198,7 @@ async function findOverridingToolsInCache(humanReadableVersion, logger) {
}
return undefined;
}
async function getCodeQLSource(toolsInput, defaultCliVersion, apiDetails, variant, zstdAvailability, features, logger) {
async function getCodeQLSource(toolsInput, defaultCliVersion, apiDetails, variant, tarSupportsZstd, features, logger) {
if (toolsInput &&
!CODEQL_BUNDLE_VERSION_ALIAS.includes(toolsInput) &&
!toolsInput.startsWith("http")) {
@ -341,7 +341,7 @@ async function getCodeQLSource(toolsInput, defaultCliVersion, apiDetails, varian
}
if (!url) {
url = await getCodeQLBundleDownloadURL(tagName, apiDetails, cliVersion !== undefined &&
(await useZstdBundle(cliVersion, features, zstdAvailability)), logger);
(await useZstdBundle(cliVersion, features, tarSupportsZstd)), logger);
}
if (cliVersion) {
logger.info(`Using CodeQL CLI version ${cliVersion} sourced from ${url}.`);
@ -479,7 +479,20 @@ function getCanonicalToolcacheVersion(cliVersion, bundleVersion, logger) {
*/
async function setupCodeQLBundle(toolsInput, apiDetails, tempDir, variant, defaultCliVersion, features, logger) {
const zstdAvailability = await tar.isZstdAvailable(logger);
const source = await getCodeQLSource(toolsInput, defaultCliVersion, apiDetails, variant, zstdAvailability, features, logger);
// If we think the installed version of tar supports zstd, try to use zstd,
// but be prepared to fall back to gzip in case we were wrong.
if (zstdAvailability.available) {
try {
return await setupCodeQLBundleWithCompressionMethod(toolsInput, apiDetails, tempDir, variant, defaultCliVersion, features, logger, zstdAvailability, true);
}
catch (e) {
logger.warning(`Failed to set up CodeQL tools with zstd. Falling back to gzipped version. Error: ${util.getErrorMessage(e)}`);
}
}
return await setupCodeQLBundleWithCompressionMethod(toolsInput, apiDetails, tempDir, variant, defaultCliVersion, features, logger, zstdAvailability, false);
}
async function setupCodeQLBundleWithCompressionMethod(toolsInput, apiDetails, tempDir, variant, defaultCliVersion, features, logger, zstdAvailability, useTarIfAvailable) {
const source = await getCodeQLSource(toolsInput, defaultCliVersion, apiDetails, variant, useTarIfAvailable, features, logger);
let codeqlFolder;
let toolsVersion = source.toolsVersion;
let toolsDownloadStatusReport;
@ -538,9 +551,9 @@ function sanitizeUrlForStatusReport(url) {
? url
: "sanitized-value";
}
async function useZstdBundle(cliVersion, features, zstdAvailability) {
return (semver.gte(cliVersion, "2.19.0") &&
!!(await features.getValue(feature_flags_1.Feature.ZstdBundle)) &&
zstdAvailability.available);
async function useZstdBundle(cliVersion, features, tarSupportsZstd) {
return (tarSupportsZstd &&
semver.gte(cliVersion, "2.19.0") &&
!!(await features.getValue(feature_flags_1.Feature.ZstdBundle)));
}
//# sourceMappingURL=setup-codeql.js.map

File diff suppressed because one or more lines are too long

View file

@ -79,7 +79,7 @@ ava_1.default.beforeEach(() => {
(0, testing_utils_1.setupActionsVars)(tmpDir, tmpDir);
const tagName = "codeql-bundle-v1.2.3";
(0, testing_utils_1.mockBundleDownloadApi)({ tagName });
const source = await setupCodeql.getCodeQLSource(`https://github.com/github/codeql-action/releases/download/${tagName}/codeql-bundle-linux64.tar.gz`, testing_utils_1.SAMPLE_DEFAULT_CLI_VERSION, testing_utils_1.SAMPLE_DOTCOM_API_DETAILS, util_1.GitHubVariant.DOTCOM, getStubZstdAvailability(), (0, testing_utils_1.createFeatures)([]), (0, logging_1.getRunnerLogger)(true));
const source = await setupCodeql.getCodeQLSource(`https://github.com/github/codeql-action/releases/download/${tagName}/codeql-bundle-linux64.tar.gz`, testing_utils_1.SAMPLE_DEFAULT_CLI_VERSION, testing_utils_1.SAMPLE_DOTCOM_API_DETAILS, util_1.GitHubVariant.DOTCOM, false, (0, testing_utils_1.createFeatures)([]), (0, logging_1.getRunnerLogger)(true));
t.is(source.sourceType, "download");
t.is(source["cliVersion"], "1.2.3");
});
@ -87,7 +87,7 @@ ava_1.default.beforeEach(() => {
(0, ava_1.default)("getCodeQLSource correctly returns bundled CLI version when tools == linked", async (t) => {
await (0, util_1.withTmpDir)(async (tmpDir) => {
(0, testing_utils_1.setupActionsVars)(tmpDir, tmpDir);
const source = await setupCodeql.getCodeQLSource("linked", testing_utils_1.SAMPLE_DEFAULT_CLI_VERSION, testing_utils_1.SAMPLE_DOTCOM_API_DETAILS, util_1.GitHubVariant.DOTCOM, getStubZstdAvailability(), (0, testing_utils_1.createFeatures)([]), (0, logging_1.getRunnerLogger)(true));
const source = await setupCodeql.getCodeQLSource("linked", testing_utils_1.SAMPLE_DEFAULT_CLI_VERSION, testing_utils_1.SAMPLE_DOTCOM_API_DETAILS, util_1.GitHubVariant.DOTCOM, false, (0, testing_utils_1.createFeatures)([]), (0, logging_1.getRunnerLogger)(true));
t.is(source.toolsVersion, testing_utils_1.LINKED_CLI_VERSION.cliVersion);
t.is(source.sourceType, "download");
});
@ -97,7 +97,7 @@ ava_1.default.beforeEach(() => {
const logger = (0, testing_utils_1.getRecordingLogger)(loggedMessages);
await (0, util_1.withTmpDir)(async (tmpDir) => {
(0, testing_utils_1.setupActionsVars)(tmpDir, tmpDir);
const source = await setupCodeql.getCodeQLSource("latest", testing_utils_1.SAMPLE_DEFAULT_CLI_VERSION, testing_utils_1.SAMPLE_DOTCOM_API_DETAILS, util_1.GitHubVariant.DOTCOM, getStubZstdAvailability(), (0, testing_utils_1.createFeatures)([]), logger);
const source = await setupCodeql.getCodeQLSource("latest", testing_utils_1.SAMPLE_DEFAULT_CLI_VERSION, testing_utils_1.SAMPLE_DOTCOM_API_DETAILS, util_1.GitHubVariant.DOTCOM, false, (0, testing_utils_1.createFeatures)([]), logger);
// First, ensure that the CLI version is the linked version, so that backwards
// compatibility is maintained.
t.is(source.toolsVersion, testing_utils_1.LINKED_CLI_VERSION.cliVersion);
@ -164,9 +164,4 @@ ava_1.default.beforeEach(() => {
msg.message.includes(expected_message)));
});
});
function getStubZstdAvailability() {
return {
available: false,
};
}
//# sourceMappingURL=setup-codeql.test.js.map

File diff suppressed because one or more lines are too long