WIP: Move generation out absolute paths for diff-informed PR ranges

This commit is contained in:
Kasper Svendsen
2025-10-28 10:12:55 +01:00
parent 5e51e6e0db
commit d321539bd2
9 changed files with 43 additions and 63 deletions
+6 -12
View File
@@ -93721,15 +93721,11 @@ extensions:
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("");
let data = ranges.map((range) => {
const filename = path16.join(getRequiredInput("checkout_path"), range.path).replaceAll(path16.sep, "/");
return ` - [${dump(filename, { forceQuotes: true }).trim()}, ${range.startLine}, ${range.endLine}]
`;
}).join("");
if (!data) {
data = ' - ["", 0, 0]\n';
}
@@ -96016,7 +96012,6 @@ function filterAlertsByDiffRange(logger, sarif) {
if (!diffRanges?.length) {
return sarif;
}
const checkoutPath = getRequiredInput("checkout_path");
for (const run2 of sarif.runs) {
if (run2.results) {
run2.results = run2.results.filter((result) => {
@@ -96030,9 +96025,8 @@ function filterAlertsByDiffRange(logger, sarif) {
if (!locationUri || locationStartLine === void 0) {
return false;
}
const locationPath = path18.join(checkoutPath, locationUri).replaceAll(path18.sep, "/");
return diffRanges.some(
(range) => range.path === locationPath && (range.startLine <= locationStartLine && range.endLine >= locationStartLine || range.startLine === 0 && range.endLine === 0)
(range) => range.path === locationUri && (range.startLine <= locationStartLine && range.endLine >= locationStartLine || range.startLine === 0 && range.endLine === 0)
);
});
});
+5 -7
View File
@@ -91192,8 +91192,8 @@ var require_primordials = __commonJS({
ArrayPrototypeIndexOf(self2, el) {
return self2.indexOf(el);
},
ArrayPrototypeJoin(self2, sep5) {
return self2.join(sep5);
ArrayPrototypeJoin(self2, sep4) {
return self2.join(sep4);
},
ArrayPrototypeMap(self2, fn) {
return self2.map(fn);
@@ -103080,7 +103080,7 @@ var require_commonjs16 = __commonJS({
*
* @internal
*/
constructor(cwd = process.cwd(), pathImpl, sep5, { nocase, childrenCacheSize = 16 * 1024, fs: fs20 = defaultFS } = {}) {
constructor(cwd = process.cwd(), pathImpl, sep4, { nocase, childrenCacheSize = 16 * 1024, fs: fs20 = defaultFS } = {}) {
this.#fs = fsFromOption(fs20);
if (cwd instanceof URL || cwd.startsWith("file://")) {
cwd = (0, node_url_1.fileURLToPath)(cwd);
@@ -103091,7 +103091,7 @@ var require_commonjs16 = __commonJS({
this.#resolveCache = new ResolveCache();
this.#resolvePosixCache = new ResolveCache();
this.#children = new ChildrenCache(childrenCacheSize);
const split = cwdPath.substring(this.rootPath.length).split(sep5);
const split = cwdPath.substring(this.rootPath.length).split(sep4);
if (split.length === 1 && !split[0]) {
split.pop();
}
@@ -133524,7 +133524,6 @@ function filterAlertsByDiffRange(logger, sarif) {
if (!diffRanges?.length) {
return sarif;
}
const checkoutPath = getRequiredInput("checkout_path");
for (const run2 of sarif.runs) {
if (run2.results) {
run2.results = run2.results.filter((result) => {
@@ -133538,9 +133537,8 @@ function filterAlertsByDiffRange(logger, sarif) {
if (!locationUri || locationStartLine === void 0) {
return false;
}
const locationPath = path17.join(checkoutPath, locationUri).replaceAll(path17.sep, "/");
return diffRanges.some(
(range) => range.path === locationPath && (range.startLine <= locationStartLine && range.endLine >= locationStartLine || range.startLine === 0 && range.endLine === 0)
(range) => range.path === locationUri && (range.startLine <= locationStartLine && range.endLine >= locationStartLine || range.startLine === 0 && range.endLine === 0)
);
});
});
+1 -1
View File
@@ -89093,7 +89093,7 @@ Error Response: ${JSON.stringify(error2.response, null, 2)}`
}
}
function getDiffRanges(fileDiff, logger) {
const filename = path10.join(getRequiredInput("checkout_path"), fileDiff.filename).replaceAll(path10.sep, "/");
const filename = fileDiff.filename;
if (fileDiff.patch === void 0) {
if (fileDiff.changes === 0) {
return [];
+1 -3
View File
@@ -92962,7 +92962,6 @@ function filterAlertsByDiffRange(logger, sarif) {
if (!diffRanges?.length) {
return sarif;
}
const checkoutPath = getRequiredInput("checkout_path");
for (const run of sarif.runs) {
if (run.results) {
run.results = run.results.filter((result) => {
@@ -92976,9 +92975,8 @@ function filterAlertsByDiffRange(logger, sarif) {
if (!locationUri || locationStartLine === void 0) {
return false;
}
const locationPath = path14.join(checkoutPath, locationUri).replaceAll(path14.sep, "/");
return diffRanges.some(
(range) => range.path === locationPath && (range.startLine <= locationStartLine && range.endLine >= locationStartLine || range.startLine === 0 && range.endLine === 0)
(range) => range.path === locationUri && (range.startLine <= locationStartLine && range.endLine >= locationStartLine || range.startLine === 0 && range.endLine === 0)
);
});
});
+1 -3
View File
@@ -93588,7 +93588,6 @@ function filterAlertsByDiffRange(logger, sarif) {
if (!diffRanges?.length) {
return sarif;
}
const checkoutPath = getRequiredInput("checkout_path");
for (const run2 of sarif.runs) {
if (run2.results) {
run2.results = run2.results.filter((result) => {
@@ -93602,9 +93601,8 @@ function filterAlertsByDiffRange(logger, sarif) {
if (!locationUri || locationStartLine === void 0) {
return false;
}
const locationPath = path15.join(checkoutPath, locationUri).replaceAll(path15.sep, "/");
return diffRanges.some(
(range) => range.path === locationPath && (range.startLine <= locationStartLine && range.endLine >= locationStartLine || range.startLine === 0 && range.endLine === 0)
(range) => range.path === locationUri && (range.startLine <= locationStartLine && range.endLine >= locationStartLine || range.startLine === 0 && range.endLine === 0)
);
});
});
+17 -9
View File
@@ -6,7 +6,7 @@ import * as io from "@actions/io";
import * as del from "del";
import * as yaml from "js-yaml";
import { getTemporaryDirectory } from "./actions-util";
import { getTemporaryDirectory, getRequiredInput } from "./actions-util";
import * as analyses from "./analyses";
import { setupCppAutobuild } from "./autobuild";
import { type CodeQL } from "./codeql";
@@ -385,14 +385,22 @@ extensions:
`;
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`,
)
.map((range) => {
// Diff-informed queries expect the file path to be absolute. CodeQL always
// uses forward slashes as the path separator, so on Windows we need to
// replace any backslashes with forward slashes.
const filename = path
.join(getRequiredInput("checkout_path"), range.path)
.replaceAll(path.sep, "/");
// 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.
return (
` - [${yaml.dump(filename, { 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
+10 -14
View File
@@ -188,10 +188,6 @@ test(
);
function runGetDiffRanges(changes: number, patch: string[] | undefined): any {
sinon
.stub(actionsUtil, "getRequiredInput")
.withArgs("checkout_path")
.returns("/checkout/path");
return exportedForTesting.getDiffRanges(
{
filename: "test.txt",
@@ -211,7 +207,7 @@ test("getDiffRanges: file diff too large", async (t) => {
const diffRanges = runGetDiffRanges(1000000, undefined);
t.deepEqual(diffRanges, [
{
path: "/checkout/path/test.txt",
path: "test.txt",
startLine: 0,
endLine: 0,
},
@@ -232,7 +228,7 @@ test("getDiffRanges: diff thunk with single addition range", async (t) => {
]);
t.deepEqual(diffRanges, [
{
path: "/checkout/path/test.txt",
path: "test.txt",
startLine: 53,
endLine: 54,
},
@@ -268,7 +264,7 @@ test("getDiffRanges: diff thunk with single update range", async (t) => {
]);
t.deepEqual(diffRanges, [
{
path: "/checkout/path/test.txt",
path: "test.txt",
startLine: 53,
endLine: 53,
},
@@ -290,12 +286,12 @@ test("getDiffRanges: diff thunk with addition ranges", async (t) => {
]);
t.deepEqual(diffRanges, [
{
path: "/checkout/path/test.txt",
path: "test.txt",
startLine: 53,
endLine: 53,
},
{
path: "/checkout/path/test.txt",
path: "test.txt",
startLine: 55,
endLine: 55,
},
@@ -322,12 +318,12 @@ test("getDiffRanges: diff thunk with mixed ranges", async (t) => {
]);
t.deepEqual(diffRanges, [
{
path: "/checkout/path/test.txt",
path: "test.txt",
startLine: 54,
endLine: 54,
},
{
path: "/checkout/path/test.txt",
path: "test.txt",
startLine: 57,
endLine: 58,
},
@@ -357,12 +353,12 @@ test("getDiffRanges: multiple diff thunks", async (t) => {
]);
t.deepEqual(diffRanges, [
{
path: "/checkout/path/test.txt",
path: "test.txt",
startLine: 53,
endLine: 54,
},
{
path: "/checkout/path/test.txt",
path: "test.txt",
startLine: 153,
endLine: 154,
},
@@ -373,7 +369,7 @@ test("getDiffRanges: no diff context lines", async (t) => {
const diffRanges = runGetDiffRanges(2, ["@@ -30 +50,2 @@", "+1", "+2"]);
t.deepEqual(diffRanges, [
{
path: "/checkout/path/test.txt",
path: "test.txt",
startLine: 50,
endLine: 51,
},
+1 -6
View File
@@ -191,12 +191,7 @@ function getDiffRanges(
fileDiff: FileDiff,
logger: Logger,
): DiffThunkRange[] | undefined {
// Diff-informed queries expect the file path to be absolute. CodeQL always
// uses forward slashes as the path separator, so on Windows we need to
// replace any backslashes with forward slashes.
const filename = path
.join(actionsUtil.getRequiredInput("checkout_path"), fileDiff.filename)
.replaceAll(path.sep, "/");
const filename = fileDiff.filename;
if (fileDiff.patch === undefined) {
if (fileDiff.changes === 0) {
+1 -8
View File
@@ -1140,8 +1140,6 @@ function filterAlertsByDiffRange(logger: Logger, sarif: SarifFile): SarifFile {
return sarif;
}
const checkoutPath = actionsUtil.getRequiredInput("checkout_path");
for (const run of sarif.runs) {
if (run.results) {
run.results = run.results.filter((result) => {
@@ -1156,11 +1154,6 @@ function filterAlertsByDiffRange(logger: Logger, sarif: SarifFile): SarifFile {
if (!locationUri || locationStartLine === undefined) {
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, "/");
// Alert filtering here replicates the same behavior as the restrictAlertsTo
// extensible predicate in CodeQL. See the restrictAlertsTo documentation
// https://codeql.github.com/codeql-standard-libraries/csharp/codeql/util/AlertFiltering.qll/predicate.AlertFiltering$restrictAlertsTo.3.html
@@ -1168,7 +1161,7 @@ function filterAlertsByDiffRange(logger: Logger, sarif: SarifFile): SarifFile {
// of an alert location.
return diffRanges.some(
(range) =>
range.path === locationPath &&
range.path === locationUri &&
((range.startLine <= locationStartLine &&
range.endLine >= locationStartLine) ||
(range.startLine === 0 && range.endLine === 0)),