From d18f3acf743dcecd3ca5926b5dc22e4d2838a8b5 Mon Sep 17 00:00:00 2001 From: Kasper Svendsen Date: Tue, 28 Oct 2025 13:11:49 +0100 Subject: [PATCH] Move diff-range extension pack generation into testable function --- lib/analyze-action.js | 45 +++++++++++++++++---------------- src/analyze.ts | 58 ++++++++++++++++++++++++------------------- 2 files changed, 56 insertions(+), 47 deletions(-) diff --git a/lib/analyze-action.js b/lib/analyze-action.js index 06408d9dd..301c3cf89 100644 --- a/lib/analyze-action.js +++ b/lib/analyze-action.js @@ -91324,6 +91324,29 @@ async function setupDiffInformedQueryRun(branches, logger) { } ); } +function diffRangeExtensionPackContents(ranges) { + const header = ` +extensions: + - addsTo: + pack: codeql/util + extensible: restrictAlertsTo + checkPresence: false + data: +`; + let data = ranges.map( + (range) => ( + // Using yaml.dump() with `forceQuotes: true` ensures that all special + // characters are escaped, and that the path is always rendered as a + // quoted string on a single line. + ` - [${dump(range.path, { forceQuotes: true }).trim()}, ${range.startLine}, ${range.endLine}] +` + ) + ).join(""); + if (!data) { + data = ' - ["", 0, 0]\n'; + } + return header + data; +} function writeDiffRangeDataExtensionPack(logger, ranges) { if (ranges === void 0) { return void 0; @@ -91345,27 +91368,7 @@ dataExtensions: - pr-diff-range.yml ` ); - const header = ` -extensions: - - addsTo: - pack: codeql/util - extensible: restrictAlertsTo - checkPresence: false - data: -`; - let data = ranges.map( - (range) => ( - // Using yaml.dump() with `forceQuotes: true` ensures that all special - // characters are escaped, and that the path is always rendered as a - // quoted string on a single line. - ` - [${dump(range.path, { forceQuotes: true }).trim()}, ${range.startLine}, ${range.endLine}] -` - ) - ).join(""); - if (!data) { - data = ' - ["", 0, 0]\n'; - } - const extensionContents = header + data; + const extensionContents = diffRangeExtensionPackContents(ranges); const extensionFilePath = path12.join(diffRangeDir, "pr-diff-range.yml"); fs12.writeFileSync(extensionFilePath, extensionContents); logger.debug( diff --git a/src/analyze.ts b/src/analyze.ts index cd82ad61b..40a637568 100644 --- a/src/analyze.ts +++ b/src/analyze.ts @@ -244,6 +244,37 @@ export async function setupDiffInformedQueryRun( ); } +export function diffRangeExtensionPackContents( + ranges: DiffThunkRange[], +): string { + const header = ` +extensions: + - addsTo: + pack: codeql/util + extensible: restrictAlertsTo + checkPresence: false + data: +`; + + let data = ranges + .map( + (range) => + // Using yaml.dump() with `forceQuotes: true` ensures that all special + // characters are escaped, and that the path is always rendered as a + // quoted string on a single line. + ` - [${yaml.dump(range.path, { forceQuotes: true }).trim()}, ` + + `${range.startLine}, ${range.endLine}]\n`, + ) + .join(""); + if (!data) { + // Ensure that the data extension is not empty, so that a pull request with + // no edited lines would exclude (instead of accepting) all alerts. + data = ' - ["", 0, 0]\n'; + } + + return header + data; +} + /** * Create an extension pack in the temporary directory that contains the file * line ranges that were added or modified in the pull request. @@ -292,32 +323,7 @@ dataExtensions: `, ); - const header = ` -extensions: - - addsTo: - pack: codeql/util - extensible: restrictAlertsTo - checkPresence: false - data: -`; - - let data = ranges - .map( - (range) => - // Using yaml.dump() with `forceQuotes: true` ensures that all special - // characters are escaped, and that the path is always rendered as a - // quoted string on a single line. - ` - [${yaml.dump(range.path, { forceQuotes: true }).trim()}, ` + - `${range.startLine}, ${range.endLine}]\n`, - ) - .join(""); - if (!data) { - // Ensure that the data extension is not empty, so that a pull request with - // no edited lines would exclude (instead of accepting) all alerts. - data = ' - ["", 0, 0]\n'; - } - - const extensionContents = header + data; + const extensionContents = diffRangeExtensionPackContents(ranges); const extensionFilePath = path.join(diffRangeDir, "pr-diff-range.yml"); fs.writeFileSync(extensionFilePath, extensionContents); logger.debug(