Skip to content

Commit

Permalink
Infer compression method from URL
Browse files Browse the repository at this point in the history
Using the downloaded path is unreliable since we may have removed the file extension.
  • Loading branch information
henrymercer committed Aug 29, 2024
1 parent 379271d commit 27dbb1a
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 36 deletions.
7 changes: 4 additions & 3 deletions lib/setup-codeql.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/setup-codeql.js.map

Large diffs are not rendered by default.

27 changes: 15 additions & 12 deletions lib/tar.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/tar.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 9 additions & 4 deletions src/setup-codeql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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" },
Expand All @@ -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).`,
Expand Down Expand Up @@ -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;
}
Expand Down
33 changes: 18 additions & 15 deletions src/tar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}

export function inferCompressionMethod(path: string): CompressionMethod {
if (path.endsWith(".tar.gz")) {
return "gzip";
}
return {
compressionMethod: "zstd",
// By specifying only the "x" flag, we ask tar to autodetect the compression
// method.
outputPath: await toolcache.extractTar(path, undefined, "x"),
};
return "zstd";
}

0 comments on commit 27dbb1a

Please sign in to comment.