Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 43018ce

Browse files
committedJan 13, 2022
Allow duplicate categories in the same validation step
A single SARIF file should be allowed to have duplicated categories.
1 parent 8454e21 commit 43018ce

6 files changed

+24
-5
lines changed
 

‎lib/upload-lib.js

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎lib/upload-lib.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎lib/upload-lib.test.js

+3-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎lib/upload-lib.test.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/upload-lib.test.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -310,8 +310,11 @@ test("validateUniqueCategory for multiple runs", (t) => {
310310
const sarif1 = createMockSarif("abc", "def");
311311
const sarif2 = createMockSarif("ghi", "jkl");
312312

313-
const multiSarif = { runs: [sarif1.runs[0], sarif2.runs[0]] };
313+
// duplicate categories are allowed within the same sarif file
314+
const multiSarif = { runs: [sarif1.runs[0], sarif1.runs[0], sarif2.runs[0]] };
314315
t.notThrows(() => uploadLib.validateUniqueCategory(multiSarif));
316+
317+
// should throw if there are duplicate categories in separate validations
315318
t.throws(() => uploadLib.validateUniqueCategory(sarif1));
316319
t.throws(() => uploadLib.validateUniqueCategory(sarif2));
317320
});

‎src/upload-lib.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -485,10 +485,18 @@ export async function waitForProcessing(
485485
export function validateUniqueCategory(sarif: SarifFile): void {
486486
// This check only works on actions as env vars don't persist between calls to the runner
487487
if (util.isActions()) {
488+
// duplicate categories are allowed in the same sarif file
489+
// but not across multiple sarif files
490+
const categories = {} as Record<string, { id?: string; tool?: string }>;
491+
488492
for (const run of sarif.runs) {
489493
const id = run?.automationDetails?.id;
490494
const tool = run.tool?.driver?.name;
491495
const category = `${sanitize(id)}_${sanitize(tool)}`;
496+
categories[category] = { id, tool };
497+
}
498+
499+
for (const [category, { id, tool }] of Object.entries(categories)) {
492500
const sentinelEnvVar = `CODEQL_UPLOAD_SARIF_${category}`;
493501
if (process.env[sentinelEnvVar]) {
494502
throw new Error(
@@ -499,7 +507,7 @@ export function validateUniqueCategory(sarif: SarifFile): void {
499507
}
500508
core.exportVariable(sentinelEnvVar, sentinelEnvVar);
501509
}
502-
}
510+
}
503511
}
504512

505513
/**

0 commit comments

Comments
 (0)
Please sign in to comment.