Skip to content

Commit 6e38976

Browse files
committed
Add toolcache.ts script for bundle-toolcache.yml
1 parent 3db7caf commit 6e38976

3 files changed

Lines changed: 120 additions & 60 deletions

File tree

‎.github/workflows/__bundle-toolcache.yml‎

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

‎pr-checks/checks/bundle-toolcache.yml‎

Lines changed: 8 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,47 +6,25 @@ operatingSystems:
66
- ubuntu
77
- macos
88
- windows
9+
installNode: true
910
steps:
1011
- name: Remove CodeQL from toolcache
11-
uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8.0.0
12-
with:
13-
script: |
14-
const fs = require('fs');
15-
const path = require('path');
16-
const codeqlPath = path.join(process.env['RUNNER_TOOL_CACHE'], 'CodeQL');
17-
fs.rmdirSync(codeqlPath, { recursive: true });
18-
- name: Install @actions/tool-cache
19-
run: npm install @actions/tool-cache@3
12+
run: cd ./../action && npx tsx ./pr-checks/toolcache.ts clear
13+
2014
- name: Check toolcache does not contain CodeQL
21-
uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8.0.0
22-
with:
23-
script: |
24-
const toolcache = require('@actions/tool-cache');
25-
const allCodeqlVersions = toolcache.findAllVersions('CodeQL');
26-
if (allCodeqlVersions.length !== 0) {
27-
throw new Error(`CodeQL should not be found in the toolcache, but found ${allCodeqlVersions}`);
28-
}
29-
console.log('No versions of CodeQL found in the toolcache');
15+
run: cd ./../action && npx tsx ./pr-checks/toolcache.ts absent
16+
3017
- id: init
3118
uses: ./../action/init
3219
with:
3320
# Request multiple languages so this check uses the combined bundle.
3421
languages: javascript,python
3522
tools: ${{ steps.prepare-test.outputs.tools-url }}
23+
3624
- uses: ./../action/analyze
3725
with:
3826
output: ${{ runner.temp }}/results
3927
upload-database: false
28+
4029
- name: Check CodeQL is installed within the toolcache
41-
uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8.0.0
42-
with:
43-
script: |
44-
const toolcache = require('@actions/tool-cache');
45-
const allCodeqlVersions = toolcache.findAllVersions('CodeQL');
46-
console.log(`Found CodeQL versions: ${allCodeqlVersions}`);
47-
if (allCodeqlVersions.length === 0) {
48-
throw new Error('CodeQL not found in toolcache');
49-
}
50-
if (allCodeqlVersions.length > 1) {
51-
throw new Error('Multiple CodeQL versions found in toolcache');
52-
}
30+
run: cd ./../action && npx tsx ./pr-checks/toolcache.ts installed -n 1

‎pr-checks/toolcache.ts‎

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
#!/usr/bin/env npx tsx
2+
3+
/**
4+
* Provides utility commands for working with the Actions toolcache in PR checks.
5+
*/
6+
7+
import fs from "fs";
8+
import { parseArgs } from "node:util";
9+
import path from "path";
10+
11+
import * as core from "@actions/core";
12+
import * as toolcache from "@actions/tool-cache";
13+
14+
function removeCodeQLFromToolcache() {
15+
const toolcachePath = process.env["RUNNER_TOOL_CACHE"];
16+
17+
if (toolcachePath === undefined) {
18+
throw new Error("Expected RUNNER_TOOL_CACHE to be defined");
19+
}
20+
21+
const codeqlPath = path.join(toolcachePath, "CodeQL");
22+
fs.rmdirSync(codeqlPath, { recursive: true });
23+
24+
core.info(`Removed '${codeqlPath}' from the toolcache.`);
25+
}
26+
27+
function findCodeQL() {
28+
return toolcache.findAllVersions("CodeQL");
29+
}
30+
31+
function noCodeQLInToolcache() {
32+
const allCodeqlVersions = findCodeQL();
33+
if (allCodeqlVersions.length !== 0) {
34+
throw new Error(
35+
`CodeQL should not be found in the toolcache, but found ${allCodeqlVersions}`,
36+
);
37+
}
38+
core.info("No versions of CodeQL found in the toolcache");
39+
}
40+
41+
function hasCodeQLInToolcache(expectedNumber?: string) {
42+
const expected =
43+
expectedNumber !== undefined
44+
? Number.parseInt(expectedNumber, 10)
45+
: undefined;
46+
47+
const allCodeqlVersions = findCodeQL();
48+
core.info(`Found CodeQL versions: ${allCodeqlVersions}`);
49+
50+
if (allCodeqlVersions.length === 0) {
51+
throw new Error("CodeQL not found in toolcache");
52+
}
53+
if (expected !== undefined && allCodeqlVersions.length > expected) {
54+
throw new Error(
55+
`Expected ${expected} versions of CodeQL in the toolcache, but found ${allCodeqlVersions.length}`,
56+
);
57+
}
58+
}
59+
60+
function main() {
61+
const { positionals, values } = parseArgs({
62+
options: {
63+
// The *number* of expected versions in the toolcache.
64+
expectedVersions: { type: "string", short: "n" },
65+
},
66+
allowPositionals: true,
67+
});
68+
69+
if (positionals.length < 1) {
70+
throw new Error("Command not specified.");
71+
}
72+
73+
const cmd = positionals[0];
74+
75+
switch (cmd.trim().toLowerCase()) {
76+
case "clear":
77+
removeCodeQLFromToolcache();
78+
return;
79+
case "absent":
80+
noCodeQLInToolcache();
81+
return;
82+
case "installed":
83+
hasCodeQLInToolcache(values.expectedVersions);
84+
return;
85+
default:
86+
throw new Error(`Unknown command '${cmd}'.`);
87+
}
88+
}
89+
90+
if (import.meta.main) {
91+
try {
92+
main();
93+
} catch (err) {
94+
core.setFailed(err instanceof Error ? err.message : String(err));
95+
}
96+
}

0 commit comments

Comments
 (0)