This commit is contained in:
github-actions[bot] 2024-06-25 09:21:42 +00:00
parent 1895b29ac8
commit 9cf3243b0b
59 changed files with 233 additions and 248 deletions

80
lib/util.js generated
View file

@ -26,7 +26,46 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.BuildMode = exports.checkActionVersion = exports.checkDiskUsage = exports.prettyPrintPack = exports.getErrorMessage = exports.wrapError = exports.fixInvalidNotificationsInFile = exports.fixInvalidNotifications = exports.parseMatrixInput = exports.isHostedRunner = exports.checkForTimeout = exports.withTimeout = exports.tryGetFolderBytes = exports.listFolder = exports.doesDirectoryExist = exports.isInTestMode = exports.isGoodVersion = exports.delay = exports.bundleDb = exports.codeQlVersionAtLeast = exports.getCachedCodeQlVersion = exports.cacheCodeQlVersion = exports.isHTTPError = exports.ConfigurationError = exports.HTTPError = exports.getRequiredEnvParam = exports.initializeEnvironment = exports.assertNever = exports.apiVersionInRange = exports.DisallowedAPIVersionReason = exports.checkGitHubVersionInRange = exports.GitHubVariant = exports.parseGitHubUrl = exports.getCodeQLDatabasePath = exports.getThreadsFlag = exports.getCgroupCpuCountFromCpus = exports.getThreadsFlagValue = exports.getAddSnippetsFlag = exports.getMemoryFlag = exports.getMemoryFlagValue = exports.getMemoryFlagValueForPlatform = exports.withTmpDir = exports.getToolNames = exports.getExtraOptionsEnvParam = exports.DEFAULT_DEBUG_DATABASE_NAME = exports.DEFAULT_DEBUG_ARTIFACT_NAME = exports.GITHUB_DOTCOM_URL = void 0;
exports.BuildMode = exports.ConfigurationError = exports.HTTPError = exports.DisallowedAPIVersionReason = exports.GitHubVariant = exports.DEFAULT_DEBUG_DATABASE_NAME = exports.DEFAULT_DEBUG_ARTIFACT_NAME = exports.GITHUB_DOTCOM_URL = void 0;
exports.getExtraOptionsEnvParam = getExtraOptionsEnvParam;
exports.getToolNames = getToolNames;
exports.withTmpDir = withTmpDir;
exports.getMemoryFlagValueForPlatform = getMemoryFlagValueForPlatform;
exports.getMemoryFlagValue = getMemoryFlagValue;
exports.getMemoryFlag = getMemoryFlag;
exports.getAddSnippetsFlag = getAddSnippetsFlag;
exports.getThreadsFlagValue = getThreadsFlagValue;
exports.getCgroupCpuCountFromCpus = getCgroupCpuCountFromCpus;
exports.getThreadsFlag = getThreadsFlag;
exports.getCodeQLDatabasePath = getCodeQLDatabasePath;
exports.parseGitHubUrl = parseGitHubUrl;
exports.checkGitHubVersionInRange = checkGitHubVersionInRange;
exports.apiVersionInRange = apiVersionInRange;
exports.assertNever = assertNever;
exports.initializeEnvironment = initializeEnvironment;
exports.getRequiredEnvParam = getRequiredEnvParam;
exports.isHTTPError = isHTTPError;
exports.cacheCodeQlVersion = cacheCodeQlVersion;
exports.getCachedCodeQlVersion = getCachedCodeQlVersion;
exports.codeQlVersionAtLeast = codeQlVersionAtLeast;
exports.bundleDb = bundleDb;
exports.delay = delay;
exports.isGoodVersion = isGoodVersion;
exports.isInTestMode = isInTestMode;
exports.doesDirectoryExist = doesDirectoryExist;
exports.listFolder = listFolder;
exports.tryGetFolderBytes = tryGetFolderBytes;
exports.withTimeout = withTimeout;
exports.checkForTimeout = checkForTimeout;
exports.isHostedRunner = isHostedRunner;
exports.parseMatrixInput = parseMatrixInput;
exports.fixInvalidNotifications = fixInvalidNotifications;
exports.fixInvalidNotificationsInFile = fixInvalidNotificationsInFile;
exports.wrapError = wrapError;
exports.getErrorMessage = getErrorMessage;
exports.prettyPrintPack = prettyPrintPack;
exports.checkDiskUsage = checkDiskUsage;
exports.checkActionVersion = checkActionVersion;
const fs = __importStar(require("fs"));
const os = __importStar(require("os"));
const path = __importStar(require("path"));
@ -81,7 +120,6 @@ function getExtraOptionsEnvParam() {
throw new ConfigurationError(`${varName} environment variable is set, but does not contain valid JSON: ${error.message}`);
}
}
exports.getExtraOptionsEnvParam = getExtraOptionsEnvParam;
/**
* Get the array of all the tool names contained in the given sarif contents.
*
@ -98,7 +136,6 @@ function getToolNames(sarif) {
}
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) {
@ -107,7 +144,6 @@ async function withTmpDir(body) {
await (0, del_1.default)(tmpDir, { force: true });
return result;
}
exports.withTmpDir = withTmpDir;
/**
* Gets an OS-specific amount of memory (in MB) to reserve for OS processes
* when the user doesn't explicitly specify a memory setting.
@ -152,7 +188,6 @@ function getMemoryFlagValueForPlatform(userInput, totalMemoryBytes, platform) {
}
return Math.floor(memoryToUseMegaBytes);
}
exports.getMemoryFlagValueForPlatform = getMemoryFlagValueForPlatform;
/**
* Get the total amount of memory available to the Action, taking into account constraints imposed
* by cgroups on Linux.
@ -211,7 +246,6 @@ function getCgroupMemoryLimitBytes(limitFile, logger) {
function getMemoryFlagValue(userInput, logger) {
return getMemoryFlagValueForPlatform(userInput, getTotalMemoryBytes(logger), process.platform);
}
exports.getMemoryFlagValue = getMemoryFlagValue;
/**
* Get the codeql `--ram` flag as configured by the `ram` input. If no value was
* specified, the total available memory will be used minus a threshold
@ -223,7 +257,6 @@ function getMemoryFlag(userInput, logger) {
const megabytes = getMemoryFlagValue(userInput, logger);
return `--ram=${megabytes}`;
}
exports.getMemoryFlag = getMemoryFlag;
/**
* Get the codeql flag to specify whether to add code snippets to the sarif file.
*
@ -236,7 +269,6 @@ function getAddSnippetsFlag(userInput) {
}
return userInput ? "--sarif-add-snippets" : "--no-sarif-add-snippets";
}
exports.getAddSnippetsFlag = getAddSnippetsFlag;
/**
* Get the value of the codeql `--threads` flag specified for the `threads`
* input. If no value was specified, all available threads will be used.
@ -280,7 +312,6 @@ function getThreadsFlagValue(userInput, logger) {
}
return numThreads;
}
exports.getThreadsFlagValue = getThreadsFlagValue;
/**
* Gets the number of available cores specified by the cgroup cpu.max file at the given path.
* Format of file: two values, the limit and the duration (period). If the limit is "max" then
@ -334,7 +365,6 @@ function getCgroupCpuCountFromCpus(cpusFile, logger) {
logger.info(`While resolving threads, found a cgroup CPUs file with ${cpuCount} CPUs in ${cpusFile}.`);
return cpuCount;
}
exports.getCgroupCpuCountFromCpus = getCgroupCpuCountFromCpus;
/**
* Get the codeql `--threads` flag specified for the `threads` input.
* If no value was specified, all available threads will be used.
@ -346,14 +376,12 @@ exports.getCgroupCpuCountFromCpus = getCgroupCpuCountFromCpus;
function getThreadsFlag(userInput, logger) {
return `--threads=${getThreadsFlagValue(userInput, logger)}`;
}
exports.getThreadsFlag = getThreadsFlag;
/**
* Get the path where the CodeQL database for the given language lives.
*/
function getCodeQLDatabasePath(config, language) {
return path.resolve(config.dbLocation, language);
}
exports.getCodeQLDatabasePath = getCodeQLDatabasePath;
/**
* Parses user input of a github.com or GHES URL to a canonical form.
* Removes any API prefix or suffix if one is present.
@ -392,7 +420,6 @@ function parseGitHubUrl(inputUrl) {
}
return url.toString();
}
exports.parseGitHubUrl = parseGitHubUrl;
const CODEQL_ACTION_WARNED_ABOUT_VERSION_ENV_VAR = "CODEQL_ACTION_WARNED_ABOUT_VERSION";
let hasBeenWarnedAboutVersion = false;
var GitHubVariant;
@ -415,7 +442,6 @@ function checkGitHubVersionInRange(version, logger) {
hasBeenWarnedAboutVersion = true;
core.exportVariable(CODEQL_ACTION_WARNED_ABOUT_VERSION_ENV_VAR, true);
}
exports.checkGitHubVersionInRange = checkGitHubVersionInRange;
var DisallowedAPIVersionReason;
(function (DisallowedAPIVersionReason) {
DisallowedAPIVersionReason[DisallowedAPIVersionReason["ACTION_TOO_OLD"] = 0] = "ACTION_TOO_OLD";
@ -430,7 +456,6 @@ function apiVersionInRange(version, minimumVersion, maximumVersion) {
}
return undefined;
}
exports.apiVersionInRange = apiVersionInRange;
/**
* This error is used to indicate a runtime failure of an exhaustivity check enforced at compile time.
*/
@ -447,7 +472,6 @@ class ExhaustivityCheckingError extends Error {
function assertNever(value) {
throw new ExhaustivityCheckingError(value);
}
exports.assertNever = assertNever;
/**
* Set some initial environment variables that we can set even without
* knowing what version of CodeQL we're running.
@ -459,7 +483,6 @@ function initializeEnvironment(version) {
core.exportVariable(String(environment_1.EnvVar.FEATURE_WILL_UPLOAD), "true");
core.exportVariable(String(environment_1.EnvVar.VERSION), version);
}
exports.initializeEnvironment = initializeEnvironment;
/**
* Get an environment parameter, but throw an error if it is not set.
*/
@ -470,7 +493,6 @@ function getRequiredEnvParam(paramName) {
}
return value;
}
exports.getRequiredEnvParam = getRequiredEnvParam;
class HTTPError extends Error {
constructor(message, status) {
super(message);
@ -491,7 +513,6 @@ exports.ConfigurationError = ConfigurationError;
function isHTTPError(arg) {
return arg?.status !== undefined && Number.isInteger(arg.status);
}
exports.isHTTPError = isHTTPError;
let cachedCodeQlVersion = undefined;
function cacheCodeQlVersion(version) {
if (cachedCodeQlVersion !== undefined) {
@ -499,15 +520,12 @@ function cacheCodeQlVersion(version) {
}
cachedCodeQlVersion = version;
}
exports.cacheCodeQlVersion = cacheCodeQlVersion;
function getCachedCodeQlVersion() {
return cachedCodeQlVersion;
}
exports.getCachedCodeQlVersion = getCachedCodeQlVersion;
async function codeQlVersionAtLeast(codeql, requiredVersion) {
return semver.gte((await codeql.getVersion()).version, requiredVersion);
}
exports.codeQlVersionAtLeast = codeQlVersionAtLeast;
// Create a bundle for the given DB, if it doesn't already exist
async function bundleDb(config, language, codeql, dbName) {
const databasePath = getCodeQLDatabasePath(config, language);
@ -523,7 +541,6 @@ async function bundleDb(config, language, codeql, dbName) {
await codeql.databaseBundle(databasePath, databaseBundlePath, dbName);
return databaseBundlePath;
}
exports.bundleDb = bundleDb;
/**
* @param milliseconds time to delay
* @param opts options
@ -540,11 +557,9 @@ async function delay(milliseconds, opts) {
}
});
}
exports.delay = delay;
function isGoodVersion(versionSpec) {
return !BROKEN_VERSIONS.includes(versionSpec);
}
exports.isGoodVersion = isGoodVersion;
/*
* Returns whether we are in test mode.
*
@ -553,7 +568,6 @@ exports.isGoodVersion = isGoodVersion;
function isInTestMode() {
return process.env[environment_1.EnvVar.TEST_MODE] === "true";
}
exports.isInTestMode = isInTestMode;
/*
* Returns whether the path in the argument represents an existing directory.
*/
@ -566,7 +580,6 @@ function doesDirectoryExist(dirPath) {
return false;
}
}
exports.doesDirectoryExist = doesDirectoryExist;
/**
* Returns a recursive list of files in a given directory.
*/
@ -586,7 +599,6 @@ function listFolder(dir) {
}
return files;
}
exports.listFolder = listFolder;
/**
* Get the size a folder in bytes. This will log any filesystem errors
* as a warning and then return undefined.
@ -604,7 +616,6 @@ async function tryGetFolderBytes(cacheDir, logger) {
return undefined;
}
}
exports.tryGetFolderBytes = tryGetFolderBytes;
let hadTimeout = false;
/**
* Run a promise for a given amount of time, and if it doesn't resolve within
@ -647,7 +658,6 @@ async function withTimeout(timeoutMs, promise, onTimeout) {
};
return await Promise.race([mainTask(), timeoutTask()]);
}
exports.withTimeout = withTimeout;
/**
* Check if the global hadTimeout variable has been set, and if so then
* exit the process to ensure any background tasks that are still running
@ -661,7 +671,6 @@ async function checkForTimeout() {
process.exit();
}
}
exports.checkForTimeout = checkForTimeout;
/**
* This function implements a heuristic to determine whether the
* runner we are on is hosted by GitHub. It does this by checking
@ -681,14 +690,12 @@ function isHostedRunner() {
// Segment of the path to the tool cache on all hosted runners
process.env["RUNNER_TOOL_CACHE"]?.includes("hostedtoolcache"));
}
exports.isHostedRunner = isHostedRunner;
function parseMatrixInput(matrixInput) {
if (matrixInput === undefined || matrixInput === "null") {
return undefined;
}
return JSON.parse(matrixInput);
}
exports.parseMatrixInput = parseMatrixInput;
function removeDuplicateLocations(locations) {
const newJsonLocations = new Set();
return locations.filter((location) => {
@ -749,7 +756,6 @@ function fixInvalidNotifications(sarif, logger) {
}
return newSarif;
}
exports.fixInvalidNotifications = fixInvalidNotifications;
/**
* Removes duplicates from the sarif file.
*
@ -772,19 +778,15 @@ function fixInvalidNotificationsInFile(inputPath, outputPath, logger) {
fs.writeFileSync(outputPath, JSON.stringify(sarif));
}
}
exports.fixInvalidNotificationsInFile = fixInvalidNotificationsInFile;
function wrapError(error) {
return error instanceof Error ? error : new Error(String(error));
}
exports.wrapError = wrapError;
function getErrorMessage(error) {
return error instanceof Error ? error.toString() : String(error);
}
exports.getErrorMessage = getErrorMessage;
function prettyPrintPack(pack) {
return `${pack.name}${pack.version ? `@${pack.version}` : ""}${pack.path ? `:${pack.path}` : ""}`;
}
exports.prettyPrintPack = prettyPrintPack;
async function checkDiskUsage(logger) {
try {
const diskUsage = await (0, check_disk_space_1.default)(getRequiredEnvParam("GITHUB_WORKSPACE"));
@ -812,7 +814,6 @@ async function checkDiskUsage(logger) {
return undefined;
}
}
exports.checkDiskUsage = checkDiskUsage;
/**
* Prompt the customer to upgrade to CodeQL Action v3, if appropriate.
*
@ -842,7 +843,6 @@ function checkActionVersion(version, githubVersion) {
}
}
}
exports.checkActionVersion = checkActionVersion;
/**
* Supported build modes.
*