Skip to content

Commit 8c514cb

Browse files
Copilotfelickz
andauthored
Support bare CodeQL CLI version numbers in the tools input
Co-authored-by: felickz <1760475+felickz@users.noreply.github.com>
1 parent 6a9359a commit 8c514cb

6 files changed

Lines changed: 196 additions & 5 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ See the [releases page](https://github.com/github/codeql-action/releases) for th
44

55
## [UNRELEASED]
66

7-
No user facing changes.
7+
- The `tools` input for the `init` and `setup-codeql` Actions now accepts a bare CodeQL CLI version number, for example `2.19.0` or `v2.19.0`. The Action will automatically download the CodeQL Bundle release asset that matches the runner's operating system and architecture, preferring the smaller `zstd`-compressed bundle where it is supported.
88

99
## 4.37.5 - 03 Aug 2026
1010

‎init/action.yml‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ inputs:
1010
1111
- A local path to a CodeQL Bundle tarball, or
1212
- The URL of a CodeQL Bundle tarball GitHub release asset, or
13+
- A CodeQL CLI version number, for example `2.19.0` or `v2.19.0`, in which case the CodeQL
14+
Bundle containing that version will be downloaded, selecting the archive format and
15+
release asset that matches the runner's operating system and architecture, or
1316
- A special value `linked` which uses the version of the CodeQL tools
1417
that the Action has been bundled with.
1518
- A special value `nightly` which uses the latest nightly version of the

‎lib/entry-points.js‎

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

‎setup-codeql/action.yml‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ inputs:
1010
1111
- A local path to a CodeQL Bundle tarball, or
1212
- The URL of a CodeQL Bundle tarball GitHub release asset, or
13+
- A CodeQL CLI version number, for example `2.19.0` or `v2.19.0`, in which case the CodeQL
14+
Bundle containing that version will be downloaded, selecting the archive format and
15+
release asset that matches the runner's operating system and architecture, or
1316
- A special value `linked` which uses the version of the CodeQL tools
1417
that the Action has been bundled with.
1518
- A special value `nightly` which uses the latest nightly version of the

‎src/setup-codeql.test.ts‎

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,151 @@ for (const {
181181
);
182182
}
183183

184+
const CLI_VERSION_TOOLS_INPUT_TEST_CASES = [
185+
{
186+
toolsInput: "2.20.1",
187+
platform: "linux",
188+
tarSupportsZstd: true,
189+
expectedBundleName: "codeql-bundle-linux64.tar.zst",
190+
expectedCompressionMethod: "zstd",
191+
},
192+
{
193+
toolsInput: "v2.20.1",
194+
platform: "darwin",
195+
tarSupportsZstd: true,
196+
expectedBundleName: "codeql-bundle-osx64.tar.zst",
197+
expectedCompressionMethod: "zstd",
198+
},
199+
{
200+
toolsInput: "v2.20.1",
201+
platform: "win32",
202+
tarSupportsZstd: true,
203+
expectedBundleName: "codeql-bundle-win64.tar.gz",
204+
expectedCompressionMethod: "gzip",
205+
},
206+
{
207+
toolsInput: "2.20.1",
208+
platform: "linux",
209+
tarSupportsZstd: false,
210+
expectedBundleName: "codeql-bundle-linux64.tar.gz",
211+
expectedCompressionMethod: "gzip",
212+
},
213+
{
214+
// CodeQL versions older than 2.19.0 don't have zstd bundles, so gzip should be selected
215+
// even though the runner supports zstd.
216+
toolsInput: "v2.18.4",
217+
platform: "linux",
218+
tarSupportsZstd: true,
219+
expectedBundleName: "codeql-bundle-linux64.tar.gz",
220+
expectedCompressionMethod: "gzip",
221+
},
222+
] as const;
223+
224+
for (const {
225+
toolsInput,
226+
platform,
227+
tarSupportsZstd,
228+
expectedBundleName,
229+
expectedCompressionMethod,
230+
} of CLI_VERSION_TOOLS_INPUT_TEST_CASES) {
231+
test.serial(
232+
`getCodeQLSource selects ${expectedBundleName} for 'tools: ${toolsInput}'`,
233+
async (t) => {
234+
const features = createFeatures([]);
235+
sinon.stub(process, "platform").value(platform);
236+
237+
await withTmpDir(async (tmpDir) => {
238+
setupActionsVars(tmpDir, tmpDir);
239+
const source = await setupCodeql.getCodeQLSource(
240+
toolsInput,
241+
SAMPLE_DEFAULT_CLI_VERSION,
242+
undefined, // rawLanguages
243+
false, // useOverlayAwareDefaultCliVersion
244+
SAMPLE_DOTCOM_API_DETAILS,
245+
GitHubVariant.DOTCOM,
246+
tarSupportsZstd,
247+
features,
248+
getRunnerLogger(true),
249+
);
250+
251+
const expectedCliVersion = toolsInput.replace(/^v/, "");
252+
t.is(source.toolsVersion, expectedCliVersion);
253+
t.is(source["cliVersion"], expectedCliVersion);
254+
t.is(source.sourceType, "download");
255+
if (source.sourceType === "download") {
256+
t.is(source.compressionMethod, expectedCompressionMethod);
257+
t.true(source.codeqlURL.endsWith(`/${expectedBundleName}`));
258+
t.true(
259+
source.codeqlURL.includes(`/codeql-bundle-v${expectedCliVersion}/`),
260+
);
261+
}
262+
});
263+
},
264+
);
265+
}
266+
267+
test.serial(
268+
"getCodeQLSource logs a message when given a bare CLI version number",
269+
async (t) => {
270+
const loggedMessages: LoggedMessage[] = [];
271+
const logger = getRecordingLogger(loggedMessages);
272+
const features = createFeatures([]);
273+
274+
sinon.stub(process, "platform").value("linux");
275+
276+
await withTmpDir(async (tmpDir) => {
277+
setupActionsVars(tmpDir, tmpDir);
278+
const source = await setupCodeql.getCodeQLSource(
279+
"v2.20.1",
280+
SAMPLE_DEFAULT_CLI_VERSION,
281+
undefined, // rawLanguages
282+
false, // useOverlayAwareDefaultCliVersion
283+
SAMPLE_DOTCOM_API_DETAILS,
284+
GitHubVariant.DOTCOM,
285+
true,
286+
features,
287+
logger,
288+
);
289+
290+
t.is(source.sourceType, "download");
291+
t.is(source.toolsVersion, "2.20.1");
292+
293+
checkExpectedLogMessages(t, loggedMessages, [
294+
"'tools: v2.20.1' was requested, so using CodeQL version 2.20.1.",
295+
]);
296+
});
297+
},
298+
);
299+
300+
test.serial(
301+
"getCodeQLSource still resolves a local tarball path when the path looks like a version-ish string",
302+
async (t) => {
303+
const features = createFeatures([]);
304+
305+
await withTmpDir(async (tmpDir) => {
306+
setupActionsVars(tmpDir, tmpDir);
307+
const localPath = "/path/to/codeql-bundle-2.20.1.tar.gz";
308+
const source = await setupCodeql.getCodeQLSource(
309+
localPath,
310+
SAMPLE_DEFAULT_CLI_VERSION,
311+
undefined, // rawLanguages
312+
false, // useOverlayAwareDefaultCliVersion
313+
SAMPLE_DOTCOM_API_DETAILS,
314+
GitHubVariant.DOTCOM,
315+
false,
316+
features,
317+
getRunnerLogger(true),
318+
);
319+
320+
t.is(source.sourceType, "local");
321+
if (source.sourceType === "local") {
322+
t.is(source.codeqlTarPath, localPath);
323+
t.is(source.compressionMethod, "gzip");
324+
}
325+
});
326+
},
327+
);
328+
184329
test.serial(
185330
"getCodeQLSource correctly returns bundled CLI version when tools == latest",
186331
async (t) => {

‎src/setup-codeql.ts‎

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,22 @@ export function convertToSemVer(version: string, logger: Logger): string {
209209
return s;
210210
}
211211

212+
/**
213+
* If the `tools` input is a bare CodeQL CLI version number, for example `2.19.0` or `v2.19.0`,
214+
* returns the version normalized to the `x.y.z` form used elsewhere in the Action to identify
215+
* CLI versions. Otherwise returns `undefined`.
216+
*
217+
* This allows users to request a specific version of the CodeQL CLI without needing to know the
218+
* name or URL of the bundle asset for their platform: the appropriate bundle for the runner's OS,
219+
* and the best compression method it supports, are selected automatically, in the same way as
220+
* they are for the default and `linked` versions of the tools.
221+
*/
222+
function tryGetCliVersionFromToolsInput(
223+
toolsInput: string,
224+
): string | undefined {
225+
return semver.valid(toolsInput) ?? undefined;
226+
}
227+
212228
export type CodeQLToolsSource =
213229
| {
214230
codeqlTarPath: string;
@@ -418,13 +434,15 @@ export async function getCodeQLSource(
418434
features: FeatureEnablement,
419435
logger: Logger,
420436
): Promise<CodeQLToolsSource> {
421-
// If there is an explicit `tools` input, it's not one of the reserved values, and it doesn't appear
422-
// to point to a URL, then we assume it is a local path and use the CLI from there.
437+
// If there is an explicit `tools` input, it's not one of the reserved values, it doesn't appear
438+
// to point to a URL, and it isn't a bare CodeQL CLI version number, then we assume it is a local
439+
// path and use the CLI from there.
423440
// TODO: This appears to misclassify filenames that happen to start with `http` as URLs.
424441
if (
425442
toolsInput &&
426443
!isReservedToolsValue(toolsInput) &&
427-
!toolsInput.startsWith("http")
444+
!toolsInput.startsWith("http") &&
445+
tryGetCliVersionFromToolsInput(toolsInput) === undefined
428446
) {
429447
logger.info(`Using CodeQL CLI from local path ${toolsInput}`);
430448
const compressionMethod = tar.inferCompressionMethod(toolsInput);
@@ -569,6 +587,19 @@ export async function getCodeQLSource(
569587
cliVersion = version.cliVersion;
570588
tagName = version.tagName;
571589
}
590+
} else if (
591+
toolsInput !== undefined &&
592+
tryGetCliVersionFromToolsInput(toolsInput) !== undefined
593+
) {
594+
// A bare CodeQL CLI version number, e.g. `2.19.0` or `v2.19.0`, was provided. Compute the tag
595+
// name of the CodeQL bundle containing this CLI version so that the appropriate bundle for
596+
// the current platform can be downloaded below.
597+
cliVersion = tryGetCliVersionFromToolsInput(toolsInput);
598+
tagName = `codeql-bundle-v${cliVersion}`;
599+
600+
logger.info(
601+
`'tools: ${toolsInput}' was requested, so using CodeQL version ${cliVersion}.`,
602+
);
572603
} else if (toolsInput !== undefined) {
573604
// If a tools URL was provided, then use that.
574605
tagName = tryGetTagNameFromUrl(toolsInput, logger);

0 commit comments

Comments
 (0)