Skip to content

Commit 6ed1ccd

Browse files
authoredAug 10, 2023
Merge pull request #1832 from github/henrymercer/nicer-toolcache-version-numbers
Simplify toolcache version number for semantically versioned bundles
2 parents 9e4932e + ceb4b69 commit 6ed1ccd

7 files changed

+113
-28
lines changed
 

‎CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ See the [releases page](https://github.com/github/codeql-action/releases) for th
55
## [UNRELEASED]
66

77
- Log a warning if the amount of available disk space runs low during a code scanning run. [#1825](https://github.com/github/codeql-action/pull/1825)
8+
- When downloading CodeQL bundle version 2.13.4 and later, cache these bundles in the Actions tool cache using a simpler version number. [#1832](https://github.com/github/codeql-action/pull/1832)
89

910
## 2.21.3 - 08 Aug 2023
1011

‎lib/codeql.test.js

+15
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎lib/codeql.test.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎lib/setup-codeql.js

+29-12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎lib/setup-codeql.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/codeql.test.ts

+25
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,31 @@ test("downloads and caches explicitly requested bundles that aren't in the toolc
165165
});
166166
});
167167

168+
test("caches semantically versioned bundles using their semantic version number", async (t) => {
169+
await util.withTmpDir(async (tmpDir) => {
170+
setupActionsVars(tmpDir, tmpDir);
171+
const url = mockBundleDownloadApi({
172+
tagName: `codeql-bundle-v2.14.0`,
173+
isPinned: false,
174+
});
175+
const result = await codeql.setupCodeQL(
176+
url,
177+
SAMPLE_DOTCOM_API_DETAILS,
178+
tmpDir,
179+
util.GitHubVariant.DOTCOM,
180+
SAMPLE_DEFAULT_CLI_VERSION,
181+
getRunnerLogger(true),
182+
false,
183+
);
184+
185+
t.is(toolcache.findAllVersions("CodeQL").length, 1);
186+
t.assert(toolcache.find("CodeQL", `2.14.0`));
187+
t.is(result.toolsVersion, `2.14.0`);
188+
t.is(result.toolsSource, ToolsSource.Download);
189+
t.assert(Number.isInteger(result.toolsDownloadDurationMs));
190+
});
191+
});
192+
168193
test("downloads an explicitly requested bundle even if a different version is cached", async (t) => {
169194
await util.withTmpDir(async (tmpDir) => {
170195
setupActionsVars(tmpDir, tmpDir);

‎src/setup-codeql.ts

+41-14
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ import * as api from "./api-client";
1515
// creation scripts. Ensure that any changes to the format of this file are compatible with both of
1616
// these dependents.
1717
import * as defaults from "./defaults.json";
18-
import { CodeQLDefaultVersionInfo } from "./feature-flags";
18+
import {
19+
CODEQL_VERSION_BUNDLE_SEMANTICALLY_VERSIONED,
20+
CodeQLDefaultVersionInfo,
21+
} from "./feature-flags";
1922
import { Logger } from "./logging";
2023
import * as util from "./util";
2124
import { isGoodVersion, wrapError } from "./util";
@@ -610,20 +613,12 @@ export async function downloadCodeQL(
610613
);
611614
}
612615

613-
// Include both the CLI version and the bundle version in the toolcache version number. That way
614-
// if the user requests the same URL again, we can get it from the cache without having to call
615-
// any of the Releases API.
616-
//
617-
// Special case: If the CLI version is a pre-release or contains build metadata, then cache the
618-
// bundle as `0.0.0-<bundleVersion>` to avoid the bundle being interpreted as containing a stable
619-
// CLI release. In principle, it should be enough to just check that the CLI version isn't a
620-
// pre-release, but the version numbers of CodeQL nightlies have the format `x.y.z+<timestamp>`,
621-
// and we don't want these nightlies to override stable CLI versions in the toolcache.
622-
const toolcacheVersion = maybeCliVersion?.match(/^[0-9]+\.[0-9]+\.[0-9]+$/)
623-
? `${maybeCliVersion}-${bundleVersion}`
624-
: convertToSemVer(bundleVersion, logger);
625-
626616
logger.debug("Caching CodeQL bundle.");
617+
const toolcacheVersion = getCanonicalToolcacheVersion(
618+
maybeCliVersion,
619+
bundleVersion,
620+
logger,
621+
);
627622
const toolcachedBundlePath = await toolcache.cacheDir(
628623
extractedBundlePath,
629624
"CodeQL",
@@ -656,6 +651,38 @@ export function getCodeQLURLVersion(url: string): string {
656651
return match[1];
657652
}
658653

654+
/**
655+
* Returns the toolcache version number to use to store the bundle with the associated CLI version
656+
* and bundle version.
657+
*
658+
* This is the canonical version number, since toolcaches populated by different versions of the
659+
* CodeQL Action or different runner image creation scripts may store the bundle using a different
660+
* version number. Functions like `getCodeQLSource` that fetch the bundle from rather than save the
661+
* bundle to the toolcache should handle these different version numbers.
662+
*/
663+
function getCanonicalToolcacheVersion(
664+
cliVersion: string | undefined,
665+
bundleVersion: string,
666+
logger: Logger,
667+
) {
668+
// If the CLI version is a pre-release or contains build metadata, then cache the
669+
// bundle as `0.0.0-<bundleVersion>` to avoid the bundle being interpreted as containing a stable
670+
// CLI release. In principle, it should be enough to just check that the CLI version isn't a
671+
// pre-release, but the version numbers of CodeQL nightlies have the format `x.y.z+<timestamp>`,
672+
// and we don't want these nightlies to override stable CLI versions in the toolcache.
673+
if (!cliVersion?.match(/^[0-9]+\.[0-9]+\.[0-9]+$/)) {
674+
return convertToSemVer(bundleVersion, logger);
675+
}
676+
// If the bundle is semantically versioned, it can be looked up based on just the CLI version
677+
// number, so version it in the toolcache using just the CLI version number.
678+
if (semver.gte(cliVersion, CODEQL_VERSION_BUNDLE_SEMANTICALLY_VERSIONED)) {
679+
return cliVersion;
680+
}
681+
// Include both the CLI version and the bundle version in the toolcache version number. That way
682+
// we can find the bundle in the toolcache based on either the CLI version or the bundle version.
683+
return `${cliVersion}-${bundleVersion}`;
684+
}
685+
659686
/**
660687
* Obtains the CodeQL bundle, installs it in the toolcache if appropriate, and extracts it.
661688
*

0 commit comments

Comments
 (0)
Please sign in to comment.