|
| 1 | +import * as fs from "fs"; |
| 2 | +import path from "path"; |
| 3 | + |
| 4 | +import { getTemporaryDirectory } from "../actions-util"; |
| 5 | +import { Env } from "../environment"; |
| 6 | +import { Logger } from "../logging"; |
| 7 | + |
| 8 | +import type { VersionInfo } from "./types"; |
| 9 | + |
| 10 | +/** |
| 11 | + * The keys of the command cache. Each key corresponds to a command whose output we cache. |
| 12 | + */ |
| 13 | +export type CommandCacheKey = string; |
| 14 | + |
| 15 | +/** |
| 16 | + * The type of the command cache that is persisted to disk. |
| 17 | + */ |
| 18 | +export interface OutputCache { |
| 19 | + cmd: string; |
| 20 | + entries: Record<CommandCacheKey, unknown>; |
| 21 | +} |
| 22 | + |
| 23 | +/** |
| 24 | + * The name of the temporary file that backs the on-disk cache of |
| 25 | + * CLI responses between workflow steps. |
| 26 | + */ |
| 27 | +const COMMAND_CACHE_FILENAME = "codeql-action-command-cache.json"; |
| 28 | + |
| 29 | +/** |
| 30 | + * The module-global variable that caches the CodeQL CLI version in-memory. |
| 31 | + */ |
| 32 | +let cachedCodeQlVersion: undefined | VersionInfo = undefined; |
| 33 | + |
| 34 | +/** |
| 35 | + * Resets the in-process cache of the CodeQL CLI version. Only for use in tests, |
| 36 | + * which exercise multiple "steps" within a single process. |
| 37 | + */ |
| 38 | +export function resetCachedCodeQlVersion(): void { |
| 39 | + cachedCodeQlVersion = undefined; |
| 40 | +} |
| 41 | + |
| 42 | +/** |
| 43 | + * Returns the path to the temporary file that backs the |
| 44 | + * on-disk cache of CLI responses between workflow steps. |
| 45 | + */ |
| 46 | +function getCommandCacheFilePath(env: Env): string { |
| 47 | + return path.join(getTemporaryDirectory(env), COMMAND_CACHE_FILENAME); |
| 48 | +} |
| 49 | + |
| 50 | +/** |
| 51 | + * Caches the CodeQL CLI version both in-memory and on disk. |
| 52 | + * @param env The environment variables to use. |
| 53 | + * @param cmd The path to the CodeQL CLI. |
| 54 | + * @param version The version information to cache. |
| 55 | + */ |
| 56 | +export function cacheCodeQlVersion( |
| 57 | + env: Env, |
| 58 | + cmd: string, |
| 59 | + version: VersionInfo, |
| 60 | +): void { |
| 61 | + if (cachedCodeQlVersion !== undefined) { |
| 62 | + throw new Error("cacheCodeQlVersion() should be called only once"); |
| 63 | + } |
| 64 | + cachedCodeQlVersion = version; |
| 65 | + const outputCache = { |
| 66 | + cmd, |
| 67 | + entries: { version }, |
| 68 | + } satisfies OutputCache; |
| 69 | + // Persist the version so that subsequent Actions steps, which run in separate |
| 70 | + // processes, can reuse it rather than invoking `codeql version` again. We |
| 71 | + // record the CLI path so that a different step using a different CodeQL bundle |
| 72 | + // doesn't pick up a stale version. |
| 73 | + fs.writeFileSync( |
| 74 | + getCommandCacheFilePath(env), |
| 75 | + JSON.stringify(outputCache), |
| 76 | + "utf8", |
| 77 | + ); |
| 78 | +} |
| 79 | + |
| 80 | +/** |
| 81 | + * Returns the cached CodeQL CLI version, if any. |
| 82 | + * @param logger The logger to use for logging messages. |
| 83 | + * @param env The environment variables to use. |
| 84 | + * @param cmd The path to the CodeQL CLI. |
| 85 | + */ |
| 86 | +export function getCachedCodeQlVersion( |
| 87 | + logger: Logger, |
| 88 | + env: Env, |
| 89 | + cmd?: string, |
| 90 | +): undefined | VersionInfo { |
| 91 | + if (cachedCodeQlVersion !== undefined) { |
| 92 | + return cachedCodeQlVersion; |
| 93 | + } |
| 94 | + // Fall back to the value persisted by an earlier Actions step, if any. This is |
| 95 | + // best-effort: any malformed or mismatched value is ignored so that the caller |
| 96 | + // invokes `codeql version` instead. |
| 97 | + let serialized: string; |
| 98 | + try { |
| 99 | + serialized = fs.readFileSync(getCommandCacheFilePath(env), "utf8"); |
| 100 | + } catch (e) { |
| 101 | + logger.debug( |
| 102 | + `Cannot read CLI-cache file ${getCommandCacheFilePath(env)}: ${e}`, |
| 103 | + ); |
| 104 | + return undefined; |
| 105 | + } |
| 106 | + let persisted: unknown; |
| 107 | + try { |
| 108 | + persisted = JSON.parse(serialized); |
| 109 | + } catch (e) { |
| 110 | + logger.debug(`Cannot parse CLI-cache data as JSON: ${e}`); |
| 111 | + return undefined; |
| 112 | + } |
| 113 | + if ( |
| 114 | + !isOutputCache(persisted) || |
| 115 | + (cmd !== undefined && persisted.cmd !== cmd) |
| 116 | + ) { |
| 117 | + return undefined; |
| 118 | + } |
| 119 | + // Memoize the parsed value so that subsequent calls in this process don't |
| 120 | + // re-parse the environment variable. |
| 121 | + cachedCodeQlVersion = persisted.entries.version as VersionInfo; |
| 122 | + return cachedCodeQlVersion; |
| 123 | +} |
| 124 | + |
| 125 | +/** |
| 126 | + * Determines whether a value is a `VersionInfo` object. |
| 127 | + * @param x The value to test |
| 128 | + */ |
| 129 | +function isVersionInfo(x: unknown): x is VersionInfo { |
| 130 | + const candidate = x as Partial<VersionInfo> | null; |
| 131 | + return ( |
| 132 | + typeof candidate === "object" && |
| 133 | + candidate !== null && |
| 134 | + typeof candidate.version === "string" && |
| 135 | + (candidate.features === undefined || |
| 136 | + (typeof candidate.features === "object" && |
| 137 | + candidate.features !== null)) && |
| 138 | + (candidate.overlayVersion === undefined || |
| 139 | + typeof candidate.overlayVersion === "number") |
| 140 | + ); |
| 141 | +} |
| 142 | + |
| 143 | +/** |
| 144 | + * Determines whether a value is a `OutputCache` object. |
| 145 | + * @param x The value to test |
| 146 | + */ |
| 147 | +function isOutputCache(x: unknown): x is OutputCache { |
| 148 | + const candidate = x as Partial<OutputCache> | null; |
| 149 | + return ( |
| 150 | + typeof candidate === "object" && |
| 151 | + candidate !== null && |
| 152 | + typeof candidate.cmd === "string" && |
| 153 | + candidate.entries !== undefined && |
| 154 | + isVersionInfo(candidate.entries.version) |
| 155 | + ); |
| 156 | +} |
0 commit comments