Skip to content

Commit c1e8d8d

Browse files
henrymercerCopilot
andcommitted
Reuse the toolcache for stable releases in the CodeQL Action repositories
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
1 parent 17f1478 commit c1e8d8d

3 files changed

Lines changed: 127 additions & 10 deletions

File tree

‎lib/entry-points.js‎

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

‎src/setup-codeql.test.ts‎

Lines changed: 78 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1519,15 +1519,88 @@ test.serial(
15191519
},
15201520
);
15211521

1522+
for (const [apiDetails, variant] of [
1523+
[SAMPLE_DOTCOM_API_DETAILS, GitHubVariant.DOTCOM],
1524+
[GHES_API_DETAILS, GitHubVariant.GHES],
1525+
] as const) {
1526+
test.serial(
1527+
`setupCodeQLBundle caches a stable release in the CodeQL Action repository on ${variant}`,
1528+
async (t) => {
1529+
const fixture = stubRequestedRelease({
1530+
apiDetails,
1531+
repository: "github/codeql-action",
1532+
});
1533+
const extract = stubDownloadAndExtract();
1534+
1535+
await withTmpDir(async (tmpDir) => {
1536+
setupActionsVars(tmpDir, tmpDir);
1537+
const setup = () =>
1538+
setupCodeql.setupCodeQLBundle(
1539+
fixture.releaseURL,
1540+
apiDetails,
1541+
tmpDir,
1542+
variant,
1543+
PER_LANGUAGE_CLI_VERSION,
1544+
undefined, // rawLanguages
1545+
false, // useOverlayAwareDefaultCliVersion
1546+
createFeatures([]),
1547+
getRunnerLogger(true),
1548+
);
1549+
1550+
const downloaded = await setup();
1551+
t.deepEqual(fixture.requests, [
1552+
`${apiDetails.apiURL}/repos/github/codeql-action/releases/tags/codeql-bundle-v${MIN_PER_LANGUAGE_BUNDLE_CLI_VERSION}`,
1553+
]);
1554+
t.is(downloaded.toolsSource, setupCodeql.ToolsSource.Download);
1555+
t.is(extract.firstCall.args[0], fixture.assets[0].url);
1556+
t.is(extract.firstCall.args[3], `token ${apiDetails.auth}`);
1557+
t.is(
1558+
downloaded.codeqlFolder,
1559+
toolcache.find("CodeQL", MIN_PER_LANGUAGE_BUNDLE_CLI_VERSION),
1560+
);
1561+
1562+
const cached = await setup();
1563+
t.is(cached.toolsSource, setupCodeql.ToolsSource.Toolcache);
1564+
t.is(cached.codeqlFolder, downloaded.codeqlFolder);
1565+
t.true(extract.calledOnce);
1566+
});
1567+
},
1568+
);
1569+
}
1570+
15221571
for (const { repository, tagName, markers } of [
1572+
{
1573+
repository: "github/codeql-action",
1574+
tagName: "codeql-bundle-v2.27.1-rc.1",
1575+
markers: [],
1576+
},
1577+
{
1578+
repository: "github/codeql-action",
1579+
tagName: "codeql-bundle-v2.27.1",
1580+
markers: ["2.27.1+202609241200"],
1581+
},
1582+
{
1583+
repository: "github/codeql-action",
1584+
tagName: "codeql-bundle-20260924",
1585+
markers: ["2.27.1+202609241200"],
1586+
},
1587+
{
1588+
repository: "github/codeql-action",
1589+
tagName: "codeql-bundle-acme-2.27.1",
1590+
markers: ["2.27.1"],
1591+
},
15231592
{
15241593
repository: "octo/tools",
15251594
tagName: "codeql-bundle-feature_branch",
15261595
markers: [],
15271596
},
15281597
]) {
1598+
const description =
1599+
markers.length === 0
1600+
? tagName
1601+
: `${tagName} with marker ${markers.join(", ")}`;
15291602
test.serial(
1530-
`setupCodeQLBundle doesn't share the toolcache with ${tagName} in ${repository}`,
1603+
`setupCodeQLBundle doesn't share the toolcache with ${description} in ${repository}`,
15311604
async (t) => {
15321605
const fixture = stubRequestedRelease({ repository, tagName, markers });
15331606
const extract = stubDownloadAndExtract();
@@ -1590,7 +1663,10 @@ for (const repository of ["github/codeql-action", "octo/tools"]) {
15901663
`https://github.com/${repository}/releases/download/codeql-bundle-v${MIN_PER_LANGUAGE_BUNDLE_CLI_VERSION}/codeql-bundle-linux64.tar.zst`,
15911664
);
15921665
t.is(extract.firstCall.args[3], undefined);
1593-
t.false(fs.existsSync(`${result.codeqlFolder}.complete`));
1666+
t.is(
1667+
fs.existsSync(`${result.codeqlFolder}.complete`),
1668+
repository === "github/codeql-action",
1669+
);
15941670
});
15951671
},
15961672
);

‎src/setup-codeql.ts‎

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
BundleSelection,
2727
BundleSelectionOptions,
2828
CodeQLRelease,
29+
CodeQLReleaseReference,
2930
getPublicRelease,
3031
getRelease,
3132
getReleaseCliVersion,
@@ -114,6 +115,32 @@ function getDefaultBundleSources(
114115
});
115116
}
116117

118+
/**
119+
* Whether a release requested by URL contains the same build as the toolcache entry for its CLI
120+
* version. The toolcache is keyed by version alone, so we only assume this for stable releases
121+
* tagged `codeql-bundle-v<version>` in the repositories we download the default bundles from.
122+
*/
123+
function isCacheableRelease(
124+
reference: CodeQLReleaseReference,
125+
cliVersion: string | undefined,
126+
apiDetails: api.GitHubApiDetails,
127+
logger: Logger,
128+
): boolean {
129+
if (
130+
cliVersion === undefined ||
131+
!/^\d+\.\d+\.\d+$/.test(cliVersion) ||
132+
reference.tagName !== `codeql-bundle-v${cliVersion}`
133+
) {
134+
return false;
135+
}
136+
const repository = `${reference.owner}/${reference.repo}`.toLowerCase();
137+
return getDefaultBundleSources(apiDetails, logger).some(
138+
([serverURL, sourceRepository]) =>
139+
new URL(serverURL).origin === new URL(reference.serverURL).origin &&
140+
sourceRepository.toLowerCase() === repository,
141+
);
142+
}
143+
117144
/**
118145
* Selects a bundle from the first release tagged `tagName` that has a compatible bundle, trying the
119146
* Action repositories on this GitHub instance before the canonical Action on GitHub.com. If we
@@ -405,9 +432,10 @@ async function resolveDefaultCliVersion(
405432
*
406433
* - A local path is extracted without using the toolcache.
407434
* - A release URL selects a bundle from that release, and takes precedence over the `force_nightly`
408-
* feature flag. We don't use the toolcache, since a release may contain a different build than
409-
* the cached bundle for its version. Bundle URLs keep using the toolcache for the version in
410-
* their tag, for compatibility.
435+
* feature flag. Only stable releases in the repositories we download the default bundles from use
436+
* the toolcache, since other releases may contain a different build than the cached bundle for
437+
* their version. Bundle URLs keep using the toolcache for the version in their tag, for
438+
* compatibility.
411439
* - `nightly` or `nightly-latest`, or the `force_nightly` feature flag in a dynamic workflow,
412440
* selects a bundle from the latest nightly release. We then continue with that bundle's URL.
413441
* - `linked`, or its old name `latest`, selects the version shipped with the Action.
@@ -625,7 +653,9 @@ export async function getCodeQLSource(
625653
release.assetNames ?? [],
626654
logger,
627655
);
628-
customReleaseURL = release.url;
656+
if (!isCacheableRelease(requestedRelease, cliVersion, apiDetails, logger)) {
657+
customReleaseURL = release.url;
658+
}
629659
} else if (toolsInput !== undefined) {
630660
// Any other value is a bundle URL, including one we selected from the latest nightly above.
631661
// We use the version in its tag, if any, for the toolcache, so we assume that bundles with the
@@ -971,8 +1001,8 @@ function getToolcacheDestination(
9711001
): util.Result<string, string> {
9721002
if (source.customReleaseURL !== undefined) {
9731003
return new util.Failure(
974-
`Not caching the CodeQL tools from ${source.customReleaseURL}, since we don't cache ` +
975-
"releases requested by URL.",
1004+
`Not caching the CodeQL tools from ${source.customReleaseURL}, since we only cache stable ` +
1005+
"releases in the CodeQL Action repositories.",
9761006
);
9771007
}
9781008
if (source.bundle.kind !== "combined") {

0 commit comments

Comments
 (0)