Skip to content

Commit f703e30

Browse files
dsymeCopilot
andcommitted
Count SARIF results without reparsing the serialized upload payload
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent c5320e3 commit f703e30

4 files changed

Lines changed: 54 additions & 10 deletions

File tree

‎lib/entry-points.js‎

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/upload-lib.test.ts‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,50 @@ test.beforeEach(() => {
2424
getEnv().initialise("1.2.3");
2525
});
2626

27+
test.serial(
28+
"countResultsInSarif counts materialized results across runs",
29+
(t) => {
30+
const tool = { driver: { name: "test" } };
31+
const result = { message: { text: "Finding" } };
32+
t.is(uploadLib.countResultsInSarif({ runs: [] }), 0);
33+
t.is(
34+
uploadLib.countResultsInSarif({
35+
runs: [
36+
{ tool, results: [] },
37+
{ tool, results: [result, result] },
38+
{ tool, results: [result] },
39+
],
40+
}),
41+
3,
42+
);
43+
},
44+
);
45+
46+
test.serial("countResultsInSarif preserves malformed array errors", (t) => {
47+
for (const value of ["{}", '{"runs":null}', '{"runs":{}}']) {
48+
t.throws(
49+
() =>
50+
uploadLib.countResultsInSarif(JSON.parse(value) as Partial<sarif.Log>),
51+
{
52+
message: "Invalid SARIF. Missing 'runs' array.",
53+
},
54+
);
55+
}
56+
for (const value of [
57+
'{"runs":[{}]}',
58+
'{"runs":[{"results":null}]}',
59+
'{"runs":[{"results":{}}]}',
60+
]) {
61+
t.throws(
62+
() =>
63+
uploadLib.countResultsInSarif(JSON.parse(value) as Partial<sarif.Log>),
64+
{
65+
message: "Invalid SARIF. Missing 'results' array in run.",
66+
},
67+
);
68+
}
69+
});
70+
2771
test.serial("validateSarifFileSchema - valid", (t) => {
2872
const inputFile = `${__dirname}/../src/testdata/valid-sarif.sarif`;
2973
t.notThrows(() =>

‎src/upload-lib.ts‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -425,15 +425,14 @@ export async function getGroupedSarifFilePaths(
425425
return results;
426426
}
427427

428-
// Counts the number of results in the given SARIF file
429-
function countResultsInSarif(sarifLog: string): number {
428+
// Counts the number of results in the given SARIF object.
429+
export function countResultsInSarif(sarifLog: Partial<sarif.Log>): number {
430430
let numResults = 0;
431-
const parsedSarif = JSON.parse(sarifLog);
432-
if (!Array.isArray(parsedSarif.runs)) {
431+
if (!Array.isArray(sarifLog.runs)) {
433432
throw new InvalidSarifUploadError("Invalid SARIF. Missing 'runs' array.");
434433
}
435434

436-
for (const run of parsedSarif.runs) {
435+
for (const run of sarifLog.runs) {
437436
if (!Array.isArray(run.results)) {
438437
throw new InvalidSarifUploadError(
439438
"Invalid SARIF. Missing 'results' array in run.",
@@ -799,7 +798,7 @@ export async function uploadPostProcessedFiles(
799798
logger.debug(`Raw upload size: ${rawUploadSizeBytes} bytes`);
800799
const zippedUploadSizeBytes = zippedSarif.length;
801800
logger.debug(`Base64 zipped upload size: ${zippedUploadSizeBytes} bytes`);
802-
const numResultInSarif = countResultsInSarif(sarifPayload);
801+
const numResultInSarif = countResultsInSarif(sarifLog);
803802
logger.debug(`Number of results in upload: ${numResultInSarif}`);
804803

805804
// Make the upload
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Reduced SARIF upload postprocessing overhead by counting results directly from the existing SARIF object instead of parsing the serialized payload again.

0 commit comments

Comments
 (0)