Allow using a x.y.z-yyyymmdd toolcache version for CLI x.y.z.

This commit is contained in:
Henry Mercer 2023-01-12 15:39:26 +00:00
parent e8c12e1f7d
commit 648838c4a0
6 changed files with 100 additions and 17 deletions

25
lib/codeql.test.js generated
View file

@ -147,14 +147,31 @@ async function installIntoToolcache({ apiDetails = sampleApiDetails, cliVersion,
t.deepEqual(result.toolsVersion, "0.0.0-20200610");
});
});
for (const isCached of [true, false]) {
(0, ava_1.default)(`uses default version on Dotcom when default version bundle is ${isCached ? "" : "not "}cached`, async (t) => {
for (const { isCached, tagName, toolcacheCliVersion } of [
{
isCached: true,
tagName: "codeql-bundle-20230101",
toolcacheCliVersion: SAMPLE_DEFAULT_CLI_VERSION.cliVersion,
},
{
isCached: true,
// By leaving toolcacheCliVersion undefined, the bundle will be installed
// into the toolcache as `${SAMPLE_DEFAULT_CLI_VERSION.cliVersion}-20230101`.
// This lets us test that `x.y.z-yyyymmdd` toolcache versions are used if an
// `x.y.z` version isn't in the toolcache.
tagName: `codeql-bundle-${SAMPLE_DEFAULT_CLI_VERSION.cliVersion}-20230101`,
},
{
isCached: false,
tagName: "codeql-bundle-20230101",
},
]) {
(0, ava_1.default)(`uses default version on Dotcom when default version bundle ${tagName} is ${isCached ? "" : "not "}cached`, async (t) => {
await util.withTmpDir(async (tmpDir) => {
(0, testing_utils_1.setupActionsVars)(tmpDir, tmpDir);
const tagName = `codeql-bundle-20230101`;
if (isCached) {
await installIntoToolcache({
cliVersion: SAMPLE_DEFAULT_CLI_VERSION.cliVersion,
cliVersion: toolcacheCliVersion,
tagName,
isPinned: true,
tmpDir,

File diff suppressed because one or more lines are too long

26
lib/setup-codeql.js generated
View file

@ -250,8 +250,8 @@ async function getCodeQLSource(toolsInput, bypassToolcache, defaultCliVersion, a
* We include a `variant` property to let us verify using the type system that
* `tagName` is only undefined when the variant is Dotcom. This lets us ensure
* that we can always compute `tagName`, either by using the existing tag name
* on enterprise instances, or safely calling `findCodeQLBundleTagDotcomOnly`
* on Dotcom.
* on enterprise instances, or calling `findCodeQLBundleTagDotcomOnly` on
* Dotcom.
*/
const requestedVersion = forceLatest
? // case 1
@ -273,13 +273,33 @@ async function getCodeQLSource(toolsInput, bypassToolcache, defaultCliVersion, a
// If we find the specified version, we always use that.
let codeqlFolder = toolcache.find("CodeQL", requestedVersion.cliVersion);
let tagName = requestedVersion["tagName"];
if (!codeqlFolder) {
logger.debug("Didn't find a version of the CodeQL tools in the toolcache with a version number " +
`exactly matching ${requestedVersion.cliVersion}.`);
const allVersions = toolcache.findAllVersions("CodeQL");
logger.debug(`Found the following versions of the CodeQL tools in the toolcache: ${JSON.stringify(allVersions)}.`);
// If there is exactly one version of the CodeQL tools in the toolcache, and that version is
// the form `x.y.z-<tagName>`, then use it.
const candidateVersions = allVersions.filter((version) => version.startsWith(`${requestedVersion.cliVersion}-`));
if (candidateVersions.length === 1) {
logger.debug("Exactly one candidate version found, using that.");
codeqlFolder = toolcache.find("CodeQL", candidateVersions[0]);
}
else {
logger.debug("Did not find exactly one version of the CodeQL tools starting with the requested version.");
}
}
if (!codeqlFolder && !requestedVersion.cliVersion.startsWith("0.0.0")) {
// Fall back to accepting a `0.0.0-<tagName>` version if we didn't find the
// `x.y.z` version. This is to support old versions of the toolcache.
//
// If we are on Dotcom, we will make an HTTP request to the Releases API here
// to find the tag name for the requested version.
tagName =
tagName || (await getOrFindBundleTagName(requestedVersion, logger));
const fallbackVersion = convertToSemVer(tagName, logger);
logger.debug(`Computed a fallback toolcache version number of ${fallbackVersion} for CodeQL tools version ${requestedVersion.cliVersion}.`);
logger.debug(`Computed a fallback toolcache version number of ${fallbackVersion} for CodeQL tools version ` +
`${requestedVersion.cliVersion}.`);
codeqlFolder = toolcache.find("CodeQL", fallbackVersion);
}
if (codeqlFolder) {

File diff suppressed because one or more lines are too long

View file

@ -204,18 +204,34 @@ test("downloads an explicitly requested bundle even if a different version is ca
});
});
for (const isCached of [true, false]) {
test(`uses default version on Dotcom when default version bundle is ${
for (const { isCached, tagName, toolcacheCliVersion } of [
{
isCached: true,
tagName: "codeql-bundle-20230101",
toolcacheCliVersion: SAMPLE_DEFAULT_CLI_VERSION.cliVersion,
},
{
isCached: true,
// By leaving toolcacheCliVersion undefined, the bundle will be installed
// into the toolcache as `${SAMPLE_DEFAULT_CLI_VERSION.cliVersion}-20230101`.
// This lets us test that `x.y.z-yyyymmdd` toolcache versions are used if an
// `x.y.z` version isn't in the toolcache.
tagName: `codeql-bundle-${SAMPLE_DEFAULT_CLI_VERSION.cliVersion}-20230101`,
},
{
isCached: false,
tagName: "codeql-bundle-20230101",
},
]) {
test(`uses default version on Dotcom when default version bundle ${tagName} is ${
isCached ? "" : "not "
}cached`, async (t) => {
await util.withTmpDir(async (tmpDir) => {
setupActionsVars(tmpDir, tmpDir);
const tagName = `codeql-bundle-20230101`;
if (isCached) {
await installIntoToolcache({
cliVersion: SAMPLE_DEFAULT_CLI_VERSION.cliVersion,
cliVersion: toolcacheCliVersion,
tagName,
isPinned: true,
tmpDir,

View file

@ -311,8 +311,8 @@ export async function getCodeQLSource(
* We include a `variant` property to let us verify using the type system that
* `tagName` is only undefined when the variant is Dotcom. This lets us ensure
* that we can always compute `tagName`, either by using the existing tag name
* on enterprise instances, or safely calling `findCodeQLBundleTagDotcomOnly`
* on Dotcom.
* on enterprise instances, or calling `findCodeQLBundleTagDotcomOnly` on
* Dotcom.
*/
const requestedVersion = forceLatest
? // case 1
@ -339,14 +339,44 @@ export async function getCodeQLSource(
let codeqlFolder = toolcache.find("CodeQL", requestedVersion.cliVersion);
let tagName: string | undefined = requestedVersion["tagName"];
if (!codeqlFolder) {
logger.debug(
"Didn't find a version of the CodeQL tools in the toolcache with a version number " +
`exactly matching ${requestedVersion.cliVersion}.`
);
const allVersions = toolcache.findAllVersions("CodeQL");
logger.debug(
`Found the following versions of the CodeQL tools in the toolcache: ${JSON.stringify(
allVersions
)}.`
);
// If there is exactly one version of the CodeQL tools in the toolcache, and that version is
// the form `x.y.z-<tagName>`, then use it.
const candidateVersions = allVersions.filter((version) =>
version.startsWith(`${requestedVersion.cliVersion}-`)
);
if (candidateVersions.length === 1) {
logger.debug("Exactly one candidate version found, using that.");
codeqlFolder = toolcache.find("CodeQL", candidateVersions[0]);
} else {
logger.debug(
"Did not find exactly one version of the CodeQL tools starting with the requested version."
);
}
}
if (!codeqlFolder && !requestedVersion.cliVersion.startsWith("0.0.0")) {
// Fall back to accepting a `0.0.0-<tagName>` version if we didn't find the
// `x.y.z` version. This is to support old versions of the toolcache.
//
// If we are on Dotcom, we will make an HTTP request to the Releases API here
// to find the tag name for the requested version.
tagName =
tagName || (await getOrFindBundleTagName(requestedVersion, logger));
const fallbackVersion = convertToSemVer(tagName, logger);
logger.debug(
`Computed a fallback toolcache version number of ${fallbackVersion} for CodeQL tools version ${requestedVersion.cliVersion}.`
`Computed a fallback toolcache version number of ${fallbackVersion} for CodeQL tools version ` +
`${requestedVersion.cliVersion}.`
);
codeqlFolder = toolcache.find("CodeQL", fallbackVersion);
}