Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions lib/entry-points.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 44 additions & 0 deletions src/upload-lib.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<sarif.Log>),
{
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<sarif.Log>),
{
message: "Invalid SARIF. Missing 'results' array in run.",
},
);
}
});

test.serial("validateSarifFileSchema - valid", (t) => {
const inputFile = `${__dirname}/../src/testdata/valid-sarif.sarif`;
t.notThrows(() =>
Expand Down
11 changes: 5 additions & 6 deletions src/upload-lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<sarif.Log>): 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.",
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Reduced SARIF upload postprocessing overhead by counting results directly from the existing SARIF object instead of parsing the serialized payload again.
Loading