Initialise CodeQL CLI at most once in upload-sarif action

This commit is contained in:
Michael B. Gale
2025-08-06 15:45:32 +01:00
parent e84e781d34
commit 55b6208ff8
6 changed files with 62 additions and 44 deletions

37
lib/upload-lib.js generated
View File

@@ -173,7 +173,7 @@ async function shouldDisableCombineSarifFiles(sarifObjects, githubVersion) {
* Minimally initialises CodeQL if needed to combine SARIF files and CodeQL
* wasn't already initialised before.
*/
async function initCodeQLForUpload(gitHubVersion, features, tempDir, logger) {
async function initCodeQLForUpload(gitHubVersion, features, logger) {
logger.info("Initializing CodeQL since the 'init' Action was not called before this step.");
const apiDetails = {
auth: actionsUtil.getRequiredInput("token"),
@@ -183,14 +183,14 @@ async function initCodeQLForUpload(gitHubVersion, features, tempDir, logger) {
};
const codeQLDefaultVersionInfo = await features.getDefaultCliVersion(gitHubVersion.type);
const initCodeQLResult = await (0, init_1.initCodeQL)(undefined, // There is no tools input on the upload action
apiDetails, tempDir, gitHubVersion.type, codeQLDefaultVersionInfo, features, logger);
apiDetails, actionsUtil.getTemporaryDirectory(), gitHubVersion.type, codeQLDefaultVersionInfo, features, logger);
return initCodeQLResult.codeql;
}
// Takes a list of paths to sarif files and combines them together using the
// CLI `github merge-results` command when all SARIF files are produced by
// CodeQL. Otherwise, it will fall back to combining the files in the action.
// Returns the contents of the combined sarif file.
async function combineSarifFilesUsingCLI(sarifFiles, gitHubVersion, features, logger) {
async function combineSarifFilesUsingCLI(codeQL, sarifFiles, gitHubVersion, logger) {
logger.info("Combining SARIF files using the CodeQL CLI");
const sarifObjects = sarifFiles.map((sarifFile) => {
return JSON.parse(fs.readFileSync(sarifFile, "utf8"));
@@ -209,18 +209,6 @@ async function combineSarifFilesUsingCLI(sarifFiles, gitHubVersion, features, lo
// If not, use the naive method of combining the files.
return combineSarifFiles(sarifFiles, logger);
}
// Initialize CodeQL, either by using the config file from the 'init' step,
// or by initializing it here.
let codeQL;
let tempDir = actionsUtil.getTemporaryDirectory();
const config = await (0, config_utils_1.getConfig)(tempDir, logger);
if (config !== undefined) {
codeQL = await (0, codeql_1.getCodeQL)(config.codeQLCmd);
tempDir = config.tempDir;
}
else {
codeQL = await initCodeQLForUpload(gitHubVersion, features, tempDir, logger);
}
if (!(await codeQL.supportsFeature(tools_features_1.ToolsFeature.SarifMergeRunsFromEqualCategory))) {
await throwIfCombineSarifFilesDisabled(sarifObjects, gitHubVersion);
logger.warning("The CodeQL CLI does not support merging SARIF files. Merging files in the action.");
@@ -230,6 +218,7 @@ async function combineSarifFilesUsingCLI(sarifFiles, gitHubVersion, features, lo
}
return combineSarifFiles(sarifFiles, logger);
}
const tempDir = actionsUtil.getTemporaryDirectory();
const baseTempDir = path.resolve(tempDir, "combined-sarif");
fs.mkdirSync(baseTempDir, { recursive: true });
const outputDirectory = fs.mkdtempSync(path.resolve(baseTempDir, "output-"));
@@ -466,12 +455,12 @@ exports.CodeQualityTarget = {
*/
async function uploadFiles(inputSarifPath, checkoutPath, category, features, logger, uploadTarget) {
const sarifPaths = getSarifFilePaths(inputSarifPath, uploadTarget.sarifPredicate);
return uploadSpecifiedFiles(sarifPaths, checkoutPath, category, features, logger, uploadTarget);
return uploadSpecifiedFiles(undefined, sarifPaths, checkoutPath, category, features, logger, uploadTarget);
}
/**
* Uploads the given array of SARIF files.
*/
async function uploadSpecifiedFiles(sarifPaths, checkoutPath, category, features, logger, uploadTarget = exports.CodeScanningTarget) {
async function uploadSpecifiedFiles(codeQL, sarifPaths, checkoutPath, category, features, logger, uploadTarget = exports.CodeScanningTarget) {
logger.startGroup(`Uploading ${uploadTarget.name} results`);
logger.info(`Processing sarif files: ${JSON.stringify(sarifPaths)}`);
const gitHubVersion = await (0, api_client_1.getGitHubVersion)();
@@ -482,7 +471,19 @@ async function uploadSpecifiedFiles(sarifPaths, checkoutPath, category, features
const parsedSarif = readSarifFile(sarifPath);
validateSarifFileSchema(parsedSarif, sarifPath, logger);
}
sarif = await combineSarifFilesUsingCLI(sarifPaths, gitHubVersion, features, logger);
// Initialize CodeQL, either by using the config file from the 'init' step,
// or by initializing it here if we don't already have an instance.
if (codeQL === undefined) {
const tempDir = actionsUtil.getTemporaryDirectory();
const config = await (0, config_utils_1.getConfig)(tempDir, logger);
if (config !== undefined) {
codeQL = await (0, codeql_1.getCodeQL)(config.codeQLCmd);
}
else {
codeQL = await initCodeQLForUpload(gitHubVersion, features, logger);
}
}
sarif = await combineSarifFilesUsingCLI(codeQL, sarifPaths, gitHubVersion, logger);
}
else {
const sarifPath = sarifPaths[0];