diff --git a/lib/entry-points.js b/lib/entry-points.js index 0e179f3fc7..7bee9b2bb7 100644 --- a/lib/entry-points.js +++ b/lib/entry-points.js @@ -186700,6 +186700,7 @@ async function uploadBundledDatabase(repositoryNwo, language, commitOid, bundled var upload_lib_exports = {}; __export(upload_lib_exports, { buildPayload: () => buildPayload, + countResultsInSarif: () => countResultsInSarif, filterAlertsByDiffRange: () => filterAlertsByDiffRange, findSarifFilesInDir: () => findSarifFilesInDir, getGroupedSarifFilePaths: () => getGroupedSarifFilePaths, @@ -188393,11 +188394,10 @@ async function getGroupedSarifFilePaths(logger2, sarifPath) { } function countResultsInSarif(sarifLog) { let numResults = 0; - const parsedSarif = JSON.parse(sarifLog); - if (!Array.isArray(parsedSarif.runs)) { + if (!Array.isArray(sarifLog.runs)) { throw new InvalidSarifUploadError("Invalid SARIF. Missing 'runs' array."); } - for (const run9 of parsedSarif.runs) { + for (const run9 of sarifLog.runs) { if (!Array.isArray(run9.results)) { throw new InvalidSarifUploadError( "Invalid SARIF. Missing 'results' array in run." @@ -188598,7 +188598,7 @@ async function uploadPostProcessedFiles(logger2, checkoutPath, uploadTarget, pos logger2.debug(`Raw upload size: ${rawUploadSizeBytes} bytes`); const zippedUploadSizeBytes = zippedSarif.length; logger2.debug(`Base64 zipped upload size: ${zippedUploadSizeBytes} bytes`); - const numResultInSarif = countResultsInSarif(sarifPayload); + const numResultInSarif = countResultsInSarif(sarifLog); logger2.debug(`Number of results in upload: ${numResultInSarif}`); const sarifID = await uploadPayload( payload, diff --git a/src/upload-lib.test.ts b/src/upload-lib.test.ts index 4738c07c62..df7f00d9ec 100644 --- a/src/upload-lib.test.ts +++ b/src/upload-lib.test.ts @@ -24,6 +24,50 @@ test.beforeEach(() => { getEnv().initialise("1.2.3"); }); +test.serial( + "countResultsInSarif counts materialized results across runs", + (t) => { + const tool = { driver: { name: "test" } }; + const result = { message: { text: "Finding" } }; + t.is(uploadLib.countResultsInSarif({ runs: [] }), 0); + t.is( + uploadLib.countResultsInSarif({ + runs: [ + { tool, results: [] }, + { tool, results: [result, result] }, + { tool, results: [result] }, + ], + }), + 3, + ); + }, +); + +test.serial("countResultsInSarif preserves malformed array errors", (t) => { + for (const value of ["{}", '{"runs":null}', '{"runs":{}}']) { + t.throws( + () => + uploadLib.countResultsInSarif(JSON.parse(value) as Partial), + { + message: "Invalid SARIF. Missing 'runs' array.", + }, + ); + } + for (const value of [ + '{"runs":[{}]}', + '{"runs":[{"results":null}]}', + '{"runs":[{"results":{}}]}', + ]) { + t.throws( + () => + uploadLib.countResultsInSarif(JSON.parse(value) as Partial), + { + message: "Invalid SARIF. Missing 'results' array in run.", + }, + ); + } +}); + test.serial("validateSarifFileSchema - valid", (t) => { const inputFile = `${__dirname}/../src/testdata/valid-sarif.sarif`; t.notThrows(() => diff --git a/src/upload-lib.ts b/src/upload-lib.ts index 4e97c1d1db..d3e3645dd2 100644 --- a/src/upload-lib.ts +++ b/src/upload-lib.ts @@ -425,15 +425,14 @@ export async function getGroupedSarifFilePaths( return results; } -// Counts the number of results in the given SARIF file -function countResultsInSarif(sarifLog: string): number { +// Counts the number of results in the given SARIF object. +export function countResultsInSarif(sarifLog: Partial): number { let numResults = 0; - const parsedSarif = JSON.parse(sarifLog); - if (!Array.isArray(parsedSarif.runs)) { + if (!Array.isArray(sarifLog.runs)) { throw new InvalidSarifUploadError("Invalid SARIF. Missing 'runs' array."); } - for (const run of parsedSarif.runs) { + for (const run of sarifLog.runs) { if (!Array.isArray(run.results)) { throw new InvalidSarifUploadError( "Invalid SARIF. Missing 'results' array in run.", @@ -799,7 +798,7 @@ export async function uploadPostProcessedFiles( logger.debug(`Raw upload size: ${rawUploadSizeBytes} bytes`); const zippedUploadSizeBytes = zippedSarif.length; logger.debug(`Base64 zipped upload size: ${zippedUploadSizeBytes} bytes`); - const numResultInSarif = countResultsInSarif(sarifPayload); + const numResultInSarif = countResultsInSarif(sarifLog); logger.debug(`Number of results in upload: ${numResultInSarif}`); // Make the upload diff --git a/unreleased-change-notes/count-materialized-sarif-results.md b/unreleased-change-notes/count-materialized-sarif-results.md new file mode 100644 index 0000000000..411c83db6f --- /dev/null +++ b/unreleased-change-notes/count-materialized-sarif-results.md @@ -0,0 +1 @@ +- Reduced SARIF upload postprocessing overhead by counting results directly from the existing SARIF object instead of parsing the serialized payload again.