refactor: single source of truth for getDiffRangesJsonFilePath and simplified getDiffRangeFilePaths

This commit is contained in:
Sam Robson
2026-03-25 15:42:39 +00:00
parent 521c3536d3
commit d5bb39fa0b
13 changed files with 3929 additions and 3990 deletions

File diff suppressed because it is too large Load Diff

554
lib/analyze-action.js generated

File diff suppressed because it is too large Load Diff

1149
lib/autobuild-action.js generated

File diff suppressed because it is too large Load Diff

1078
lib/init-action-post.js generated

File diff suppressed because it is too large Load Diff

572
lib/init-action.js generated

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -103624,6 +103624,10 @@ function getTemporaryDirectory() {
const value = process.env["CODEQL_ACTION_TEMP"];
return value !== void 0 && value !== "" ? value : getRequiredEnvParam("RUNNER_TEMP");
}
var PR_DIFF_RANGE_JSON_FILENAME = "pr-diff-range.json";
function getDiffRangesJsonFilePath() {
return path2.join(getTemporaryDirectory(), PR_DIFF_RANGE_JSON_FILENAME);
}
function getActionVersion() {
return "4.34.2";
}
@@ -104260,13 +104264,16 @@ function computeChangedFiles(baseFileOids, overlayFileOids) {
return changes;
}
async function getDiffRangeFilePaths(sourceRoot, logger) {
const jsonFilePath = path3.join(getTemporaryDirectory(), "pr-diff-range.json");
if (!fs3.existsSync(jsonFilePath)) {
const jsonFilePath = getDiffRangesJsonFilePath();
let contents;
try {
contents = await fs3.promises.readFile(jsonFilePath, "utf8");
} catch {
return [];
}
let diffRanges;
try {
diffRanges = JSON.parse(fs3.readFileSync(jsonFilePath, "utf8"));
diffRanges = JSON.parse(contents);
} catch (e) {
logger.warning(
`Failed to parse diff ranges JSON file at ${jsonFilePath}: ${e}`
@@ -104276,30 +104283,17 @@ async function getDiffRangeFilePaths(sourceRoot, logger) {
logger.debug(
`Read ${diffRanges.length} diff range(s) from ${jsonFilePath} for overlay changes.`
);
const repoRelativePaths = [...new Set(diffRanges.map((r) => r.path))];
const repoRoot = await getGitRoot(sourceRoot);
if (repoRoot === void 0) {
logger.warning(
"Cannot determine git root; returning diff range paths as-is."
);
return repoRelativePaths;
return [...new Set(diffRanges.map((r) => r.path))];
}
const sourceRootRelPrefix = path3.relative(repoRoot, sourceRoot).replaceAll(path3.sep, "/");
if (sourceRootRelPrefix === "") {
return repoRelativePaths;
}
const prefixWithSlash = `${sourceRootRelPrefix}/`;
const result = [];
for (const p of repoRelativePaths) {
if (p.startsWith(prefixWithSlash)) {
result.push(p.slice(prefixWithSlash.length));
} else {
logger.debug(
`Skipping diff range path "${p}" (not under source root "${sourceRootRelPrefix}").`
);
}
}
return result;
const relativePaths = diffRanges.map(
(r) => path3.relative(sourceRoot, path3.join(repoRoot, r.path)).replaceAll(path3.sep, "/")
).filter((rel) => !rel.startsWith(".."));
return [...new Set(relativePaths)];
}
// src/tools-features.ts

536
lib/upload-lib.js generated

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -53,6 +53,12 @@ export function getTemporaryDirectory(): string {
: getRequiredEnvParam("RUNNER_TEMP");
}
const PR_DIFF_RANGE_JSON_FILENAME = "pr-diff-range.json";
export function getDiffRangesJsonFilePath(): string {
return path.join(getTemporaryDirectory(), PR_DIFF_RANGE_JSON_FILENAME);
}
export function getActionVersion(): string {
return __CODEQL_ACTION_VERSION__;
}

View File

@@ -1,5 +1,4 @@
import * as fs from "fs";
import * as path from "path";
import * as actionsUtil from "./actions-util";
import type { PullRequestBranches } from "./actions-util";
@@ -77,16 +76,12 @@ export interface DiffThunkRange {
endLine: number;
}
function getDiffRangesJsonFilePath(): string {
return path.join(actionsUtil.getTemporaryDirectory(), "pr-diff-range.json");
}
export function writeDiffRangesJsonFile(
logger: Logger,
ranges: DiffThunkRange[],
): void {
const jsonContents = JSON.stringify(ranges, null, 2);
const jsonFilePath = getDiffRangesJsonFilePath();
const jsonFilePath = actionsUtil.getDiffRangesJsonFilePath();
fs.writeFileSync(jsonFilePath, jsonContents);
logger.debug(
`Wrote pr-diff-range JSON file to ${jsonFilePath}:\n${jsonContents}`,
@@ -96,7 +91,7 @@ export function writeDiffRangesJsonFile(
export function readDiffRangesJsonFile(
logger: Logger,
): DiffThunkRange[] | undefined {
const jsonFilePath = getDiffRangesJsonFilePath();
const jsonFilePath = actionsUtil.getDiffRangesJsonFilePath();
if (!fs.existsSync(jsonFilePath)) {
logger.debug(`Diff ranges JSON file does not exist at ${jsonFilePath}`);
return undefined;

View File

@@ -72,9 +72,13 @@ test.serial(
// Write the overlay changes file, which uses the mocked overlay OIDs
// and the base database OIDs file
const diffRangeFilePath = path.join(tempDir, "pr-diff-range.json");
const getTempDirStub = sinon
.stub(actionsUtil, "getTemporaryDirectory")
.returns(tempDir);
const getDiffRangesStub = sinon
.stub(actionsUtil, "getDiffRangesJsonFilePath")
.returns(diffRangeFilePath);
const getGitRootStub = sinon
.stub(gitUtils, "getGitRoot")
.resolves(sourceRoot);
@@ -85,6 +89,7 @@ test.serial(
);
getFileOidsStubForOverlay.restore();
getTempDirStub.restore();
getDiffRangesStub.restore();
getGitRootStub.restore();
const fileContent = await fs.promises.readFile(changesFilePath, "utf-8");
@@ -143,9 +148,13 @@ test.serial(
.stub(gitUtils, "getFileOidsUnderPath")
.resolves(currentOids);
const diffRangeFilePath = path.join(tempDir, "pr-diff-range.json");
const getTempDirStub = sinon
.stub(actionsUtil, "getTemporaryDirectory")
.returns(tempDir);
const getDiffRangesStub = sinon
.stub(actionsUtil, "getDiffRangesJsonFilePath")
.returns(diffRangeFilePath);
const getGitRootStub = sinon
.stub(gitUtils, "getGitRoot")
.resolves(sourceRoot);
@@ -153,7 +162,7 @@ test.serial(
// Write a pr-diff-range.json file with diff ranges including
// "reverted.js" (unchanged OIDs) and "modified.js" (already in OID changes)
await fs.promises.writeFile(
path.join(tempDir, "pr-diff-range.json"),
diffRangeFilePath,
JSON.stringify([
{ path: "reverted.js", startLine: 1, endLine: 10 },
{ path: "modified.js", startLine: 1, endLine: 5 },
@@ -168,6 +177,7 @@ test.serial(
);
getFileOidsStubForOverlay.restore();
getTempDirStub.restore();
getDiffRangesStub.restore();
getGitRootStub.restore();
const fileContent = await fs.promises.readFile(changesFilePath, "utf-8");
@@ -218,9 +228,13 @@ test.serial(
.stub(gitUtils, "getFileOidsUnderPath")
.resolves(currentOids);
const diffRangeFilePath = path.join(tempDir, "pr-diff-range.json");
const getTempDirStub = sinon
.stub(actionsUtil, "getTemporaryDirectory")
.returns(tempDir);
const getDiffRangesStub = sinon
.stub(actionsUtil, "getDiffRangesJsonFilePath")
.returns(diffRangeFilePath);
const getGitRootStub = sinon
.stub(gitUtils, "getGitRoot")
.resolves(sourceRoot);
@@ -233,6 +247,7 @@ test.serial(
);
getFileOidsStubForOverlay.restore();
getTempDirStub.restore();
getDiffRangesStub.restore();
getGitRootStub.restore();
const fileContent = await fs.promises.readFile(changesFilePath, "utf-8");
@@ -286,9 +301,13 @@ test.serial(
.stub(gitUtils, "getFileOidsUnderPath")
.resolves(currentOids);
const diffRangeFilePath = path.join(tempDir, "pr-diff-range.json");
const getTempDirStub = sinon
.stub(actionsUtil, "getTemporaryDirectory")
.returns(tempDir);
const getDiffRangesStub = sinon
.stub(actionsUtil, "getDiffRangesJsonFilePath")
.returns(diffRangeFilePath);
// getGitRoot returns the repo root (parent of sourceRoot)
const getGitRootStub = sinon
.stub(gitUtils, "getGitRoot")
@@ -296,7 +315,7 @@ test.serial(
// Diff ranges use repo-root-relative paths (as returned by the GitHub compare API)
await fs.promises.writeFile(
path.join(tempDir, "pr-diff-range.json"),
diffRangeFilePath,
JSON.stringify([
{ path: "src/app.js", startLine: 1, endLine: 10 },
{ path: "src/lib/util.js", startLine: 5, endLine: 8 },
@@ -311,6 +330,7 @@ test.serial(
);
getFileOidsStubForOverlay.restore();
getTempDirStub.restore();
getDiffRangesStub.restore();
getGitRootStub.restore();
const fileContent = await fs.promises.readFile(changesFilePath, "utf-8");

View File

@@ -3,6 +3,7 @@ import * as path from "path";
import * as actionsCache from "@actions/cache";
import * as actionsUtil from "../actions-util";
import {
getRequiredInput,
getTemporaryDirectory,
@@ -175,15 +176,18 @@ async function getDiffRangeFilePaths(
sourceRoot: string,
logger: Logger,
): Promise<string[]> {
const jsonFilePath = path.join(getTemporaryDirectory(), "pr-diff-range.json");
if (!fs.existsSync(jsonFilePath)) {
const jsonFilePath = actionsUtil.getDiffRangesJsonFilePath();
let contents: string;
try {
contents = await fs.promises.readFile(jsonFilePath, "utf8");
} catch {
return [];
}
let diffRanges: Array<{ path: string }>;
try {
diffRanges = JSON.parse(fs.readFileSync(jsonFilePath, "utf8")) as Array<{
path: string;
}>;
diffRanges = JSON.parse(contents) as Array<{ path: string }>;
} catch (e) {
logger.warning(
`Failed to parse diff ranges JSON file at ${jsonFilePath}: ${e}`,
@@ -193,7 +197,6 @@ async function getDiffRangeFilePaths(
logger.debug(
`Read ${diffRanges.length} diff range(s) from ${jsonFilePath} for overlay changes.`,
);
const repoRelativePaths = [...new Set(diffRanges.map((r) => r.path))];
// Diff-range paths are relative to the repo root (from the GitHub compare
// API), but overlay changed files must be relative to sourceRoot (to match
@@ -203,31 +206,17 @@ async function getDiffRangeFilePaths(
logger.warning(
"Cannot determine git root; returning diff range paths as-is.",
);
return repoRelativePaths;
return [...new Set(diffRanges.map((r) => r.path))];
}
// e.g. if repoRoot=/workspace and sourceRoot=/workspace/src, prefix="src"
const sourceRootRelPrefix = path
.relative(repoRoot, sourceRoot)
.replaceAll(path.sep, "/");
// If sourceRoot IS the repo root, prefix is "" and all paths pass through.
if (sourceRootRelPrefix === "") {
return repoRelativePaths;
}
const prefixWithSlash = `${sourceRootRelPrefix}/`;
const result: string[] = [];
for (const p of repoRelativePaths) {
if (p.startsWith(prefixWithSlash)) {
result.push(p.slice(prefixWithSlash.length));
} else {
logger.debug(
`Skipping diff range path "${p}" (not under source root "${sourceRootRelPrefix}").`,
);
}
}
return result;
const relativePaths = diffRanges
.map((r) =>
path
.relative(sourceRoot, path.join(repoRoot, r.path))
.replaceAll(path.sep, "/"),
)
.filter((rel) => !rel.startsWith(".."));
return [...new Set(relativePaths)];
}
// Constants for database caching