Skip to content

Commit 927de48

Browse files
authored
Merge pull request #1523 from github/henrymercer/fix/cli-version-for-different-bundles
Fix toolcache behavior when downloading bundle from another repo
2 parents d396227 + e4c0a1b commit 927de48

File tree

6 files changed

+97
-25
lines changed

6 files changed

+97
-25
lines changed

lib/codeql.test.js

Lines changed: 26 additions & 2 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: 8 additions & 7 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: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,14 @@ test.beforeEach(() => {
8787
function mockDownloadApi({
8888
apiDetails = sampleApiDetails,
8989
isPinned,
90+
repo = "github/codeql-action",
91+
platformSpecific = true,
9092
tagName,
9193
}: {
9294
apiDetails?: GitHubApiDetails;
9395
isPinned?: boolean;
96+
repo?: string;
97+
platformSpecific?: boolean;
9498
tagName: string;
9599
}): string {
96100
const platform =
@@ -102,7 +106,9 @@ function mockDownloadApi({
102106

103107
const baseUrl = apiDetails?.url ?? "https://example.com";
104108
const relativeUrl = apiDetails
105-
? `/github/codeql-action/releases/download/${tagName}/codeql-bundle-${platform}.tar.gz`
109+
? `/${repo}/releases/download/${tagName}/codeql-bundle${
110+
platformSpecific ? `-${platform}` : ""
111+
}.tar.gz`
106112
: `/download/${tagName}/codeql-bundle.tar.gz`;
107113

108114
nock(baseUrl)
@@ -546,6 +552,45 @@ for (const isBundleVersionInUrl of [true, false]) {
546552
});
547553
}
548554

555+
test("bundle URL from another repo is cached as 0.0.0-bundleVersion", async (t) => {
556+
await util.withTmpDir(async (tmpDir) => {
557+
setupActionsVars(tmpDir, tmpDir);
558+
559+
mockApiDetails(sampleApiDetails);
560+
sinon.stub(actionsUtil, "isRunningLocalAction").returns(true);
561+
const releasesApiMock = mockReleaseApi({
562+
assetNames: ["cli-version-2.12.2.txt"],
563+
tagName: "codeql-bundle-20230203",
564+
});
565+
mockDownloadApi({
566+
repo: "dsp-testing/codeql-cli-nightlies",
567+
platformSpecific: false,
568+
tagName: "codeql-bundle-20230203",
569+
});
570+
571+
const result = await codeql.setupCodeQL(
572+
"https://github.com/dsp-testing/codeql-cli-nightlies/releases/download/codeql-bundle-20230203/codeql-bundle.tar.gz",
573+
sampleApiDetails,
574+
tmpDir,
575+
util.GitHubVariant.DOTCOM,
576+
false,
577+
SAMPLE_DEFAULT_CLI_VERSION,
578+
getRunnerLogger(true),
579+
false
580+
);
581+
582+
t.is(result.toolsVersion, "0.0.0-20230203");
583+
t.is(result.toolsSource, ToolsSource.Download);
584+
t.true(Number.isInteger(result.toolsDownloadDurationMs));
585+
586+
const cachedVersions = toolcache.findAllVersions("CodeQL");
587+
t.is(cachedVersions.length, 1);
588+
t.is(cachedVersions[0], "0.0.0-20230203");
589+
590+
t.false(releasesApiMock.isDone());
591+
});
592+
});
593+
549594
test("getExtraOptions works for explicit paths", (t) => {
550595
t.deepEqual(codeql.getExtraOptions({}, ["foo"], []), []);
551596

src/setup-codeql.ts

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -663,14 +663,17 @@ export async function downloadCodeQL(
663663
}
664664

665665
// Try to compute the CLI version for this bundle
666-
const cliVersion: string | undefined =
667-
maybeCliVersion ||
668-
(variant === util.GitHubVariant.DOTCOM &&
669-
(await tryFindCliVersionDotcomOnly(
670-
`codeql-bundle-${bundleVersion}`,
671-
logger
672-
))) ||
673-
undefined;
666+
if (
667+
maybeCliVersion === undefined &&
668+
variant === util.GitHubVariant.DOTCOM &&
669+
codeqlURL.includes(`/${CODEQL_DEFAULT_ACTION_REPOSITORY}/`)
670+
) {
671+
maybeCliVersion = await tryFindCliVersionDotcomOnly(
672+
`codeql-bundle-${bundleVersion}`,
673+
logger
674+
);
675+
}
676+
674677
// Include both the CLI version and the bundle version in the toolcache version number. That way
675678
// if the user requests the same URL again, we can get it from the cache without having to call
676679
// any of the Releases API.
@@ -680,12 +683,11 @@ export async function downloadCodeQL(
680683
// CLI release. In principle, it should be enough to just check that the CLI version isn't a
681684
// pre-release, but the version numbers of CodeQL nightlies have the format `x.y.z+<timestamp>`,
682685
// and we don't want these nightlies to override stable CLI versions in the toolcache.
683-
const toolcacheVersion =
684-
cliVersion && cliVersion.match(/^[0-9]+\.[0-9]+\.[0-9]+$/)
685-
? `${cliVersion}-${bundleVersion}`
686-
: convertToSemVer(bundleVersion, logger);
686+
const toolcacheVersion = maybeCliVersion?.match(/^[0-9]+\.[0-9]+\.[0-9]+$/)
687+
? `${maybeCliVersion}-${bundleVersion}`
688+
: convertToSemVer(bundleVersion, logger);
687689
return {
688-
toolsVersion: cliVersion || toolcacheVersion,
690+
toolsVersion: maybeCliVersion ?? toolcacheVersion,
689691
codeqlFolder: await toolcache.cacheDir(
690692
codeqlExtracted,
691693
"CodeQL",

0 commit comments

Comments
 (0)