Skip to content

Commit 681e323

Browse files
committed
Move diff-range extension pack generation into testable function
1 parent e32c499 commit 681e323

File tree

2 files changed

+56
-47
lines changed

2 files changed

+56
-47
lines changed

lib/analyze-action.js

Lines changed: 24 additions & 21 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/analyze.ts

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,37 @@ export async function setupDiffInformedQueryRun(
308308
);
309309
}
310310

311+
export function diffRangeExtensionPackContents(
312+
ranges: DiffThunkRange[],
313+
): string {
314+
const header = `
315+
extensions:
316+
- addsTo:
317+
pack: codeql/util
318+
extensible: restrictAlertsTo
319+
checkPresence: false
320+
data:
321+
`;
322+
323+
let data = ranges
324+
.map(
325+
(range) =>
326+
// Using yaml.dump() with `forceQuotes: true` ensures that all special
327+
// characters are escaped, and that the path is always rendered as a
328+
// quoted string on a single line.
329+
` - [${yaml.dump(range.path, { forceQuotes: true }).trim()}, ` +
330+
`${range.startLine}, ${range.endLine}]\n`,
331+
)
332+
.join("");
333+
if (!data) {
334+
// Ensure that the data extension is not empty, so that a pull request with
335+
// no edited lines would exclude (instead of accepting) all alerts.
336+
data = ' - ["", 0, 0]\n';
337+
}
338+
339+
return header + data;
340+
}
341+
311342
/**
312343
* Create an extension pack in the temporary directory that contains the file
313344
* line ranges that were added or modified in the pull request.
@@ -356,32 +387,7 @@ dataExtensions:
356387
`,
357388
);
358389

359-
const header = `
360-
extensions:
361-
- addsTo:
362-
pack: codeql/util
363-
extensible: restrictAlertsTo
364-
checkPresence: false
365-
data:
366-
`;
367-
368-
let data = ranges
369-
.map(
370-
(range) =>
371-
// Using yaml.dump() with `forceQuotes: true` ensures that all special
372-
// characters are escaped, and that the path is always rendered as a
373-
// quoted string on a single line.
374-
` - [${yaml.dump(range.path, { forceQuotes: true }).trim()}, ` +
375-
`${range.startLine}, ${range.endLine}]\n`,
376-
)
377-
.join("");
378-
if (!data) {
379-
// Ensure that the data extension is not empty, so that a pull request with
380-
// no edited lines would exclude (instead of accepting) all alerts.
381-
data = ' - ["", 0, 0]\n';
382-
}
383-
384-
const extensionContents = header + data;
390+
const extensionContents = diffRangeExtensionPackContents(ranges);
385391
const extensionFilePath = path.join(diffRangeDir, "pr-diff-range.yml");
386392
fs.writeFileSync(extensionFilePath, extensionContents);
387393
logger.debug(

0 commit comments

Comments
 (0)