Skip to content

Commit 10abe8f

Browse files
committed
Move diff-range extension pack generation into testable function
1 parent 7ecc114 commit 10abe8f

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
@@ -244,6 +244,37 @@ export async function setupDiffInformedQueryRun(
244244
);
245245
}
246246

247+
export function diffRangeExtensionPackContents(
248+
ranges: DiffThunkRange[],
249+
): string {
250+
const header = `
251+
extensions:
252+
- addsTo:
253+
pack: codeql/util
254+
extensible: restrictAlertsTo
255+
checkPresence: false
256+
data:
257+
`;
258+
259+
let data = ranges
260+
.map(
261+
(range) =>
262+
// Using yaml.dump() with `forceQuotes: true` ensures that all special
263+
// characters are escaped, and that the path is always rendered as a
264+
// quoted string on a single line.
265+
` - [${yaml.dump(range.path, { forceQuotes: true }).trim()}, ` +
266+
`${range.startLine}, ${range.endLine}]\n`,
267+
)
268+
.join("");
269+
if (!data) {
270+
// Ensure that the data extension is not empty, so that a pull request with
271+
// no edited lines would exclude (instead of accepting) all alerts.
272+
data = ' - ["", 0, 0]\n';
273+
}
274+
275+
return header + data;
276+
}
277+
247278
/**
248279
* Create an extension pack in the temporary directory that contains the file
249280
* line ranges that were added or modified in the pull request.
@@ -292,32 +323,7 @@ dataExtensions:
292323
`,
293324
);
294325

295-
const header = `
296-
extensions:
297-
- addsTo:
298-
pack: codeql/util
299-
extensible: restrictAlertsTo
300-
checkPresence: false
301-
data:
302-
`;
303-
304-
let data = ranges
305-
.map(
306-
(range) =>
307-
// Using yaml.dump() with `forceQuotes: true` ensures that all special
308-
// characters are escaped, and that the path is always rendered as a
309-
// quoted string on a single line.
310-
` - [${yaml.dump(range.path, { forceQuotes: true }).trim()}, ` +
311-
`${range.startLine}, ${range.endLine}]\n`,
312-
)
313-
.join("");
314-
if (!data) {
315-
// Ensure that the data extension is not empty, so that a pull request with
316-
// no edited lines would exclude (instead of accepting) all alerts.
317-
data = ' - ["", 0, 0]\n';
318-
}
319-
320-
const extensionContents = header + data;
326+
const extensionContents = diffRangeExtensionPackContents(ranges);
321327
const extensionFilePath = path.join(diffRangeDir, "pr-diff-range.yml");
322328
fs.writeFileSync(extensionFilePath, extensionContents);
323329
logger.debug(

0 commit comments

Comments
 (0)