Files
codeql-action/lib/analysis-paths.js
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

84 lines
4.2 KiB
JavaScript
Raw Permalink Normal View History

"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;
});
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);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
2021-07-27 17:59:59 +01:00
exports.includeAndExcludeAnalysisPaths = exports.printPathFiltersWarning = exports.legalWindowsPathCharactersRegex = void 0;
const path = __importStar(require("path"));
2020-07-07 11:37:56 +01:00
function isInterpretedLanguage(language) {
2021-06-24 18:52:53 +02:00
return (language === "javascript" || language === "python" || language === "ruby");
2020-07-07 11:37:56 +01:00
}
// Matches a string containing only characters that are legal to include in paths on windows.
exports.legalWindowsPathCharactersRegex = /^[^<>:"|?]*$/;
2020-07-08 16:33:00 +01:00
// Builds an environment variable suitable for LGTM_INDEX_INCLUDE or LGTM_INDEX_EXCLUDE
function buildIncludeExcludeEnvVar(paths) {
// Ignore anything containing a *
2020-09-14 10:44:43 +01:00
paths = paths.filter((p) => p.indexOf("*") === -1);
// Some characters are illegal in path names in windows
2020-09-14 10:44:43 +01:00
if (process.platform === "win32") {
paths = paths.filter((p) => p.match(exports.legalWindowsPathCharactersRegex));
}
2020-09-14 10:44:43 +01:00
return paths.join("\n");
2020-07-08 16:33:00 +01:00
}
2020-08-25 16:19:15 +01:00
function printPathFiltersWarning(config, logger) {
// Index include/exclude/filters only work in javascript/python/ruby.
2020-08-25 16:19:15 +01:00
// If any other languages are detected/configured then show a warning.
2020-09-14 10:44:43 +01:00
if ((config.paths.length !== 0 || config.pathsIgnore.length !== 0) &&
2020-08-25 16:19:15 +01:00
!config.languages.every(isInterpretedLanguage)) {
logger.warning('The "paths"/"paths-ignore" fields of the config only have effect for JavaScript, Python, and Ruby');
2020-08-25 16:19:15 +01:00
}
}
exports.printPathFiltersWarning = printPathFiltersWarning;
function includeAndExcludeAnalysisPaths(config) {
2020-07-07 11:37:56 +01:00
// The 'LGTM_INDEX_INCLUDE' and 'LGTM_INDEX_EXCLUDE' environment variables
// control which files/directories are traversed when scanning.
// This allows including files that otherwise would not be scanned, or
// excluding and not traversing entire file subtrees.
2020-07-10 11:08:29 +01:00
// It does not understand globs or double-globs because that would require it to
2020-07-07 11:37:56 +01:00
// traverse the entire file tree to determine which files are matched.
2020-07-10 11:08:29 +01:00
// Any paths containing "*" are not included in these.
if (config.paths.length !== 0) {
2020-09-14 10:44:43 +01:00
process.env["LGTM_INDEX_INCLUDE"] = buildIncludeExcludeEnvVar(config.paths);
}
// If the temporary or tools directory is in the working directory ignore that too.
const tempRelativeToWorking = path.relative(process.cwd(), config.tempDir);
let pathsIgnore = config.pathsIgnore;
if (!tempRelativeToWorking.startsWith("..") &&
!path.isAbsolute(tempRelativeToWorking)) {
pathsIgnore = pathsIgnore.concat(tempRelativeToWorking);
}
if (pathsIgnore.length !== 0) {
process.env["LGTM_INDEX_EXCLUDE"] = buildIncludeExcludeEnvVar(pathsIgnore);
}
2020-07-07 11:37:56 +01:00
// The 'LGTM_INDEX_FILTERS' environment variable controls which files are
// extracted or ignored. It does not control which directories are traversed.
2020-07-10 11:08:29 +01:00
// This does understand the glob and double-glob syntax.
2020-07-07 11:37:56 +01:00
const filters = [];
2020-09-14 10:44:43 +01:00
filters.push(...config.paths.map((p) => `include:${p}`));
filters.push(...config.pathsIgnore.map((p) => `exclude:${p}`));
2020-07-07 11:37:56 +01:00
if (filters.length !== 0) {
2020-09-14 10:44:43 +01:00
process.env["LGTM_INDEX_FILTERS"] = filters.join("\n");
}
}
exports.includeAndExcludeAnalysisPaths = includeAndExcludeAnalysisPaths;
2020-05-13 16:31:24 +01:00
//# sourceMappingURL=analysis-paths.js.map