Merge pull request #1510 from github/henrymercer/fix-fallback-version-number

Fix computation of fallback version number
This commit is contained in:
Henry Mercer 2023-01-26 14:17:40 +00:00 committed by GitHub
commit 43f1a6c701
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 110 additions and 115 deletions

70
lib/codeql.test.js generated
View file

@ -216,66 +216,50 @@ for (const { cliVersion, expectedToolcacheVersion, } of EXPLICITLY_REQUESTED_BUN
}); });
}); });
} }
for (const { isCached, tagName, toolcacheCliVersion } of [ for (const { githubReleases, toolcacheVersion } of [
// Test that we use the tools from the toolcache when `SAMPLE_DEFAULT_CLI_VERSION` is requested
// and `SAMPLE_DEFAULT_CLI_VERSION-` is in the toolcache.
{ {
isCached: true, toolcacheVersion: SAMPLE_DEFAULT_CLI_VERSION.cliVersion,
tagName: "codeql-bundle-20230101",
toolcacheCliVersion: SAMPLE_DEFAULT_CLI_VERSION.cliVersion,
}, },
{ {
isCached: true, githubReleases: {
// By leaving toolcacheCliVersion undefined, the bundle will be installed "codeql-bundle-20230101": `cli-version-${SAMPLE_DEFAULT_CLI_VERSION.cliVersion}.txt`,
// 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 toolcacheVersion: "0.0.0-20230101",
// `x.y.z` version isn't in the toolcache.
tagName: `codeql-bundle-${SAMPLE_DEFAULT_CLI_VERSION.cliVersion}-20230101`,
}, },
{ {
isCached: false, toolcacheVersion: `${SAMPLE_DEFAULT_CLI_VERSION.cliVersion}-20230101`,
tagName: "codeql-bundle-20230101",
}, },
]) { ]) {
(0, ava_1.default)(`uses default version on Dotcom when default version bundle ${tagName} is ${isCached ? "" : "not "}cached`, async (t) => { (0, ava_1.default)(`uses tools from toolcache when ${SAMPLE_DEFAULT_CLI_VERSION.cliVersion} is requested and ` +
`${toolcacheVersion} is installed`, async (t) => {
await util.withTmpDir(async (tmpDir) => { await util.withTmpDir(async (tmpDir) => {
(0, testing_utils_1.setupActionsVars)(tmpDir, tmpDir); (0, testing_utils_1.setupActionsVars)(tmpDir, tmpDir);
if (isCached) { sinon
await installIntoToolcache({ .stub(toolcache, "find")
cliVersion: toolcacheCliVersion, .withArgs("CodeQL", toolcacheVersion)
tagName, .returns("path/to/cached/codeql");
isPinned: true, sinon.stub(toolcache, "findAllVersions").returns([toolcacheVersion]);
tmpDir, if (githubReleases) {
});
}
else {
mockDownloadApi({
tagName,
});
sinon.stub(api, "getApiClient").value(() => ({ sinon.stub(api, "getApiClient").value(() => ({
repos: { repos: {
listReleases: sinon.stub().resolves(undefined), listReleases: sinon.stub().resolves(undefined),
}, },
paginate: sinon.stub().resolves([ paginate: sinon.stub().resolves(Object.entries(githubReleases).map(([releaseTagName, cliVersionMarkerFile]) => ({
{ assets: [
assets: [ {
{ name: cliVersionMarkerFile,
name: "cli-version-2.0.0.txt", },
}, ],
], tag_name: releaseTagName,
tag_name: tagName, }))),
},
]),
})); }));
} }
const result = await codeql.setupCodeQL(undefined, sampleApiDetails, tmpDir, util.GitHubVariant.DOTCOM, false, SAMPLE_DEFAULT_CLI_VERSION, (0, logging_1.getRunnerLogger)(true), false); const result = await codeql.setupCodeQL(undefined, sampleApiDetails, tmpDir, util.GitHubVariant.DOTCOM, false, SAMPLE_DEFAULT_CLI_VERSION, (0, logging_1.getRunnerLogger)(true), false);
t.is(result.toolsVersion, SAMPLE_DEFAULT_CLI_VERSION.cliVersion); t.is(result.toolsVersion, SAMPLE_DEFAULT_CLI_VERSION.cliVersion);
if (isCached) { t.is(result.toolsSource, init_1.ToolsSource.Toolcache);
t.is(result.toolsSource, init_1.ToolsSource.Toolcache); t.is(result.toolsDownloadDurationMs, undefined);
t.is(result.toolsDownloadDurationMs, undefined);
}
else {
t.is(result.toolsSource, init_1.ToolsSource.Download);
t.is(typeof result.toolsDownloadDurationMs, "number");
}
}); });
}); });
} }

File diff suppressed because one or more lines are too long

15
lib/setup-codeql.js generated
View file

@ -211,12 +211,19 @@ async function getCodeQLBundleDownloadURL(tagName, apiDetails, variant, logger)
} }
return `https://github.com/${exports.CODEQL_DEFAULT_ACTION_REPOSITORY}/releases/download/${tagName}/${codeQLBundleName}`; return `https://github.com/${exports.CODEQL_DEFAULT_ACTION_REPOSITORY}/releases/download/${tagName}/${codeQLBundleName}`;
} }
function getBundleVersionFromTagName(tagName) {
const match = tagName.match(/^codeql-bundle-(.*)$/);
if (match === null || match.length < 2) {
throw new Error(`Malformed bundle tag name: ${tagName}. Bundle version could not be inferred`);
}
return match[1];
}
function getBundleVersionFromUrl(url) { function getBundleVersionFromUrl(url) {
const match = url.match(/\/codeql-bundle-(.*)\//); const match = url.match(/\/(codeql-bundle-.*)\//);
if (match === null || match.length < 2) { if (match === null || match.length < 2) {
throw new Error(`Malformed tools url: ${url}. Bundle version could not be inferred`); throw new Error(`Malformed tools url: ${url}. Bundle version could not be inferred`);
} }
return match[1]; return getBundleVersionFromTagName(match[1]);
} }
exports.getBundleVersionFromUrl = getBundleVersionFromUrl; exports.getBundleVersionFromUrl = getBundleVersionFromUrl;
function convertToSemVer(version, logger) { function convertToSemVer(version, logger) {
@ -349,14 +356,14 @@ async function getCodeQLSource(toolsInput, bypassToolcache, defaultCliVersion, a
} }
} }
if (!codeqlFolder && requestedVersion.cliVersion) { if (!codeqlFolder && requestedVersion.cliVersion) {
// Fall back to accepting a `0.0.0-<tagName>` version if we didn't find the // Fall back to accepting a `0.0.0-<bundleVersion>` version if we didn't find the
// `x.y.z` version. This is to support old versions of the toolcache. // `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 // 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. // to find the tag name for the requested version.
tagName = tagName =
tagName || (await getOrFindBundleTagName(requestedVersion, logger)); tagName || (await getOrFindBundleTagName(requestedVersion, logger));
const fallbackVersion = convertToSemVer(tagName, logger); const fallbackVersion = convertToSemVer(getBundleVersionFromTagName(tagName), logger);
logger.debug(`Computed a fallback toolcache version number of ${fallbackVersion} for CodeQL tools version ` + logger.debug(`Computed a fallback toolcache version number of ${fallbackVersion} for CodeQL tools version ` +
`${requestedVersion.cliVersion}.`); `${requestedVersion.cliVersion}.`);
codeqlFolder = toolcache.find("CodeQL", fallbackVersion); codeqlFolder = toolcache.find("CodeQL", fallbackVersion);

File diff suppressed because one or more lines are too long

View file

@ -298,80 +298,71 @@ for (const {
}); });
} }
for (const { isCached, tagName, toolcacheCliVersion } of [ for (const { githubReleases, toolcacheVersion } of [
// Test that we use the tools from the toolcache when `SAMPLE_DEFAULT_CLI_VERSION` is requested
// and `SAMPLE_DEFAULT_CLI_VERSION-` is in the toolcache.
{ {
isCached: true, toolcacheVersion: SAMPLE_DEFAULT_CLI_VERSION.cliVersion,
tagName: "codeql-bundle-20230101",
toolcacheCliVersion: SAMPLE_DEFAULT_CLI_VERSION.cliVersion,
}, },
{ {
isCached: true, githubReleases: {
// By leaving toolcacheCliVersion undefined, the bundle will be installed "codeql-bundle-20230101": `cli-version-${SAMPLE_DEFAULT_CLI_VERSION.cliVersion}.txt`,
// 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 toolcacheVersion: "0.0.0-20230101",
// `x.y.z` version isn't in the toolcache.
tagName: `codeql-bundle-${SAMPLE_DEFAULT_CLI_VERSION.cliVersion}-20230101`,
}, },
{ {
isCached: false, toolcacheVersion: `${SAMPLE_DEFAULT_CLI_VERSION.cliVersion}-20230101`,
tagName: "codeql-bundle-20230101",
}, },
]) { ]) {
test(`uses default version on Dotcom when default version bundle ${tagName} is ${ test(
isCached ? "" : "not " `uses tools from toolcache when ${SAMPLE_DEFAULT_CLI_VERSION.cliVersion} is requested and ` +
}cached`, async (t) => { `${toolcacheVersion} is installed`,
await util.withTmpDir(async (tmpDir) => { async (t) => {
setupActionsVars(tmpDir, tmpDir); await util.withTmpDir(async (tmpDir) => {
setupActionsVars(tmpDir, tmpDir);
if (isCached) { sinon
await installIntoToolcache({ .stub(toolcache, "find")
cliVersion: toolcacheCliVersion, .withArgs("CodeQL", toolcacheVersion)
tagName, .returns("path/to/cached/codeql");
isPinned: true, sinon.stub(toolcache, "findAllVersions").returns([toolcacheVersion]);
tmpDir,
}); if (githubReleases) {
} else { sinon.stub(api, "getApiClient").value(() => ({
mockDownloadApi({ repos: {
tagName, listReleases: sinon.stub().resolves(undefined),
});
sinon.stub(api, "getApiClient").value(() => ({
repos: {
listReleases: sinon.stub().resolves(undefined),
},
paginate: sinon.stub().resolves([
{
assets: [
{
name: "cli-version-2.0.0.txt",
},
],
tag_name: tagName,
}, },
]), paginate: sinon.stub().resolves(
})); Object.entries(githubReleases).map(
} ([releaseTagName, cliVersionMarkerFile]) => ({
assets: [
{
name: cliVersionMarkerFile,
},
],
tag_name: releaseTagName,
})
)
),
}));
}
const result = await codeql.setupCodeQL( const result = await codeql.setupCodeQL(
undefined, undefined,
sampleApiDetails, sampleApiDetails,
tmpDir, tmpDir,
util.GitHubVariant.DOTCOM, util.GitHubVariant.DOTCOM,
false, false,
SAMPLE_DEFAULT_CLI_VERSION, SAMPLE_DEFAULT_CLI_VERSION,
getRunnerLogger(true), getRunnerLogger(true),
false false
); );
t.is(result.toolsVersion, SAMPLE_DEFAULT_CLI_VERSION.cliVersion); t.is(result.toolsVersion, SAMPLE_DEFAULT_CLI_VERSION.cliVersion);
if (isCached) {
t.is(result.toolsSource, ToolsSource.Toolcache); t.is(result.toolsSource, ToolsSource.Toolcache);
t.is(result.toolsDownloadDurationMs, undefined); t.is(result.toolsDownloadDurationMs, undefined);
} else { });
t.is(result.toolsSource, ToolsSource.Download); }
t.is(typeof result.toolsDownloadDurationMs, "number"); );
}
});
});
} }
for (const variant of [util.GitHubVariant.GHAE, util.GitHubVariant.GHES]) { for (const variant of [util.GitHubVariant.GHAE, util.GitHubVariant.GHES]) {

View file

@ -241,14 +241,24 @@ async function getCodeQLBundleDownloadURL(
return `https://github.com/${CODEQL_DEFAULT_ACTION_REPOSITORY}/releases/download/${tagName}/${codeQLBundleName}`; return `https://github.com/${CODEQL_DEFAULT_ACTION_REPOSITORY}/releases/download/${tagName}/${codeQLBundleName}`;
} }
function getBundleVersionFromTagName(tagName: string): string {
const match = tagName.match(/^codeql-bundle-(.*)$/);
if (match === null || match.length < 2) {
throw new Error(
`Malformed bundle tag name: ${tagName}. Bundle version could not be inferred`
);
}
return match[1];
}
export function getBundleVersionFromUrl(url: string): string { export function getBundleVersionFromUrl(url: string): string {
const match = url.match(/\/codeql-bundle-(.*)\//); const match = url.match(/\/(codeql-bundle-.*)\//);
if (match === null || match.length < 2) { if (match === null || match.length < 2) {
throw new Error( throw new Error(
`Malformed tools url: ${url}. Bundle version could not be inferred` `Malformed tools url: ${url}. Bundle version could not be inferred`
); );
} }
return match[1]; return getBundleVersionFromTagName(match[1]);
} }
export function convertToSemVer(version: string, logger: Logger): string { export function convertToSemVer(version: string, logger: Logger): string {
@ -448,14 +458,17 @@ export async function getCodeQLSource(
} }
if (!codeqlFolder && requestedVersion.cliVersion) { if (!codeqlFolder && requestedVersion.cliVersion) {
// Fall back to accepting a `0.0.0-<tagName>` version if we didn't find the // Fall back to accepting a `0.0.0-<bundleVersion>` version if we didn't find the
// `x.y.z` version. This is to support old versions of the toolcache. // `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 // 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. // to find the tag name for the requested version.
tagName = tagName =
tagName || (await getOrFindBundleTagName(requestedVersion, logger)); tagName || (await getOrFindBundleTagName(requestedVersion, logger));
const fallbackVersion = convertToSemVer(tagName, logger); const fallbackVersion = convertToSemVer(
getBundleVersionFromTagName(tagName),
logger
);
logger.debug( logger.debug(
`Computed a fallback toolcache version number of ${fallbackVersion} for CodeQL tools version ` + `Computed a fallback toolcache version number of ${fallbackVersion} for CodeQL tools version ` +
`${requestedVersion.cliVersion}.` `${requestedVersion.cliVersion}.`