From 77bc2a595e0494042183906e259b153fdcc57556 Mon Sep 17 00:00:00 2001 From: Chuan-kai Lin Date: Fri, 14 Feb 2025 08:50:52 -0800 Subject: [PATCH] Write pr-diff-range JSON file --- src/analyze.ts | 14 ++++++++------ src/diff-filtering-utils.ts | 27 +++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 6 deletions(-) create mode 100644 src/diff-filtering-utils.ts diff --git a/src/analyze.ts b/src/analyze.ts index d21418d42..a3508e545 100644 --- a/src/analyze.ts +++ b/src/analyze.ts @@ -12,6 +12,10 @@ import { setupCppAutobuild } from "./autobuild"; import { CodeQL, getCodeQL } from "./codeql"; import * as configUtils from "./config-utils"; import { addDiagnostic, makeDiagnostic } from "./diagnostics"; +import { + DiffThunkRange, + writeDiffRangesJsonFile, +} from "./diff-filtering-utils"; import { EnvVar } from "./environment"; import { FeatureEnablement, Feature } from "./feature-flags"; import { isScannedLanguage, Language } from "./languages"; @@ -284,12 +288,6 @@ export async function setupDiffInformedQueryRun( ); } -interface DiffThunkRange { - path: string; - startLine: number; - endLine: number; -} - /** * Return the file line ranges that were added or modified in the pull request. * @@ -537,6 +535,10 @@ extensions: `Wrote pr-diff-range extension pack to ${extensionFilePath}:\n${extensionContents}`, ); + // Write the diff ranges to a JSON file, for action-side alert filtering by the + // upload-lib module. + writeDiffRangesJsonFile(logger, ranges); + return diffRangeDir; } diff --git a/src/diff-filtering-utils.ts b/src/diff-filtering-utils.ts new file mode 100644 index 000000000..8cf9c1c8a --- /dev/null +++ b/src/diff-filtering-utils.ts @@ -0,0 +1,27 @@ +import * as fs from "fs"; +import * as path from "path"; + +import * as actionsUtil from "./actions-util"; +import { Logger } from "./logging"; + +export interface DiffThunkRange { + path: string; + startLine: number; + endLine: number; +} + +function getDiffRangesJsonFilePath(): string { + return path.join(actionsUtil.getTemporaryDirectory(), "pr-diff-range.json"); +} + +export function writeDiffRangesJsonFile( + logger: Logger, + ranges: DiffThunkRange[], +): void { + const jsonContents = JSON.stringify(ranges, null, 2); + const jsonFilePath = getDiffRangesJsonFilePath(); + fs.writeFileSync(jsonFilePath, jsonContents); + logger.debug( + `Wrote pr-diff-range JSON file to ${jsonFilePath}:\n${jsonContents}`, + ); +}