Skip to content

Commit 43f1a6c

Browse files
authored
Merge pull request #1510 from github/henrymercer/fix-fallback-version-number
Fix computation of fallback version number
2 parents 24ca6b0 + 75ae065 commit 43f1a6c

File tree

6 files changed

+112
-117
lines changed

6 files changed

+112
-117
lines changed

lib/codeql.test.js

Lines changed: 27 additions & 43 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/codeql.test.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/setup-codeql.js

Lines changed: 11 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/setup-codeql.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/codeql.test.ts

Lines changed: 55 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -298,80 +298,71 @@ for (const {
298298
});
299299
}
300300

301-
for (const { isCached, tagName, toolcacheCliVersion } of [
301+
for (const { githubReleases, toolcacheVersion } of [
302+
// Test that we use the tools from the toolcache when `SAMPLE_DEFAULT_CLI_VERSION` is requested
303+
// and `SAMPLE_DEFAULT_CLI_VERSION-` is in the toolcache.
302304
{
303-
isCached: true,
304-
tagName: "codeql-bundle-20230101",
305-
toolcacheCliVersion: SAMPLE_DEFAULT_CLI_VERSION.cliVersion,
305+
toolcacheVersion: SAMPLE_DEFAULT_CLI_VERSION.cliVersion,
306306
},
307307
{
308-
isCached: true,
309-
// By leaving toolcacheCliVersion undefined, the bundle will be installed
310-
// into the toolcache as `${SAMPLE_DEFAULT_CLI_VERSION.cliVersion}-20230101`.
311-
// This lets us test that `x.y.z-yyyymmdd` toolcache versions are used if an
312-
// `x.y.z` version isn't in the toolcache.
313-
tagName: `codeql-bundle-${SAMPLE_DEFAULT_CLI_VERSION.cliVersion}-20230101`,
308+
githubReleases: {
309+
"codeql-bundle-20230101": `cli-version-${SAMPLE_DEFAULT_CLI_VERSION.cliVersion}.txt`,
310+
},
311+
toolcacheVersion: "0.0.0-20230101",
314312
},
315313
{
316-
isCached: false,
317-
tagName: "codeql-bundle-20230101",
314+
toolcacheVersion: `${SAMPLE_DEFAULT_CLI_VERSION.cliVersion}-20230101`,
318315
},
319316
]) {
320-
test(`uses default version on Dotcom when default version bundle ${tagName} is ${
321-
isCached ? "" : "not "
322-
}cached`, async (t) => {
323-
await util.withTmpDir(async (tmpDir) => {
324-
setupActionsVars(tmpDir, tmpDir);
325-
326-
if (isCached) {
327-
await installIntoToolcache({
328-
cliVersion: toolcacheCliVersion,
329-
tagName,
330-
isPinned: true,
331-
tmpDir,
332-
});
333-
} else {
334-
mockDownloadApi({
335-
tagName,
336-
});
337-
sinon.stub(api, "getApiClient").value(() => ({
338-
repos: {
339-
listReleases: sinon.stub().resolves(undefined),
340-
},
341-
paginate: sinon.stub().resolves([
342-
{
343-
assets: [
344-
{
345-
name: "cli-version-2.0.0.txt",
346-
},
347-
],
348-
tag_name: tagName,
317+
test(
318+
`uses tools from toolcache when ${SAMPLE_DEFAULT_CLI_VERSION.cliVersion} is requested and ` +
319+
`${toolcacheVersion} is installed`,
320+
async (t) => {
321+
await util.withTmpDir(async (tmpDir) => {
322+
setupActionsVars(tmpDir, tmpDir);
323+
324+
sinon
325+
.stub(toolcache, "find")
326+
.withArgs("CodeQL", toolcacheVersion)
327+
.returns("path/to/cached/codeql");
328+
sinon.stub(toolcache, "findAllVersions").returns([toolcacheVersion]);
329+
330+
if (githubReleases) {
331+
sinon.stub(api, "getApiClient").value(() => ({
332+
repos: {
333+
listReleases: sinon.stub().resolves(undefined),
349334
},
350-
]),
351-
}));
352-
}
353-
354-
const result = await codeql.setupCodeQL(
355-
undefined,
356-
sampleApiDetails,
357-
tmpDir,
358-
util.GitHubVariant.DOTCOM,
359-
false,
360-
SAMPLE_DEFAULT_CLI_VERSION,
361-
getRunnerLogger(true),
362-
false
363-
);
364-
t.is(result.toolsVersion, SAMPLE_DEFAULT_CLI_VERSION.cliVersion);
365-
366-
if (isCached) {
335+
paginate: sinon.stub().resolves(
336+
Object.entries(githubReleases).map(
337+
([releaseTagName, cliVersionMarkerFile]) => ({
338+
assets: [
339+
{
340+
name: cliVersionMarkerFile,
341+
},
342+
],
343+
tag_name: releaseTagName,
344+
})
345+
)
346+
),
347+
}));
348+
}
349+
350+
const result = await codeql.setupCodeQL(
351+
undefined,
352+
sampleApiDetails,
353+
tmpDir,
354+
util.GitHubVariant.DOTCOM,
355+
false,
356+
SAMPLE_DEFAULT_CLI_VERSION,
357+
getRunnerLogger(true),
358+
false
359+
);
360+
t.is(result.toolsVersion, SAMPLE_DEFAULT_CLI_VERSION.cliVersion);
367361
t.is(result.toolsSource, ToolsSource.Toolcache);
368362
t.is(result.toolsDownloadDurationMs, undefined);
369-
} else {
370-
t.is(result.toolsSource, ToolsSource.Download);
371-
t.is(typeof result.toolsDownloadDurationMs, "number");
372-
}
373-
});
374-
});
363+
});
364+
}
365+
);
375366
}
376367

377368
for (const variant of [util.GitHubVariant.GHAE, util.GitHubVariant.GHES]) {

src/setup-codeql.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -241,14 +241,24 @@ async function getCodeQLBundleDownloadURL(
241241
return `https://github.com/${CODEQL_DEFAULT_ACTION_REPOSITORY}/releases/download/${tagName}/${codeQLBundleName}`;
242242
}
243243

244+
function getBundleVersionFromTagName(tagName: string): string {
245+
const match = tagName.match(/^codeql-bundle-(.*)$/);
246+
if (match === null || match.length < 2) {
247+
throw new Error(
248+
`Malformed bundle tag name: ${tagName}. Bundle version could not be inferred`
249+
);
250+
}
251+
return match[1];
252+
}
253+
244254
export function getBundleVersionFromUrl(url: string): string {
245-
const match = url.match(/\/codeql-bundle-(.*)\//);
255+
const match = url.match(/\/(codeql-bundle-.*)\//);
246256
if (match === null || match.length < 2) {
247257
throw new Error(
248258
`Malformed tools url: ${url}. Bundle version could not be inferred`
249259
);
250260
}
251-
return match[1];
261+
return getBundleVersionFromTagName(match[1]);
252262
}
253263

254264
export function convertToSemVer(version: string, logger: Logger): string {
@@ -448,14 +458,17 @@ export async function getCodeQLSource(
448458
}
449459

450460
if (!codeqlFolder && requestedVersion.cliVersion) {
451-
// Fall back to accepting a `0.0.0-<tagName>` version if we didn't find the
461+
// Fall back to accepting a `0.0.0-<bundleVersion>` version if we didn't find the
452462
// `x.y.z` version. This is to support old versions of the toolcache.
453463
//
454464
// If we are on Dotcom, we will make an HTTP request to the Releases API here
455465
// to find the tag name for the requested version.
456466
tagName =
457467
tagName || (await getOrFindBundleTagName(requestedVersion, logger));
458-
const fallbackVersion = convertToSemVer(tagName, logger);
468+
const fallbackVersion = convertToSemVer(
469+
getBundleVersionFromTagName(tagName),
470+
logger
471+
);
459472
logger.debug(
460473
`Computed a fallback toolcache version number of ${fallbackVersion} for CodeQL tools version ` +
461474
`${requestedVersion.cliVersion}.`

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy