fix(ci): harden changed extension diff fallback

This commit is contained in:
Peter Steinberger
2026-03-23 04:40:37 +00:00
parent 7909236bd1
commit 7818344f82
3 changed files with 82 additions and 8 deletions

View File

@@ -11,6 +11,15 @@ const __dirname = path.dirname(__filename);
const repoRoot = path.resolve(__dirname, "..");
const pnpm = "pnpm";
function runGit(args, options = {}) {
return execFileSync("git", args, {
cwd: repoRoot,
stdio: ["ignore", "pipe", "pipe"],
encoding: "utf8",
...options,
});
}
function normalizeRelative(inputPath) {
return inputPath.split(path.sep).join("/");
}
@@ -46,16 +55,53 @@ function collectTestFiles(rootPath) {
return results.toSorted((left, right) => left.localeCompare(right));
}
function hasGitCommit(ref) {
if (!ref || /^0+$/.test(ref)) {
return false;
}
try {
runGit(["rev-parse", "--verify", `${ref}^{commit}`]);
return true;
} catch {
return false;
}
}
function resolveChangedPathsBase(params = {}) {
const base = params.base;
const head = params.head ?? "HEAD";
const fallbackBaseRef = params.fallbackBaseRef;
if (hasGitCommit(base)) {
return base;
}
if (fallbackBaseRef) {
const remoteBaseRef = fallbackBaseRef.startsWith("origin/")
? fallbackBaseRef
: `origin/${fallbackBaseRef}`;
if (hasGitCommit(remoteBaseRef)) {
const mergeBase = runGit(["merge-base", remoteBaseRef, head]).trim();
if (hasGitCommit(mergeBase)) {
return mergeBase;
}
}
}
if (!base) {
throw new Error("A git base revision is required to list changed extensions.");
}
throw new Error(`Git base revision is unavailable locally: ${base}`);
}
function listChangedPaths(base, head = "HEAD") {
if (!base) {
throw new Error("A git base revision is required to list changed extensions.");
}
return execFileSync("git", ["diff", "--name-only", base, head], {
cwd: repoRoot,
stdio: ["ignore", "pipe", "pipe"],
encoding: "utf8",
})
return runGit(["diff", "--name-only", base, head])
.split("\n")
.map((line) => line.trim())
.filter((line) => line.length > 0);
@@ -107,9 +153,21 @@ export function detectChangedExtensionIds(changedPaths) {
}
export function listChangedExtensionIds(params = {}) {
const base = params.base;
const head = params.head ?? "HEAD";
return detectChangedExtensionIds(listChangedPaths(base, head));
const unavailableBaseBehavior = params.unavailableBaseBehavior ?? "error";
try {
const base = resolveChangedPathsBase(params);
return detectChangedExtensionIds(listChangedPaths(base, head));
} catch (error) {
if (unavailableBaseBehavior === "all") {
return listAvailableExtensionIds();
}
if (unavailableBaseBehavior === "empty") {
return [];
}
throw error;
}
}
function resolveExtensionDirectory(targetArg, cwd = process.cwd()) {