Persist PR diff ranges early during init

We don't use them yet and will re-save them during analysis.
This commit is contained in:
Alex Eyers-Taylor
2025-10-08 17:25:52 +01:00
parent 56b69c76aa
commit 9bb8375aed
2 changed files with 600 additions and 395 deletions
+555 -394
View File
File diff suppressed because it is too large Load Diff
+45 -1
View File
@@ -45,7 +45,7 @@ import {
runDatabaseInitCluster,
} from "./init";
import { KnownLanguage } from "./languages";
import { getActionsLogger, Logger } from "./logging";
import { getActionsLogger, Logger, withGroupAsync } from "./logging";
import {
downloadOverlayBaseDatabaseFromCache,
OverlayBaseDatabaseDownloadStats,
@@ -66,6 +66,11 @@ import { ZstdAvailability } from "./tar";
import { ToolsDownloadStatusReport } from "./tools-download";
import { ToolsFeature } from "./tools-features";
import { getCombinedTracerConfig } from "./tracer-config";
import {
getPullRequestEditedDiffRanges,
writeDiffRangesJsonFile,
getDiffInformedAnalysisBranches,
} from "./diff-informed-analysis-utils";
import {
checkDiskUsage,
checkForTimeout,
@@ -336,6 +341,8 @@ async function run() {
});
await checkInstallPython311(config.languages, codeql);
await computeAndPersistDiffRangesEarly(codeql, features, logger);
} catch (unwrappedError) {
const error = wrapError(unwrappedError);
core.setFailed(error.message);
@@ -748,6 +755,43 @@ async function run() {
);
}
/**
* Compute and persist diff ranges early during init when diff-informed analysis
* is enabled (feature flag + PR context). This writes the standard pr-diff-range.json
* file for later reuse in the analyze step. Failures are logged but non-fatal.
*/
async function computeAndPersistDiffRangesEarly(
codeql: CodeQL,
features: Features,
logger: Logger,
): Promise<void> {
try {
await withGroupAsync("Compute PR diff ranges", async () => {
const branches = await getDiffInformedAnalysisBranches(
codeql,
features,
logger,
);
if (!branches) {
return;
}
const ranges = await getPullRequestEditedDiffRanges(branches, logger);
if (ranges === undefined) {
return;
}
writeDiffRangesJsonFile(logger, ranges);
const distinctFiles = new Set(ranges.map((r) => r.path)).size;
logger.info(
`Persisted ${ranges.length} diff range(s) across ${distinctFiles} file(s) for reuse during analyze step.`,
);
});
} catch (e) {
logger.warning(
`Failed to compute and persist PR diff ranges early: ${getErrorMessage(e)}`,
);
}
}
function getTrapCachingEnabled(): boolean {
// If the workflow specified something always respect that
const trapCaching = getOptionalInput("trap-caching");