diff --git a/src/sarif/index.test.ts b/src/sarif/index.test.ts index 3d41f848b..94d1c5f18 100644 --- a/src/sarif/index.test.ts +++ b/src/sarif/index.test.ts @@ -2,18 +2,9 @@ import * as fs from "fs"; import test from "ava"; -import { - getRecordingLogger, - LoggedMessage, - setupTests, -} from "../testing-utils"; +import { setupTests } from "../testing-utils"; -import { - fixInvalidNotifications, - getToolNames, - SarifLocation, - type SarifFile, -} from "."; +import { getToolNames, type SarifFile } from "."; setupTests(test); @@ -25,64 +16,3 @@ test("getToolNames", (t) => { const toolNames = getToolNames(JSON.parse(input) as SarifFile); t.deepEqual(toolNames, ["CodeQL command-line toolchain", "ESLint"]); }); - -function createMockSarifWithNotification( - locations: SarifLocation[], -): SarifFile { - return { - runs: [ - { - tool: { - driver: { - name: "CodeQL", - }, - }, - invocations: [ - { - toolExecutionNotifications: [ - { - locations, - }, - ], - }, - ], - }, - ], - }; -} - -const stubLocation: SarifLocation = { - physicalLocation: { - artifactLocation: { - uri: "file1", - }, - }, -}; - -test("fixInvalidNotifications leaves notifications with unique locations alone", (t) => { - const messages: LoggedMessage[] = []; - const result = fixInvalidNotifications( - createMockSarifWithNotification([stubLocation]), - getRecordingLogger(messages), - ); - t.deepEqual(result, createMockSarifWithNotification([stubLocation])); - t.is(messages.length, 1); - t.deepEqual(messages[0], { - type: "debug", - message: "No duplicate locations found in SARIF notification objects.", - }); -}); - -test("fixInvalidNotifications removes duplicate locations", (t) => { - const messages: LoggedMessage[] = []; - const result = fixInvalidNotifications( - createMockSarifWithNotification([stubLocation, stubLocation]), - getRecordingLogger(messages), - ); - t.deepEqual(result, createMockSarifWithNotification([stubLocation])); - t.is(messages.length, 1); - t.deepEqual(messages[0], { - type: "info", - message: "Removed 1 duplicate locations from SARIF notification objects.", - }); -}); diff --git a/src/sarif/index.ts b/src/sarif/index.ts index c8811bb42..11f5f6e3d 100644 --- a/src/sarif/index.ts +++ b/src/sarif/index.ts @@ -107,82 +107,6 @@ export function getToolNames(sarif: SarifFile): string[] { return Object.keys(toolNames); } -export function removeDuplicateLocations( - locations: SarifLocation[], -): SarifLocation[] { - const newJsonLocations = new Set(); - return locations.filter((location) => { - const jsonLocation = JSON.stringify(location); - if (!newJsonLocations.has(jsonLocation)) { - newJsonLocations.add(jsonLocation); - return true; - } - return false; - }); -} - -export function fixInvalidNotifications( - sarif: SarifFile, - logger: Logger, -): SarifFile { - if (!Array.isArray(sarif.runs)) { - return sarif; - } - - // Ensure that the array of locations for each SARIF notification contains unique locations. - // This is a workaround for a bug in the CodeQL CLI that causes duplicate locations to be - // emitted in some cases. - let numDuplicateLocationsRemoved = 0; - - const newSarif = { - ...sarif, - runs: sarif.runs.map((run) => { - if ( - run.tool?.driver?.name !== "CodeQL" || - !Array.isArray(run.invocations) - ) { - return run; - } - return { - ...run, - invocations: run.invocations.map((invocation) => { - if (!Array.isArray(invocation.toolExecutionNotifications)) { - return invocation; - } - return { - ...invocation, - toolExecutionNotifications: - invocation.toolExecutionNotifications.map((notification) => { - if (!Array.isArray(notification.locations)) { - return notification; - } - const newLocations = removeDuplicateLocations( - notification.locations, - ); - numDuplicateLocationsRemoved += - notification.locations.length - newLocations.length; - return { - ...notification, - locations: newLocations, - }; - }), - }; - }), - }; - }), - }; - - if (numDuplicateLocationsRemoved > 0) { - logger.info( - `Removed ${numDuplicateLocationsRemoved} duplicate locations from SARIF notification ` + - "objects.", - ); - } else { - logger.debug("No duplicate locations found in SARIF notification objects."); - } - return newSarif; -} - export function readSarifFile(sarifFilePath: string): SarifFile { return JSON.parse(fs.readFileSync(sarifFilePath, "utf8")) as SarifFile; }