Infer compression method from URL

Using the downloaded path is unreliable since we may have removed the file extension.
This commit is contained in:
Henry Mercer 2024-08-23 23:19:19 +01:00
parent 379271d235
commit 27dbb1ab21
6 changed files with 48 additions and 36 deletions

7
lib/setup-codeql.js generated
View file

@ -390,6 +390,7 @@ const downloadCodeQL = async function (codeqlURL, maybeBundleVersion, maybeCliVe
logger.debug("Downloading CodeQL tools without an authorization token."); logger.debug("Downloading CodeQL tools without an authorization token.");
} }
logger.info(`Downloading CodeQL tools from ${codeqlURL} . This may take a while.`); logger.info(`Downloading CodeQL tools from ${codeqlURL} . This may take a while.`);
const compressionMethod = tar.inferCompressionMethod(codeqlURL);
const dest = path.join(tempDir, (0, uuid_1.v4)()); const dest = path.join(tempDir, (0, uuid_1.v4)());
const finalHeaders = Object.assign({ "User-Agent": "CodeQL Action" }, headers); const finalHeaders = Object.assign({ "User-Agent": "CodeQL Action" }, headers);
const toolsDownloadStart = perf_hooks_1.performance.now(); const toolsDownloadStart = perf_hooks_1.performance.now();
@ -398,7 +399,7 @@ const downloadCodeQL = async function (codeqlURL, maybeBundleVersion, maybeCliVe
logger.debug(`Finished downloading CodeQL bundle to ${archivedBundlePath} (${downloadDurationMs} ms).`); logger.debug(`Finished downloading CodeQL bundle to ${archivedBundlePath} (${downloadDurationMs} ms).`);
logger.debug("Extracting CodeQL bundle."); logger.debug("Extracting CodeQL bundle.");
const extractionStart = perf_hooks_1.performance.now(); const extractionStart = perf_hooks_1.performance.now();
const { compressionMethod, outputPath: extractedBundlePath } = await tar.extract(archivedBundlePath); const extractedBundlePath = await tar.extract(archivedBundlePath, compressionMethod);
const extractionDurationMs = Math.round(perf_hooks_1.performance.now() - extractionStart); const extractionDurationMs = Math.round(perf_hooks_1.performance.now() - extractionStart);
logger.debug(`Finished extracting CodeQL bundle to ${extractedBundlePath} (${extractionDurationMs} ms).`); logger.debug(`Finished extracting CodeQL bundle to ${extractedBundlePath} (${extractionDurationMs} ms).`);
await cleanUpGlob(archivedBundlePath, "CodeQL bundle archive", logger); await cleanUpGlob(archivedBundlePath, "CodeQL bundle archive", logger);
@ -476,8 +477,8 @@ async function setupCodeQLBundle(toolsInput, apiDetails, tempDir, variant, defau
let toolsSource; let toolsSource;
switch (source.sourceType) { switch (source.sourceType) {
case "local": { case "local": {
const { outputPath } = await tar.extract(source.codeqlTarPath); const compressionMethod = tar.inferCompressionMethod(source.codeqlTarPath);
codeqlFolder = outputPath; codeqlFolder = await tar.extract(source.codeqlTarPath, compressionMethod);
toolsSource = ToolsSource.Local; toolsSource = ToolsSource.Local;
break; break;
} }

File diff suppressed because one or more lines are too long

27
lib/tar.js generated
View file

@ -25,6 +25,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "__esModule", { value: true });
exports.isZstdAvailable = isZstdAvailable; exports.isZstdAvailable = isZstdAvailable;
exports.extract = extract; exports.extract = extract;
exports.inferCompressionMethod = inferCompressionMethod;
const toolrunner_1 = require("@actions/exec/lib/toolrunner"); const toolrunner_1 = require("@actions/exec/lib/toolrunner");
const toolcache = __importStar(require("@actions/tool-cache")); const toolcache = __importStar(require("@actions/tool-cache"));
const safe_which_1 = require("@chrisgavin/safe-which"); const safe_which_1 = require("@chrisgavin/safe-which");
@ -89,21 +90,23 @@ async function isZstdAvailable(logger) {
return { available: false }; return { available: false };
} }
} }
async function extract(path) { async function extract(path, compressionMethod) {
if (path.endsWith(".tar.gz")) { switch (compressionMethod) {
return { case "gzip":
compressionMethod: "gzip",
// While we could also ask tar to autodetect the compression method, // While we could also ask tar to autodetect the compression method,
// we defensively keep the gzip call identical as requesting a gzipped // we defensively keep the gzip call identical as requesting a gzipped
// bundle will soon be a fallback option. // bundle will soon be a fallback option.
outputPath: await toolcache.extractTar(path), return await toolcache.extractTar(path);
}; case "zstd":
// By specifying only the "x" flag, we ask tar to autodetect the
// compression method.
return await toolcache.extractTar(path, undefined, "x");
} }
return { }
compressionMethod: "zstd", function inferCompressionMethod(path) {
// By specifying only the "x" flag, we ask tar to autodetect the compression if (path.endsWith(".tar.gz")) {
// method. return "gzip";
outputPath: await toolcache.extractTar(path, undefined, "x"), }
}; return "zstd";
} }
//# sourceMappingURL=tar.js.map //# sourceMappingURL=tar.js.map

View file

@ -1 +1 @@
{"version":3,"file":"tar.js","sourceRoot":"","sources":["../src/tar.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AAgDA,0CA4BC;AAID,0BAmBC;AAnGD,6DAA0D;AAC1D,+DAAiD;AACjD,uDAAmD;AAGnD,iCAAqC;AAErC,MAAM,4BAA4B,GAAG,OAAO,CAAC;AAC7C,MAAM,4BAA4B,GAAG,MAAM,CAAC;AAO5C,KAAK,UAAU,aAAa;IAC1B,MAAM,GAAG,GAAG,MAAM,IAAA,sBAAS,EAAC,KAAK,CAAC,CAAC;IACnC,IAAI,MAAM,GAAG,EAAE,CAAC;IAChB,MAAM,QAAQ,GAAG,MAAM,IAAI,uBAAU,CAAC,GAAG,EAAE,CAAC,WAAW,CAAC,EAAE;QACxD,SAAS,EAAE;YACT,MAAM,EAAE,CAAC,IAAY,EAAE,EAAE;gBACvB,MAAM,IAAI,IAAI,CAAC,QAAQ,EAAE,CAAC;YAC5B,CAAC;SACF;KACF,CAAC,CAAC,IAAI,EAAE,CAAC;IACV,IAAI,QAAQ,KAAK,CAAC,EAAE,CAAC;QACnB,MAAM,IAAI,KAAK,CAAC,8BAA8B,CAAC,CAAC;IAClD,CAAC;IACD,oEAAoE;IACpE,IAAI,MAAM,CAAC,QAAQ,CAAC,SAAS,CAAC,EAAE,CAAC;QAC/B,MAAM,KAAK,GAAG,MAAM,CAAC,KAAK,CAAC,2BAA2B,CAAC,CAAC;QACxD,IAAI,CAAC,KAAK,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,CAAC;YACxB,MAAM,IAAI,KAAK,CAAC,0CAA0C,CAAC,CAAC;QAC9D,CAAC;QAED,OAAO,EAAE,IAAI,EAAE,KAAK,EAA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AAO,IAAI,4BAA4B;oBAClD,OAAO,EAAE,UAAU;iBACpB,CAAC;YACJ;gBACE,IAAA,kBAAW,EAAC,IAAI,CAAC,CAAC;QACtB,CAAC;IACH,CAAC;IAAC,OAAO,CAAC,EAAE,CAAC;QACX,MAAM,CAAC,KAAK,CACV,oFAAoF;YAClF,6BAA6B,CAAC,EAAE,CACnC,CAAC;QACF,OAAO,EAAE,SAAS,EAAE,KAAK,EAAE,CAAC;IAC9B,CAAC;AACH,CAAC;AAIM,KAAK,UAAU,OAAO,CAC3B,IAAY,EACZ,iBAAoC;IAEpC,QAAQ,iBAAiB,EAAE,CAAC;QAC1B,KAAK,MAAM;YACT,oEAAoE;YACpE,sEAAsE;YACtE,yCAAyC;YACzC,OAAO,MAAM,SAAS,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC;QAC1C,KAAK,MAAM;YACT,gEAAgE;YAChE,sBAAsB;YACtB,OAAO,MAAM,SAAS,CAAC,UAAU,CAAC,IAAI,EAAE,SAAS,EAAE,GAAG,CAAC,CAAC;IAC5D,CAAC;AACH,CAAC;AAED,SAAgB,sBAAsB,CAAC,IAAY;IACjD,IAAI,IAAI,CAAC,QAAQ,CAAC,SAAS,CAAC,EAAE,CAAC;QAC7B,OAAO,MAAM,CAAC;IAChB,CAAC;IACD,OAAO,MAAM,CAAC;AAChB,CAAC"}

View file

@ -507,6 +507,7 @@ export const downloadCodeQL = async function (
`Downloading CodeQL tools from ${codeqlURL} . This may take a while.`, `Downloading CodeQL tools from ${codeqlURL} . This may take a while.`,
); );
const compressionMethod = tar.inferCompressionMethod(codeqlURL);
const dest = path.join(tempDir, uuidV4()); const dest = path.join(tempDir, uuidV4());
const finalHeaders = Object.assign( const finalHeaders = Object.assign(
{ "User-Agent": "CodeQL Action" }, { "User-Agent": "CodeQL Action" },
@ -528,8 +529,10 @@ export const downloadCodeQL = async function (
logger.debug("Extracting CodeQL bundle."); logger.debug("Extracting CodeQL bundle.");
const extractionStart = performance.now(); const extractionStart = performance.now();
const { compressionMethod, outputPath: extractedBundlePath } = const extractedBundlePath = await tar.extract(
await tar.extract(archivedBundlePath); archivedBundlePath,
compressionMethod,
);
const extractionDurationMs = Math.round(performance.now() - extractionStart); const extractionDurationMs = Math.round(performance.now() - extractionStart);
logger.debug( logger.debug(
`Finished extracting CodeQL bundle to ${extractedBundlePath} (${extractionDurationMs} ms).`, `Finished extracting CodeQL bundle to ${extractedBundlePath} (${extractionDurationMs} ms).`,
@ -658,8 +661,10 @@ export async function setupCodeQLBundle(
let toolsSource: ToolsSource; let toolsSource: ToolsSource;
switch (source.sourceType) { switch (source.sourceType) {
case "local": { case "local": {
const { outputPath } = await tar.extract(source.codeqlTarPath); const compressionMethod = tar.inferCompressionMethod(
codeqlFolder = outputPath; source.codeqlTarPath,
);
codeqlFolder = await tar.extract(source.codeqlTarPath, compressionMethod);
toolsSource = ToolsSource.Local; toolsSource = ToolsSource.Local;
break; break;
} }

View file

@ -78,23 +78,26 @@ export async function isZstdAvailable(
export type CompressionMethod = "gzip" | "zstd"; export type CompressionMethod = "gzip" | "zstd";
export async function extract(path: string): Promise<{ export async function extract(
compressionMethod: CompressionMethod; path: string,
outputPath: string; compressionMethod: CompressionMethod,
}> { ): Promise<string> {
if (path.endsWith(".tar.gz")) { switch (compressionMethod) {
return { case "gzip":
compressionMethod: "gzip",
// While we could also ask tar to autodetect the compression method, // While we could also ask tar to autodetect the compression method,
// we defensively keep the gzip call identical as requesting a gzipped // we defensively keep the gzip call identical as requesting a gzipped
// bundle will soon be a fallback option. // bundle will soon be a fallback option.
outputPath: await toolcache.extractTar(path), return await toolcache.extractTar(path);
}; case "zstd":
// By specifying only the "x" flag, we ask tar to autodetect the
// compression method.
return await toolcache.extractTar(path, undefined, "x");
} }
return { }
compressionMethod: "zstd",
// By specifying only the "x" flag, we ask tar to autodetect the compression export function inferCompressionMethod(path: string): CompressionMethod {
// method. if (path.endsWith(".tar.gz")) {
outputPath: await toolcache.extractTar(path, undefined, "x"), return "gzip";
}; }
return "zstd";
} }