Skip to content

Commit 8a4abfd

Browse files
committed
Support cli-version-x.y.z-pre.txt marker files
1 parent 5f1362d commit 8a4abfd

6 files changed

Lines changed: 102 additions & 84 deletions

File tree

lib/codeql.test.js

Lines changed: 28 additions & 16 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: 13 additions & 17 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: 42 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -239,36 +239,52 @@ test("downloads an explicitly requested bundle even if a different version is ca
239239
});
240240
});
241241

242-
test("tries to cache an explicitly requested bundle with its CLI version number", async (t) => {
243-
await util.withTmpDir(async (tmpDir) => {
244-
setupActionsVars(tmpDir, tmpDir);
242+
const EXPLICITLY_REQUESTED_BUNDLE_TEST_CASES = [
243+
{
244+
cliVersion: "2.10.0",
245+
expectedToolcacheVersion: "2.10.0-20200610",
246+
},
247+
{
248+
cliVersion: "2.10.0-pre",
249+
expectedToolcacheVersion: "0.0.0-20200610",
250+
},
251+
];
245252

246-
mockApiDetails(sampleApiDetails);
247-
sinon.stub(actionsUtil, "isRunningLocalAction").returns(true);
253+
for (const {
254+
cliVersion,
255+
expectedToolcacheVersion,
256+
} of EXPLICITLY_REQUESTED_BUNDLE_TEST_CASES) {
257+
test(`caches an explicitly requested bundle containing CLI ${cliVersion} as ${expectedToolcacheVersion}`, async (t) => {
258+
await util.withTmpDir(async (tmpDir) => {
259+
setupActionsVars(tmpDir, tmpDir);
248260

249-
const releaseApiMock = mockReleaseApi({
250-
assetNames: ["cli-version-2.10.0.txt"],
251-
tagName: "codeql-bundle-20200610",
252-
});
253-
const url = mockDownloadApi({
254-
tagName: "codeql-bundle-20200610",
255-
});
261+
mockApiDetails(sampleApiDetails);
262+
sinon.stub(actionsUtil, "isRunningLocalAction").returns(true);
256263

257-
const result = await codeql.setupCodeQL(
258-
url,
259-
sampleApiDetails,
260-
tmpDir,
261-
util.GitHubVariant.DOTCOM,
262-
false,
263-
SAMPLE_DEFAULT_CLI_VERSION,
264-
getRunnerLogger(true),
265-
false
266-
);
267-
t.assert(releaseApiMock.isDone(), "Releases API should have been called");
268-
t.assert(toolcache.find("CodeQL", "2.10.0-20200610"));
269-
t.deepEqual(result.toolsVersion, "2.10.0");
264+
const releaseApiMock = mockReleaseApi({
265+
assetNames: [`cli-version-${cliVersion}.txt`],
266+
tagName: "codeql-bundle-20200610",
267+
});
268+
const url = mockDownloadApi({
269+
tagName: "codeql-bundle-20200610",
270+
});
271+
272+
const result = await codeql.setupCodeQL(
273+
url,
274+
sampleApiDetails,
275+
tmpDir,
276+
util.GitHubVariant.DOTCOM,
277+
false,
278+
SAMPLE_DEFAULT_CLI_VERSION,
279+
getRunnerLogger(true),
280+
false
281+
);
282+
t.assert(releaseApiMock.isDone(), "Releases API should have been called");
283+
t.assert(toolcache.find("CodeQL", expectedToolcacheVersion));
284+
t.deepEqual(result.toolsVersion, cliVersion);
285+
});
270286
});
271-
});
287+
}
272288

273289
for (const { isCached, tagName, toolcacheCliVersion } of [
274290
{

src/setup-codeql.ts

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -76,28 +76,16 @@ async function getCodeQLBundleReleasesDotcomOnly(
7676
});
7777
logger.debug(`Found ${releases.length} releases.`);
7878

79-
return releases.flatMap((release) => {
80-
const cliVersionFileVersions = release.assets
81-
.map((asset) => asset.name.match(/cli-version-(.*)\.txt/)?.[1])
82-
.filter((v) => v)
83-
.map((v) => v as string);
84-
85-
if (cliVersionFileVersions.length > 1) {
86-
logger.warning(
87-
`Ignoring release ${release.tag_name} with multiple CLI version marker files.`
88-
);
89-
return [];
90-
}
91-
return [
92-
{ cliVersion: cliVersionFileVersions[0], tagName: release.tag_name },
93-
];
94-
});
79+
return releases.map((release) => ({
80+
cliVersion: tryGetCodeQLCliVersionForRelease(release, logger),
81+
tagName: release.tag_name,
82+
}));
9583
}
9684

97-
async function tryGetCodeQLCliVersionForRelease(
85+
function tryGetCodeQLCliVersionForRelease(
9886
release,
9987
logger: Logger
100-
): Promise<string | undefined> {
88+
): string | undefined {
10189
const cliVersionsFromMarkerFiles = release.assets
10290
.map((asset) => asset.name.match(/cli-version-(.*)\.txt/)?.[1])
10391
.filter((v) => v)
@@ -573,11 +561,17 @@ export async function downloadCodeQL(
573561
logger
574562
))) ||
575563
undefined;
576-
// Include the bundle version in the toolcache version number so that if the user requests the
577-
// same URL again, we can get it from the cache without having to call any of the Releases API.
578-
const toolcacheVersion = cliVersion
579-
? `${cliVersion}-${bundleVersion}`
580-
: convertToSemVer(bundleVersion, logger);
564+
// Include both the CLI version and the bundle version in the toolcache version number. That way
565+
// if the user requests the same URL again, we can get it from the cache without having to call
566+
// any of the Releases API.
567+
//
568+
// Special case: If the CLI version is a pre-release, then cache the bundle as
569+
// `0.0.0-<bundleVersion>` to avoid the bundle being interpreted as containing a stable CLI
570+
// release.
571+
const toolcacheVersion =
572+
cliVersion && !cliVersion.includes("-")
573+
? `${cliVersion}-${bundleVersion}`
574+
: convertToSemVer(bundleVersion, logger);
581575
return {
582576
toolsVersion: cliVersion || toolcacheVersion,
583577
codeqlFolder: await toolcache.cacheDir(

0 commit comments

Comments
 (0)