Skip to content

Commit d321539

Browse files
committed
WIP: Move generation out absolute paths for diff-informed PR ranges
1 parent 5e51e6e commit d321539

File tree

9 files changed

+43
-63
lines changed

9 files changed

+43
-63
lines changed

lib/analyze-action.js

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

lib/init-action-post.js

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

lib/init-action.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/upload-lib.js

Lines changed: 1 addition & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/upload-sarif-action.js

Lines changed: 1 addition & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/analyze.ts

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import * as io from "@actions/io";
66
import * as del from "del";
77
import * as yaml from "js-yaml";
88

9-
import { getTemporaryDirectory } from "./actions-util";
9+
import { getTemporaryDirectory, getRequiredInput } from "./actions-util";
1010
import * as analyses from "./analyses";
1111
import { setupCppAutobuild } from "./autobuild";
1212
import { type CodeQL } from "./codeql";
@@ -385,14 +385,22 @@ extensions:
385385
`;
386386

387387
let data = ranges
388-
.map(
389-
(range) =>
390-
// Using yaml.dump() with `forceQuotes: true` ensures that all special
391-
// characters are escaped, and that the path is always rendered as a
392-
// quoted string on a single line.
393-
` - [${yaml.dump(range.path, { forceQuotes: true }).trim()}, ` +
394-
`${range.startLine}, ${range.endLine}]\n`,
395-
)
388+
.map((range) => {
389+
// Diff-informed queries expect the file path to be absolute. CodeQL always
390+
// uses forward slashes as the path separator, so on Windows we need to
391+
// replace any backslashes with forward slashes.
392+
const filename = path
393+
.join(getRequiredInput("checkout_path"), range.path)
394+
.replaceAll(path.sep, "/");
395+
396+
// Using yaml.dump() with `forceQuotes: true` ensures that all special
397+
// characters are escaped, and that the path is always rendered as a
398+
// quoted string on a single line.
399+
return (
400+
` - [${yaml.dump(filename, { forceQuotes: true }).trim()}, ` +
401+
`${range.startLine}, ${range.endLine}]\n`
402+
);
403+
})
396404
.join("");
397405
if (!data) {
398406
// Ensure that the data extension is not empty, so that a pull request with

src/diff-informed-analysis-utils.test.ts

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -188,10 +188,6 @@ test(
188188
);
189189

190190
function runGetDiffRanges(changes: number, patch: string[] | undefined): any {
191-
sinon
192-
.stub(actionsUtil, "getRequiredInput")
193-
.withArgs("checkout_path")
194-
.returns("/checkout/path");
195191
return exportedForTesting.getDiffRanges(
196192
{
197193
filename: "test.txt",
@@ -211,7 +207,7 @@ test("getDiffRanges: file diff too large", async (t) => {
211207
const diffRanges = runGetDiffRanges(1000000, undefined);
212208
t.deepEqual(diffRanges, [
213209
{
214-
path: "/checkout/path/test.txt",
210+
path: "test.txt",
215211
startLine: 0,
216212
endLine: 0,
217213
},
@@ -232,7 +228,7 @@ test("getDiffRanges: diff thunk with single addition range", async (t) => {
232228
]);
233229
t.deepEqual(diffRanges, [
234230
{
235-
path: "/checkout/path/test.txt",
231+
path: "test.txt",
236232
startLine: 53,
237233
endLine: 54,
238234
},
@@ -268,7 +264,7 @@ test("getDiffRanges: diff thunk with single update range", async (t) => {
268264
]);
269265
t.deepEqual(diffRanges, [
270266
{
271-
path: "/checkout/path/test.txt",
267+
path: "test.txt",
272268
startLine: 53,
273269
endLine: 53,
274270
},
@@ -290,12 +286,12 @@ test("getDiffRanges: diff thunk with addition ranges", async (t) => {
290286
]);
291287
t.deepEqual(diffRanges, [
292288
{
293-
path: "/checkout/path/test.txt",
289+
path: "test.txt",
294290
startLine: 53,
295291
endLine: 53,
296292
},
297293
{
298-
path: "/checkout/path/test.txt",
294+
path: "test.txt",
299295
startLine: 55,
300296
endLine: 55,
301297
},
@@ -322,12 +318,12 @@ test("getDiffRanges: diff thunk with mixed ranges", async (t) => {
322318
]);
323319
t.deepEqual(diffRanges, [
324320
{
325-
path: "/checkout/path/test.txt",
321+
path: "test.txt",
326322
startLine: 54,
327323
endLine: 54,
328324
},
329325
{
330-
path: "/checkout/path/test.txt",
326+
path: "test.txt",
331327
startLine: 57,
332328
endLine: 58,
333329
},
@@ -357,12 +353,12 @@ test("getDiffRanges: multiple diff thunks", async (t) => {
357353
]);
358354
t.deepEqual(diffRanges, [
359355
{
360-
path: "/checkout/path/test.txt",
356+
path: "test.txt",
361357
startLine: 53,
362358
endLine: 54,
363359
},
364360
{
365-
path: "/checkout/path/test.txt",
361+
path: "test.txt",
366362
startLine: 153,
367363
endLine: 154,
368364
},
@@ -373,7 +369,7 @@ test("getDiffRanges: no diff context lines", async (t) => {
373369
const diffRanges = runGetDiffRanges(2, ["@@ -30 +50,2 @@", "+1", "+2"]);
374370
t.deepEqual(diffRanges, [
375371
{
376-
path: "/checkout/path/test.txt",
372+
path: "test.txt",
377373
startLine: 50,
378374
endLine: 51,
379375
},

src/diff-informed-analysis-utils.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -191,12 +191,7 @@ function getDiffRanges(
191191
fileDiff: FileDiff,
192192
logger: Logger,
193193
): DiffThunkRange[] | undefined {
194-
// Diff-informed queries expect the file path to be absolute. CodeQL always
195-
// uses forward slashes as the path separator, so on Windows we need to
196-
// replace any backslashes with forward slashes.
197-
const filename = path
198-
.join(actionsUtil.getRequiredInput("checkout_path"), fileDiff.filename)
199-
.replaceAll(path.sep, "/");
194+
const filename = fileDiff.filename;
200195

201196
if (fileDiff.patch === undefined) {
202197
if (fileDiff.changes === 0) {

src/upload-lib.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1140,8 +1140,6 @@ function filterAlertsByDiffRange(logger: Logger, sarif: SarifFile): SarifFile {
11401140
return sarif;
11411141
}
11421142

1143-
const checkoutPath = actionsUtil.getRequiredInput("checkout_path");
1144-
11451143
for (const run of sarif.runs) {
11461144
if (run.results) {
11471145
run.results = run.results.filter((result) => {
@@ -1156,19 +1154,14 @@ function filterAlertsByDiffRange(logger: Logger, sarif: SarifFile): SarifFile {
11561154
if (!locationUri || locationStartLine === undefined) {
11571155
return false;
11581156
}
1159-
// CodeQL always uses forward slashes as the path separator, so on Windows we
1160-
// need to replace any backslashes with forward slashes.
1161-
const locationPath = path
1162-
.join(checkoutPath, locationUri)
1163-
.replaceAll(path.sep, "/");
11641157
// Alert filtering here replicates the same behavior as the restrictAlertsTo
11651158
// extensible predicate in CodeQL. See the restrictAlertsTo documentation
11661159
// https://codeql.github.com/codeql-standard-libraries/csharp/codeql/util/AlertFiltering.qll/predicate.AlertFiltering$restrictAlertsTo.3.html
11671160
// for more details, such as why the filtering applies only to the first line
11681161
// of an alert location.
11691162
return diffRanges.some(
11701163
(range) =>
1171-
range.path === locationPath &&
1164+
range.path === locationUri &&
11721165
((range.startLine <= locationStartLine &&
11731166
range.endLine >= locationStartLine) ||
11741167
(range.startLine === 0 && range.endLine === 0)),

0 commit comments

Comments
 (0)