Skip to content

Commit

Permalink
Filter alerts by pr-diff-range JSON file
Browse files Browse the repository at this point in the history
  • Loading branch information
cklin committed Feb 14, 2025
1 parent 77bc2a5 commit 3222fc7
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/diff-filtering-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,18 @@ export function writeDiffRangesJsonFile(
`Wrote pr-diff-range JSON file to ${jsonFilePath}:\n${jsonContents}`,
);
}

export function readDiffRangesJsonFile(
logger: Logger,
): DiffThunkRange[] | undefined {
const jsonFilePath = getDiffRangesJsonFilePath();
if (!fs.existsSync(jsonFilePath)) {
logger.debug(`Diff ranges JSON file does not exist at ${jsonFilePath}`);
return undefined;
}
const jsonContents = fs.readFileSync(jsonFilePath, "utf8");
logger.debug(
`Read pr-diff-range JSON file from ${jsonFilePath}:\n${jsonContents}`,
);
return JSON.parse(jsonContents) as DiffThunkRange[];
}
44 changes: 44 additions & 0 deletions src/upload-lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import * as api from "./api-client";
import { getGitHubVersion, wrapApiConfigurationError } from "./api-client";
import { CodeQL, getCodeQL } from "./codeql";
import { getConfig } from "./config-utils";
import { readDiffRangesJsonFile } from "./diff-filtering-utils";
import { EnvVar } from "./environment";
import { FeatureEnablement } from "./feature-flags";
import * as fingerprints from "./fingerprints";
Expand Down Expand Up @@ -578,6 +579,7 @@ export async function uploadFiles(
features,
logger,
);
sarif = filterAlertsByDiffRange(logger, sarif);
sarif = await fingerprints.addFingerprints(sarif, checkoutPath, logger);

const analysisKey = await api.getAnalysisKey();
Expand Down Expand Up @@ -848,3 +850,45 @@ export class InvalidSarifUploadError extends Error {
super(message);
}
}

function filterAlertsByDiffRange(logger: Logger, sarif: SarifFile): SarifFile {
const diffRanges = readDiffRangesJsonFile(logger);
if (!diffRanges) {
return sarif;
}

const checkoutPath = actionsUtil.getRequiredInput("checkout_path");

for (const run of sarif.runs) {
if (run.results) {
run.results = run.results.filter((result) => {
const locations = [
...(result.locations || []).map((loc) => loc.physicalLocation),
...(result.relatedLocations || []).map((loc) => loc.physicalLocation),
];

return locations.some((physicalLocation) => {
const locationUri = physicalLocation?.artifactLocation?.uri;
const locationStartLine = physicalLocation?.region?.startLine;
if (!locationUri || !locationStartLine) {
return false;
}
// CodeQL always uses forward slashes as the path separator, so on Windows we
// need to replace any backslashes with forward slashes.
const locationPath = path
.join(checkoutPath, locationUri)
.replaceAll(path.sep, "/");
return diffRanges.some(
(range) =>
range.path === locationPath &&
((range.startLine <= locationStartLine &&
range.endLine >= locationStartLine) ||
(range.startLine === 0 && range.endLine === 0)),
);
});
});
}
}

return sarif;
}
10 changes: 10 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,16 @@ export interface SarifResult {
};
};
}>;
relatedLocations?: Array<{
physicalLocation: {
artifactLocation: {
uri: string;
};
region?: {
startLine?: number;
};
};
}>;
partialFingerprints: {
primaryLocationLineHash?: string;
};
Expand Down

0 comments on commit 3222fc7

Please sign in to comment.