Skip to content

Commit d04fe60

Browse files
authored
Merge pull request #4255 from github/mbg/npm-versions
Add `toolcache.ts` script for `bundle(-from)-toolcache` checks
2 parents 4e7f1a8 + 0a00d0c commit d04fe60

7 files changed

Lines changed: 158 additions & 98 deletions

File tree

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

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

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

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

‎package-lock.json‎

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,20 @@ name: "Bundle: From toolcache"
22
description: "The CodeQL bundle should be cached within the toolcache"
33
versions:
44
- toolcache
5+
installNode: true
56
steps:
6-
- name: Install @actions/tool-cache
7-
run: npm install @actions/tool-cache@3
87
- name: Check toolcache contains CodeQL
98
continue-on-error: true
10-
uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8.0.0
11-
with:
12-
script: |
13-
const toolcache = require('@actions/tool-cache');
14-
const allCodeqlVersions = toolcache.findAllVersions('CodeQL');
15-
if (allCodeqlVersions.length === 0) {
16-
throw new Error(`CodeQL could not be found in the toolcache`);
17-
}
9+
run: |
10+
cd ./../action
11+
npx tsx ./pr-checks/toolcache.ts installed
12+
1813
- id: setup-codeql
1914
uses: ./../action/setup-codeql
2015
with:
2116
tools: ${{ steps.prepare-test.outputs.tools-url }}
17+
2218
- name: Check CodeQL is installed within the toolcache
23-
uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8.0.0
24-
with:
25-
script: |
26-
const toolcache = require('@actions/tool-cache');
27-
const allCodeqlVersions = toolcache.findAllVersions('CodeQL');
28-
console.log(`Found CodeQL versions: ${allCodeqlVersions}`);
29-
if (allCodeqlVersions.length === 0) {
30-
throw new Error('CodeQL not found in toolcache');
31-
}
19+
run: |
20+
cd ./../action
21+
npx tsx ./pr-checks/toolcache.ts installed

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

Lines changed: 14 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,47 +6,31 @@ 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: |
13+
cd ./../action
14+
npx tsx ./pr-checks/toolcache.ts clear
15+
2016
- 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');
17+
run: |
18+
cd ./../action
19+
npx tsx ./pr-checks/toolcache.ts absent
20+
3021
- id: init
3122
uses: ./../action/init
3223
with:
3324
# Request multiple languages so this check uses the combined bundle.
3425
languages: javascript,python
3526
tools: ${{ steps.prepare-test.outputs.tools-url }}
27+
3628
- uses: ./../action/analyze
3729
with:
3830
output: ${{ runner.temp }}/results
3931
upload-database: false
32+
4033
- 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-
}
34+
run: |
35+
cd ./../action
36+
npx tsx ./pr-checks/toolcache.ts installed -n 1

‎pr-checks/package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"dependencies": {
66
"@actions/core": "^2.0.3",
77
"@actions/github": "^8.0.1",
8+
"@actions/tool-cache": "^3.0.1",
89
"@octokit/core": "^7.0.8",
910
"@octokit/plugin-paginate-rest": ">=16.0.0",
1011
"@octokit/plugin-rest-endpoint-methods": "^18.0.0",

‎pr-checks/toolcache.ts‎

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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.rmSync(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 checkNoCodeQLInToolcache() {
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+
if (allCodeqlVersions.length === 0) {
49+
throw new Error("CodeQL not found in toolcache");
50+
}
51+
52+
core.info(`Found CodeQL versions: ${allCodeqlVersions.join(", ")}`);
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+
"expected-version-count": { 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+
checkNoCodeQLInToolcache();
81+
return;
82+
case "installed":
83+
hasCodeQLInToolcache(values["expected-version-count"]);
84+
return;
85+
default:
86+
throw new Error(`Unknown command '${cmd}'.`);
87+
}
88+
}
89+
90+
try {
91+
main();
92+
} catch (err) {
93+
core.setFailed(err instanceof Error ? err.message : String(err));
94+
}

0 commit comments

Comments
 (0)