"use strict"; var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; result["default"] = mod; return result; }; Object.defineProperty(exports, "__esModule", { value: true }); const fs = __importStar(require("fs")); const os = __importStar(require("os")); const path = __importStar(require("path")); /** * The URL for github.com. */ exports.GITHUB_DOTCOM_URL = "https://github.com"; /** * Get the extra options for the codeql commands. */ function getExtraOptionsEnvParam() { const varName = "CODEQL_ACTION_EXTRA_OPTIONS"; const raw = process.env[varName]; if (raw === undefined || raw.length === 0) { return {}; } try { return JSON.parse(raw); } catch (e) { throw new Error(`${varName} environment variable is set, but does not contain valid JSON: ${e.message}`); } } exports.getExtraOptionsEnvParam = getExtraOptionsEnvParam; function isLocalRun() { return (!!process.env.CODEQL_LOCAL_RUN && process.env.CODEQL_LOCAL_RUN !== "false" && process.env.CODEQL_LOCAL_RUN !== "0"); } exports.isLocalRun = isLocalRun; /** * Get the array of all the tool names contained in the given sarif contents. * * Returns an array of unique string tool names. */ function getToolNames(sarifContents) { const sarif = JSON.parse(sarifContents); const toolNames = {}; for (const run of sarif.runs || []) { const tool = run.tool || {}; const driver = tool.driver || {}; if (typeof driver.name === "string" && driver.name.length > 0) { toolNames[driver.name] = true; } } return Object.keys(toolNames); } exports.getToolNames = getToolNames; // Creates a random temporary directory, runs the given body, and then deletes the directory. // Mostly intended for use within tests. async function withTmpDir(body) { const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "codeql-action-")); const realSubdir = path.join(tmpDir, "real"); fs.mkdirSync(realSubdir); const symlinkSubdir = path.join(tmpDir, "symlink"); fs.symlinkSync(realSubdir, symlinkSubdir, "dir"); const result = await body(symlinkSubdir); fs.rmdirSync(tmpDir, { recursive: true }); return result; } exports.withTmpDir = withTmpDir; /** * Get the codeql `--ram` flag as configured by the `ram` input. If no value was * specified, the total available memory will be used minus 256 MB. * * @returns string */ function getMemoryFlag(userInput) { let memoryToUseMegaBytes; if (userInput) { memoryToUseMegaBytes = Number(userInput); if (Number.isNaN(memoryToUseMegaBytes) || memoryToUseMegaBytes <= 0) { throw new Error(`Invalid RAM setting "${userInput}", specified.`); } } else { const totalMemoryBytes = os.totalmem(); const totalMemoryMegaBytes = totalMemoryBytes / (1024 * 1024); const systemReservedMemoryMegaBytes = 256; memoryToUseMegaBytes = totalMemoryMegaBytes - systemReservedMemoryMegaBytes; } return `--ram=${Math.floor(memoryToUseMegaBytes)}`; } exports.getMemoryFlag = getMemoryFlag; /** * Get the codeql flag to specify whether to add code snippets to the sarif file. * * @returns string */ function getAddSnippetsFlag(userInput) { if (typeof userInput === "string") { // have to process specifically because any non-empty string is truthy userInput = userInput.toLowerCase() === "true"; } return userInput ? "--sarif-add-snippets" : "--no-sarif-add-snippets"; } exports.getAddSnippetsFlag = getAddSnippetsFlag; /** * Get the codeql `--threads` value specified for the `threads` input. * If not value was specified, all available threads will be used. * * The value will be capped to the number of available CPUs. * * @returns string */ function getThreadsFlag(userInput, logger) { let numThreads; const maxThreads = os.cpus().length; if (userInput) { numThreads = Number(userInput); if (Number.isNaN(numThreads)) { throw new Error(`Invalid threads setting "${userInput}", specified.`); } if (numThreads > maxThreads) { logger.info(`Clamping desired number of threads (${numThreads}) to max available (${maxThreads}).`); numThreads = maxThreads; } const minThreads = -maxThreads; if (numThreads < minThreads) { logger.info(`Clamping desired number of free threads (${numThreads}) to max available (${minThreads}).`); numThreads = minThreads; } } else { // Default to using all threads numThreads = maxThreads; } return `--threads=${numThreads}`; } exports.getThreadsFlag = getThreadsFlag; /** * Get the directory where CodeQL databases should be placed. */ function getCodeQLDatabasesDir(tempDir) { return path.resolve(tempDir, "codeql_databases"); } exports.getCodeQLDatabasesDir = getCodeQLDatabasesDir; /** * Get the path where the CodeQL database for the given language lives. */ function getCodeQLDatabasePath(tempDir, language) { return path.resolve(getCodeQLDatabasesDir(tempDir), language); } exports.getCodeQLDatabasePath = getCodeQLDatabasePath; //# sourceMappingURL=util.js.map