|
| 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