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

View file

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

View file

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