Files
codeql-action/lib/init.js
T

153 lines
7.1 KiB
JavaScript
Raw Normal View History

2020-08-25 16:19:15 +01:00
"use strict";
2021-07-27 17:59:59 +01:00
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
2023-01-18 20:00:33 +00:00
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
2021-07-27 17:59:59 +01:00
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
2020-08-25 16:19:15 +01:00
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
2021-07-27 17:59:59 +01:00
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
2020-08-25 16:19:15 +01:00
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
2023-03-28 18:38:33 +01:00
exports.installPythonDeps = exports.runInit = exports.initConfig = exports.initCodeQL = exports.ToolsSource = void 0;
2020-08-25 16:19:15 +01:00
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
2020-11-20 11:35:59 +01:00
const toolrunner = __importStar(require("@actions/exec/lib/toolrunner"));
const safeWhich = __importStar(require("@chrisgavin/safe-which"));
2020-08-25 16:19:15 +01:00
const analysisPaths = __importStar(require("./analysis-paths"));
const codeql_1 = require("./codeql");
const configUtils = __importStar(require("./config-utils"));
const tracer_config_1 = require("./tracer-config");
const util = __importStar(require("./util"));
var ToolsSource;
(function (ToolsSource) {
ToolsSource["Unknown"] = "UNKNOWN";
ToolsSource["Local"] = "LOCAL";
ToolsSource["Toolcache"] = "TOOLCACHE";
ToolsSource["Download"] = "DOWNLOAD";
})(ToolsSource = exports.ToolsSource || (exports.ToolsSource = {}));
async function initCodeQL(toolsInput, apiDetails, tempDir, variant, defaultCliVersion, logger) {
2020-09-14 10:44:43 +01:00
logger.startGroup("Setup CodeQL tools");
const { codeql, toolsDownloadDurationMs, toolsSource, toolsVersion } = await (0, codeql_1.setupCodeQL)(toolsInput, apiDetails, tempDir, variant, defaultCliVersion, logger, true);
2020-08-25 16:19:15 +01:00
await codeql.printVersion();
logger.endGroup();
return { codeql, toolsDownloadDurationMs, toolsSource, toolsVersion };
2020-08-25 16:19:15 +01:00
}
exports.initCodeQL = initCodeQL;
2023-04-10 07:56:09 +02:00
async function initConfig(languagesInput, queriesInput, packsInput, registriesInput, configFile, dbLocation, configInput, trapCachingEnabled, debugMode, debugArtifactName, debugDatabaseName, repository, tempDir, codeQL, workspacePath, gitHubVersion, apiDetails, features, logger) {
2020-09-14 10:44:43 +01:00
logger.startGroup("Load language configuration");
2023-04-10 07:56:09 +02:00
const config = await configUtils.initConfig(languagesInput, queriesInput, packsInput, registriesInput, configFile, dbLocation, configInput, trapCachingEnabled, debugMode, debugArtifactName, debugDatabaseName, repository, tempDir, codeQL, workspacePath, gitHubVersion, apiDetails, features, logger);
2020-08-25 16:19:15 +01:00
analysisPaths.printPathFiltersWarning(config, logger);
logger.endGroup();
return config;
}
exports.initConfig = initConfig;
2023-03-09 16:46:51 +00:00
async function runInit(codeql, config, sourceRoot, processName, registriesInput, features, apiDetails, logger) {
fs.mkdirSync(config.dbLocation, { recursive: true });
2022-01-18 13:49:24 -08:00
try {
2023-03-28 18:38:33 +01:00
// When parsing the codeql config in the CLI, we have not yet created the qlconfig file.
// So, create it now.
// If we are parsing the config file in the Action, then the qlconfig file was already created
// before the `pack download` command was invoked. It is not required for the init command.
let registriesAuthTokens;
let qlconfigFile;
if (await util.useCodeScanningConfigInCli(codeql, features)) {
({ registriesAuthTokens, qlconfigFile } =
await configUtils.generateRegistries(registriesInput, codeql, config.tempDir, logger));
2022-01-18 13:49:24 -08:00
}
2023-03-28 18:38:33 +01:00
await configUtils.wrapEnvironment({
GITHUB_TOKEN: apiDetails.auth,
CODEQL_REGISTRIES_AUTH: registriesAuthTokens,
},
// Init a database cluster
async () => await codeql.databaseInitCluster(config, sourceRoot, processName, features, qlconfigFile, logger));
}
2022-01-18 13:49:24 -08:00
catch (e) {
throw processError(e);
2020-08-25 16:19:15 +01:00
}
2023-03-28 18:38:33 +01:00
return await (0, tracer_config_1.getCombinedTracerConfig)(config);
}
exports.runInit = runInit;
/**
* Possibly convert this error into a UserError in order to avoid
* counting this error towards our internal error budget.
*
* @param e The error to possibly convert to a UserError.
*
* @returns A UserError if the error is a known error that can be
* attributed to the user, otherwise the original error.
*/
function processError(e) {
if (!(e instanceof Error)) {
return e;
}
if (
// Init action called twice
2023-01-18 20:00:33 +00:00
e.message?.includes("Refusing to create databases") &&
e.message?.includes("exists and is not an empty directory.")) {
return new util.UserError(`Is the "init" action called twice in the same job? ${e.message}`);
}
if (
// Version of CodeQL CLI is incompatible with this version of the CodeQL Action
2023-01-18 20:00:33 +00:00
e.message?.includes("is not compatible with this CodeQL CLI") ||
// Expected source location for database creation does not exist
2023-01-18 20:00:33 +00:00
e.message?.includes("Invalid source root")) {
return new util.UserError(e.message);
}
return e;
}
2020-09-11 10:53:41 +02:00
async function installPythonDeps(codeql, logger) {
2020-09-23 09:27:54 +02:00
logger.startGroup("Setup Python dependencies");
const scriptsFolder = path.resolve(__dirname, "../python-setup");
2021-07-13 08:18:17 +01:00
try {
if (process.platform === "win32") {
await new toolrunner.ToolRunner(await safeWhich.safeWhich("powershell"), [
path.join(scriptsFolder, "install_tools.ps1"),
]).exec();
}
2021-07-13 08:18:17 +01:00
else {
await new toolrunner.ToolRunner(path.join(scriptsFolder, "install_tools.sh")).exec();
}
2020-10-08 12:14:41 +02:00
const script = "auto_install_packages.py";
if (process.platform === "win32") {
2020-11-20 11:35:59 +01:00
await new toolrunner.ToolRunner(await safeWhich.safeWhich("py"), [
2020-10-08 12:14:41 +02:00
"-3",
2022-06-27 16:40:40 +02:00
"-B",
2020-10-08 12:14:41 +02:00
path.join(scriptsFolder, script),
path.dirname(codeql.getPath()),
]).exec();
}
else {
2022-06-27 16:40:40 +02:00
await new toolrunner.ToolRunner(await safeWhich.safeWhich("python3"), [
"-B",
path.join(scriptsFolder, script),
2020-10-08 12:14:41 +02:00
path.dirname(codeql.getPath()),
]).exec();
}
2020-09-11 10:53:41 +02:00
}
catch (e) {
logger.endGroup();
2021-07-13 08:18:17 +01:00
logger.warning(`An error occurred while trying to automatically install Python dependencies: ${e}\n` +
"Please make sure any necessary dependencies are installed before calling the codeql-action/analyze " +
"step, and add a 'setup-python-dependencies: false' argument to this step to disable our automatic " +
"dependency installation and avoid this warning.");
2020-10-08 12:06:29 +02:00
return;
2020-09-11 10:53:41 +02:00
}
logger.endGroup();
}
exports.installPythonDeps = installPythonDeps;
2020-08-25 16:19:15 +01:00
//# sourceMappingURL=init.js.map