Skip to content

Commit 425ef85

Browse files
committed
Support requesting CLI from toolcache with tools: toolcache
1 parent 297313d commit 425ef85

File tree

8 files changed

+293
-6
lines changed

8 files changed

+293
-6
lines changed

init/action.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ inputs:
1515
- A special value `nightly` which uses the latest nightly version of the
1616
CodeQL tools. Note that this is unstable and not recommended for
1717
production use.
18+
- A special value `toolcache` which uses the latest version available in the
19+
toolcache on the runner.
1820
1921
If not specified, the Action will check in several places until it finds
2022
the CodeQL tools.

lib/analyze-action.js

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

lib/init-action-post.js

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

lib/init-action.js

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

lib/upload-lib.js

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

lib/upload-sarif-action.js

Lines changed: 32 additions & 1 deletion
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: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,113 @@ test("setupCodeQLBundle logs the CodeQL CLI version being used when asked to dow
255255
});
256256
});
257257

258+
test("getCodeQLSource correctly returns latest version from toolcache when tools == toolcache", async (t) => {
259+
const loggedMessages: LoggedMessage[] = [];
260+
const logger = getRecordingLogger(loggedMessages);
261+
262+
const latestToolcacheVersion = "3.2.1";
263+
const latestVersionPath = "/path/to/latest";
264+
const testVersions = ["2.3.1", latestToolcacheVersion, "1.2.3"];
265+
const findAllVersionsStub = sinon
266+
.stub(toolcache, "findAllVersions")
267+
.returns(testVersions);
268+
const findStub = sinon.stub(toolcache, "find");
269+
findStub
270+
.withArgs("CodeQL", latestToolcacheVersion)
271+
.returns(latestVersionPath);
272+
273+
await withTmpDir(async (tmpDir) => {
274+
setupActionsVars(tmpDir, tmpDir);
275+
const source = await setupCodeql.getCodeQLSource(
276+
"toolcache",
277+
SAMPLE_DEFAULT_CLI_VERSION,
278+
SAMPLE_DOTCOM_API_DETAILS,
279+
GitHubVariant.DOTCOM,
280+
false,
281+
logger,
282+
);
283+
284+
// Check that the toolcache functions were called with the expected arguments
285+
t.assert(
286+
findAllVersionsStub.calledOnceWith("CodeQL"),
287+
`toolcache.findAllVersions("CodeQL") wasn't called`,
288+
);
289+
t.assert(
290+
findStub.calledOnceWith("CodeQL", latestToolcacheVersion),
291+
`toolcache.find("CodeQL", ${latestToolcacheVersion}) wasn't called`,
292+
);
293+
294+
// Check that `sourceType` and `toolsVersion` match expectations.
295+
t.is(source.sourceType, "toolcache");
296+
t.is(source.toolsVersion, latestToolcacheVersion);
297+
298+
// Check that key messages we would expect to find in the log are present.
299+
const expectedMessages: string[] = [
300+
`Attempting to use the latest CodeQL CLI version in the toolcache, as requested by 'tools: toolcache'.`,
301+
`CLI version ${latestToolcacheVersion} is the latest version in the toolcache.`,
302+
`Using CodeQL CLI version ${latestToolcacheVersion} from toolcache at ${latestVersionPath}`,
303+
];
304+
for (const expectedMessage of expectedMessages) {
305+
t.assert(
306+
loggedMessages.some(
307+
(msg) =>
308+
typeof msg.message === "string" &&
309+
msg.message.includes(expectedMessage),
310+
),
311+
`Expected '${expectedMessage}' in the logger output, but didn't find it.`,
312+
);
313+
}
314+
});
315+
});
316+
317+
test("getCodeQLSource falls back to downloading the CLI if the toolcache doesn't have a CodeQL CLI when tools == toolcache", async (t) => {
318+
const loggedMessages: LoggedMessage[] = [];
319+
const logger = getRecordingLogger(loggedMessages);
320+
321+
const testVersions = [];
322+
const findAllVersionsStub = sinon
323+
.stub(toolcache, "findAllVersions")
324+
.returns(testVersions);
325+
326+
await withTmpDir(async (tmpDir) => {
327+
setupActionsVars(tmpDir, tmpDir);
328+
const source = await setupCodeql.getCodeQLSource(
329+
"toolcache",
330+
SAMPLE_DEFAULT_CLI_VERSION,
331+
SAMPLE_DOTCOM_API_DETAILS,
332+
GitHubVariant.DOTCOM,
333+
false,
334+
logger,
335+
);
336+
337+
// Check that the toolcache functions were called with the expected arguments
338+
t.assert(
339+
findAllVersionsStub.calledWith("CodeQL"),
340+
`toolcache.findAllVersions("CodeQL") wasn't called`,
341+
);
342+
343+
// Check that `sourceType` and `toolsVersion` match expectations.
344+
t.is(source.sourceType, "download");
345+
t.is(source.toolsVersion, SAMPLE_DEFAULT_CLI_VERSION.cliVersion);
346+
347+
// Check that key messages we would expect to find in the log are present.
348+
const expectedMessages: string[] = [
349+
`Attempting to use the latest CodeQL CLI version in the toolcache, as requested by 'tools: toolcache'.`,
350+
`Found no CodeQL CLI in the toolcache, ignoring 'tools: toolcache'...`,
351+
];
352+
for (const expectedMessage of expectedMessages) {
353+
t.assert(
354+
loggedMessages.some(
355+
(msg) =>
356+
typeof msg.message === "string" &&
357+
msg.message.includes(expectedMessage),
358+
),
359+
`Expected '${expectedMessage}' in the logger output, but didn't find it.`,
360+
);
361+
}
362+
});
363+
});
364+
258365
test('tryGetTagNameFromUrl extracts the right tag name for a repo name containing "codeql-bundle"', (t) => {
259366
t.is(
260367
setupCodeql.tryGetTagNameFromUrl(

0 commit comments

Comments
 (0)