Files
openclaw/scripts/lib/merge-head-diff-base.mjs
Mason Huang 8b29ff5f16 fix(ci): scope PR merge diff checks to first parent (#90287)
Summary:
- This PR adds opt-in first-parent merge-head diff-base handling for CI changed-scope, changed-lanes, and OpenGrep PR scans, plus synthetic merge coverage and small lint/type cleanups.
- PR surface: Source +6, Tests +204, Config +1, Other +179. Total +390 across 15 files.
- Reproducibility: yes. The synthetic merge tests and PR body live-ref proof show the stale payload-base path can include main-only files, and first-parent mode narrows it to PR-owned paths.

Automerge notes:
- PR branch already contained follow-up commit before automerge: fix(ci): update workflow guard expectations
- PR branch already contained follow-up commit before automerge: fix(ci): resolve plugin guardrail lint failures
- PR branch already contained follow-up commit before automerge: fix(ci): preserve plugin run context typing
- PR branch already contained follow-up commit before automerge: fix(ci): scope PR merge diff checks to first parent

Validation:
- ClawSweeper review passed for head 40235e8c3d.
- Required merge gates passed before the squash merge.

Prepared head SHA: 40235e8c3d
Review: https://github.com/openclaw/openclaw/pull/90287#issuecomment-4621155576

Co-authored-by: Mason Huang <masonxhuang@tencent.com>
Co-authored-by: clawsweeper <274271284+clawsweeper[bot]@users.noreply.github.com>
Co-authored-by: clawsweeper[bot] <274271284+clawsweeper[bot]@users.noreply.github.com>
Approved-by: hxy91819
Co-authored-by: hxy91819 <8814856+hxy91819@users.noreply.github.com>
2026-06-04 17:24:03 +00:00

96 lines
2.2 KiB
JavaScript

import { execFileSync } from "node:child_process";
import { pathToFileURL } from "node:url";
const DEFAULT_GIT_OUTPUT_MAX_BUFFER = 16 * 1024 * 1024;
export function resolveMergeHeadDiffBase({
base,
head = "HEAD",
cwd = process.cwd(),
maxBuffer = DEFAULT_GIT_OUTPUT_MAX_BUFFER,
preferFirstParent = false,
}) {
if (!base) {
return "";
}
if (!preferFirstParent) {
return base;
}
const parents = listCommitParents({ ref: head, cwd, maxBuffer });
if (parents.length < 2) {
return base;
}
const firstParent = resolveCommit({ ref: parents[0], cwd, maxBuffer });
const explicitBase = resolveCommit({ ref: base, cwd, maxBuffer });
if (!firstParent || firstParent === explicitBase) {
return base;
}
return firstParent;
}
function listCommitParents({ ref, cwd, maxBuffer }) {
try {
const output = execFileSync("git", ["rev-list", "--parents", "-n", "1", ref], {
cwd,
stdio: ["ignore", "pipe", "ignore"],
encoding: "utf8",
maxBuffer,
}).trim();
return output.split(/\s+/u).slice(1);
} catch {
return [];
}
}
function resolveCommit({ ref, cwd, maxBuffer }) {
try {
return execFileSync("git", ["rev-parse", "--verify", `${ref}^{commit}`], {
cwd,
stdio: ["ignore", "pipe", "ignore"],
encoding: "utf8",
maxBuffer,
}).trim();
} catch {
return "";
}
}
function parseArgs(argv) {
const args = {
base: "",
head: "HEAD",
preferFirstParent: false,
};
for (let index = 0; index < argv.length; index += 1) {
const arg = argv[index];
if (arg === "--base") {
args.base = argv[index + 1] ?? "";
index += 1;
continue;
}
if (arg === "--head") {
args.head = argv[index + 1] ?? "HEAD";
index += 1;
continue;
}
if (arg === "--prefer-first-parent") {
args.preferFirstParent = true;
}
}
return args;
}
if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) {
const args = parseArgs(process.argv.slice(2));
process.stdout.write(
`${resolveMergeHeadDiffBase({
base: args.base,
head: args.head,
preferFirstParent: args.preferFirstParent,
})}\n`,
);
}