Skip to content

Commit

Permalink
Support cli-version-x.y.z-pre.txt marker files
Browse files Browse the repository at this point in the history
  • Loading branch information
henrymercer committed Jan 18, 2023
1 parent 5f1362d commit 8a4abfd
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 84 deletions.
44 changes: 28 additions & 16 deletions lib/codeql.test.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/codeql.test.js.map

Large diffs are not rendered by default.

30 changes: 13 additions & 17 deletions lib/setup-codeql.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/setup-codeql.js.map

Large diffs are not rendered by default.

68 changes: 42 additions & 26 deletions src/codeql.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,36 +239,52 @@ test("downloads an explicitly requested bundle even if a different version is ca
});
});

test("tries to cache an explicitly requested bundle with its CLI version number", async (t) => {
await util.withTmpDir(async (tmpDir) => {
setupActionsVars(tmpDir, tmpDir);
const EXPLICITLY_REQUESTED_BUNDLE_TEST_CASES = [
{
cliVersion: "2.10.0",
expectedToolcacheVersion: "2.10.0-20200610",
},
{
cliVersion: "2.10.0-pre",
expectedToolcacheVersion: "0.0.0-20200610",
},
];

mockApiDetails(sampleApiDetails);
sinon.stub(actionsUtil, "isRunningLocalAction").returns(true);
for (const {
cliVersion,
expectedToolcacheVersion,
} of EXPLICITLY_REQUESTED_BUNDLE_TEST_CASES) {
test(`caches an explicitly requested bundle containing CLI ${cliVersion} as ${expectedToolcacheVersion}`, async (t) => {
await util.withTmpDir(async (tmpDir) => {
setupActionsVars(tmpDir, tmpDir);

const releaseApiMock = mockReleaseApi({
assetNames: ["cli-version-2.10.0.txt"],
tagName: "codeql-bundle-20200610",
});
const url = mockDownloadApi({
tagName: "codeql-bundle-20200610",
});
mockApiDetails(sampleApiDetails);
sinon.stub(actionsUtil, "isRunningLocalAction").returns(true);

const result = await codeql.setupCodeQL(
url,
sampleApiDetails,
tmpDir,
util.GitHubVariant.DOTCOM,
false,
SAMPLE_DEFAULT_CLI_VERSION,
getRunnerLogger(true),
false
);
t.assert(releaseApiMock.isDone(), "Releases API should have been called");
t.assert(toolcache.find("CodeQL", "2.10.0-20200610"));
t.deepEqual(result.toolsVersion, "2.10.0");
const releaseApiMock = mockReleaseApi({
assetNames: [`cli-version-${cliVersion}.txt`],
tagName: "codeql-bundle-20200610",
});
const url = mockDownloadApi({
tagName: "codeql-bundle-20200610",
});

const result = await codeql.setupCodeQL(
url,
sampleApiDetails,
tmpDir,
util.GitHubVariant.DOTCOM,
false,
SAMPLE_DEFAULT_CLI_VERSION,
getRunnerLogger(true),
false
);
t.assert(releaseApiMock.isDone(), "Releases API should have been called");
t.assert(toolcache.find("CodeQL", expectedToolcacheVersion));
t.deepEqual(result.toolsVersion, cliVersion);
});
});
});
}

for (const { isCached, tagName, toolcacheCliVersion } of [
{
Expand Down
40 changes: 17 additions & 23 deletions src/setup-codeql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,28 +76,16 @@ async function getCodeQLBundleReleasesDotcomOnly(
});
logger.debug(`Found ${releases.length} releases.`);

return releases.flatMap((release) => {
const cliVersionFileVersions = release.assets
.map((asset) => asset.name.match(/cli-version-(.*)\.txt/)?.[1])
.filter((v) => v)
.map((v) => v as string);

if (cliVersionFileVersions.length > 1) {
logger.warning(
`Ignoring release ${release.tag_name} with multiple CLI version marker files.`
);
return [];
}
return [
{ cliVersion: cliVersionFileVersions[0], tagName: release.tag_name },
];
});
return releases.map((release) => ({
cliVersion: tryGetCodeQLCliVersionForRelease(release, logger),
tagName: release.tag_name,
}));
}

async function tryGetCodeQLCliVersionForRelease(
function tryGetCodeQLCliVersionForRelease(
release,
logger: Logger
): Promise<string | undefined> {
): string | undefined {
const cliVersionsFromMarkerFiles = release.assets
.map((asset) => asset.name.match(/cli-version-(.*)\.txt/)?.[1])
.filter((v) => v)
Expand Down Expand Up @@ -573,11 +561,17 @@ export async function downloadCodeQL(
logger
))) ||
undefined;
// Include the bundle version in the toolcache version number so that if the user requests the
// same URL again, we can get it from the cache without having to call any of the Releases API.
const toolcacheVersion = cliVersion
? `${cliVersion}-${bundleVersion}`
: convertToSemVer(bundleVersion, logger);
// Include both the CLI version and the bundle version in the toolcache version number. That way
// if the user requests the same URL again, we can get it from the cache without having to call
// any of the Releases API.
//
// Special case: If the CLI version is a pre-release, then cache the bundle as
// `0.0.0-<bundleVersion>` to avoid the bundle being interpreted as containing a stable CLI
// release.
const toolcacheVersion =
cliVersion && !cliVersion.includes("-")
? `${cliVersion}-${bundleVersion}`
: convertToSemVer(bundleVersion, logger);
return {
toolsVersion: cliVersion || toolcacheVersion,
codeqlFolder: await toolcache.cacheDir(
Expand Down

0 comments on commit 8a4abfd

Please sign in to comment.