diff --git a/lib/analyze-action.js b/lib/analyze-action.js index 1b881eb18..35c36f0fb 100644 --- a/lib/analyze-action.js +++ b/lib/analyze-action.js @@ -112406,23 +112406,24 @@ function readSarifFile(sarifFilePath) { } function combineSarifFiles(sarifFiles, logger) { logger.info(`Loading SARIF file(s)`); - const combinedSarif = { - version: "2.1.0", - runs: [] - }; + const runs = []; + let version = void 0; for (const sarifFile of sarifFiles) { logger.debug(`Loading SARIF file: ${sarifFile}`); - const sarifObject = readSarifFile(sarifFile); - if (combinedSarif.version === null) { - combinedSarif.version = sarifObject.version; - } else if (combinedSarif.version !== sarifObject.version) { + const sarifLog = readSarifFile(sarifFile); + if (version === void 0) { + version = sarifLog.version; + } else if (version !== sarifLog.version) { throw new InvalidSarifUploadError( - `Different SARIF versions encountered: ${combinedSarif.version} and ${sarifObject.version}` + `Different SARIF versions encountered: ${version} and ${sarifLog.version}` ); } - combinedSarif.runs.push(...sarifObject.runs); + runs.push(...sarifLog.runs); } - return combinedSarif; + if (version === void 0) { + version = "2.1.0"; + } + return { version, runs }; } function areAllRunsProducedByCodeQL(sarifLogs) { return sarifLogs.every((sarifLog) => { diff --git a/lib/init-action-post.js b/lib/init-action-post.js index f1fb6991e..a3ad6686e 100644 --- a/lib/init-action-post.js +++ b/lib/init-action-post.js @@ -169484,23 +169484,24 @@ function readSarifFile(sarifFilePath) { } function combineSarifFiles(sarifFiles, logger) { logger.info(`Loading SARIF file(s)`); - const combinedSarif = { - version: "2.1.0", - runs: [] - }; + const runs = []; + let version = void 0; for (const sarifFile of sarifFiles) { logger.debug(`Loading SARIF file: ${sarifFile}`); - const sarifObject = readSarifFile(sarifFile); - if (combinedSarif.version === null) { - combinedSarif.version = sarifObject.version; - } else if (combinedSarif.version !== sarifObject.version) { + const sarifLog = readSarifFile(sarifFile); + if (version === void 0) { + version = sarifLog.version; + } else if (version !== sarifLog.version) { throw new InvalidSarifUploadError( - `Different SARIF versions encountered: ${combinedSarif.version} and ${sarifObject.version}` + `Different SARIF versions encountered: ${version} and ${sarifLog.version}` ); } - combinedSarif.runs.push(...sarifObject.runs); + runs.push(...sarifLog.runs); } - return combinedSarif; + if (version === void 0) { + version = "2.1.0"; + } + return { version, runs }; } function areAllRunsProducedByCodeQL(sarifLogs) { return sarifLogs.every((sarifLog) => { diff --git a/lib/upload-lib.js b/lib/upload-lib.js index edd885103..bf7a38ff4 100644 --- a/lib/upload-lib.js +++ b/lib/upload-lib.js @@ -110293,23 +110293,24 @@ function readSarifFile(sarifFilePath) { } function combineSarifFiles(sarifFiles, logger) { logger.info(`Loading SARIF file(s)`); - const combinedSarif = { - version: "2.1.0", - runs: [] - }; + const runs = []; + let version = void 0; for (const sarifFile of sarifFiles) { logger.debug(`Loading SARIF file: ${sarifFile}`); - const sarifObject = readSarifFile(sarifFile); - if (combinedSarif.version === null) { - combinedSarif.version = sarifObject.version; - } else if (combinedSarif.version !== sarifObject.version) { + const sarifLog = readSarifFile(sarifFile); + if (version === void 0) { + version = sarifLog.version; + } else if (version !== sarifLog.version) { throw new InvalidSarifUploadError( - `Different SARIF versions encountered: ${combinedSarif.version} and ${sarifObject.version}` + `Different SARIF versions encountered: ${version} and ${sarifLog.version}` ); } - combinedSarif.runs.push(...sarifObject.runs); + runs.push(...sarifLog.runs); } - return combinedSarif; + if (version === void 0) { + version = "2.1.0"; + } + return { version, runs }; } function areAllRunsProducedByCodeQL(sarifLogs) { return sarifLogs.every((sarifLog) => { diff --git a/lib/upload-sarif-action.js b/lib/upload-sarif-action.js index 4aa79da84..8029454c1 100644 --- a/lib/upload-sarif-action.js +++ b/lib/upload-sarif-action.js @@ -107503,23 +107503,24 @@ function readSarifFile(sarifFilePath) { } function combineSarifFiles(sarifFiles, logger) { logger.info(`Loading SARIF file(s)`); - const combinedSarif = { - version: "2.1.0", - runs: [] - }; + const runs = []; + let version = void 0; for (const sarifFile of sarifFiles) { logger.debug(`Loading SARIF file: ${sarifFile}`); - const sarifObject = readSarifFile(sarifFile); - if (combinedSarif.version === null) { - combinedSarif.version = sarifObject.version; - } else if (combinedSarif.version !== sarifObject.version) { + const sarifLog = readSarifFile(sarifFile); + if (version === void 0) { + version = sarifLog.version; + } else if (version !== sarifLog.version) { throw new InvalidSarifUploadError( - `Different SARIF versions encountered: ${combinedSarif.version} and ${sarifObject.version}` + `Different SARIF versions encountered: ${version} and ${sarifLog.version}` ); } - combinedSarif.runs.push(...sarifObject.runs); + runs.push(...sarifLog.runs); } - return combinedSarif; + if (version === void 0) { + version = "2.1.0"; + } + return { version, runs }; } function areAllRunsProducedByCodeQL(sarifLogs) { return sarifLogs.every((sarifLog) => { diff --git a/src/sarif/index.ts b/src/sarif/index.ts index 645ca62fe..475110eef 100644 --- a/src/sarif/index.ts +++ b/src/sarif/index.ts @@ -46,27 +46,33 @@ export function combineSarifFiles( logger: Logger, ): sarif.Log { logger.info(`Loading SARIF file(s)`); - const combinedSarif: sarif.Log = { - version: "2.1.0", - runs: [], - }; + const runs: sarif.Run[] = []; + let version: sarif.Log.version | undefined = undefined; for (const sarifFile of sarifFiles) { logger.debug(`Loading SARIF file: ${sarifFile}`); - const sarifObject = readSarifFile(sarifFile); - // Check SARIF version - if (combinedSarif.version === null) { - combinedSarif.version = sarifObject.version; - } else if (combinedSarif.version !== sarifObject.version) { + const sarifLog = readSarifFile(sarifFile); + // If this is the first SARIF file we are reading, store the version from it so that we + // can put it in the combined SARIF. If not, then check that the versions match and + // throw an exception if they do not. + if (version === undefined) { + version = sarifLog.version; + } else if (version !== sarifLog.version) { throw new InvalidSarifUploadError( - `Different SARIF versions encountered: ${combinedSarif.version} and ${sarifObject.version}`, + `Different SARIF versions encountered: ${version} and ${sarifLog.version}`, ); } - combinedSarif.runs.push(...sarifObject.runs); + runs.push(...sarifLog.runs); } - return combinedSarif; + // We can't guarantee that the SARIF files we load will have version properties. As a fallback, + // we set it to the expected version if we didn't find any other. + if (version === undefined) { + version = "2.1.0"; + } + + return { version, runs }; } /**