Skip to content

Commit b137068

Browse files
fix: use target default branch in repair plans
1 parent a9b17a1 commit b137068

3 files changed

Lines changed: 68 additions & 4 deletions

File tree

‎src/repair/plan-cluster.ts‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -715,14 +715,21 @@ function branchWriteReason(repo: string, pullRequest: LooseRecord) {
715715
}
716716

717717
function fetchMainBranch(repo: string) {
718-
const branch = ghJson(["api", `repos/${repo}/branches/main`]);
718+
const name = fetchDefaultBranchName(repo);
719+
const branch = ghJson(["api", `repos/${repo}/branches/${encodeURIComponent(name)}`]);
719720
return {
720-
name: "main",
721+
name,
721722
sha: branch.commit?.sha,
722723
url: branch._links?.html,
723724
};
724725
}
725726

727+
function fetchDefaultBranchName(repo: string) {
728+
const repository = ghJson(["api", `repos/${repo}`]);
729+
const branch = String(repository.default_branch ?? "").trim();
730+
return branch || "main";
731+
}
732+
726733
function offlineMainBranch(repo: string) {
727734
return {
728735
name: "main",

‎test/repair/plan-cluster.test.ts‎

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,54 @@ test("plan-cluster carries worker target checkout into artifacts", () => {
4949
assert.equal(fixArtifact.target_checkout, targetCheckout);
5050
});
5151

52+
test("plan-cluster hydrates the repository default branch instead of hard-coding main", () => {
53+
const tmp = fs.mkdtempSync(path.join(os.tmpdir(), "clawsweeper-plan-default-branch-"));
54+
const binDir = path.join(tmp, "bin");
55+
const jobPath = path.join(tmp, "job.md");
56+
const runDir = path.join(tmp, "run");
57+
fs.mkdirSync(binDir);
58+
fs.writeFileSync(path.join(binDir, "gh"), fakeGhScript(), { mode: 0o755 });
59+
60+
fs.writeFileSync(
61+
jobPath,
62+
[
63+
"---",
64+
"repo: openclaw/openclaw",
65+
"cluster_id: issue-implementation-openclaw-openclaw-74134",
66+
"mode: autonomous",
67+
"allowed_actions:",
68+
" - comment",
69+
"canonical:",
70+
" - #74134",
71+
"candidates:",
72+
" - #74134",
73+
"allow_fix_pr: false",
74+
"security_policy: central_security_only",
75+
"security_sensitive: false",
76+
"---",
77+
"Plan this item.",
78+
"",
79+
].join("\n"),
80+
);
81+
82+
execFileSync(process.execPath, ["dist/repair/plan-cluster.js", jobPath, "--run-dir", runDir], {
83+
cwd: process.cwd(),
84+
env: {
85+
...process.env,
86+
PATH: `${binDir}${path.delimiter}${process.env.PATH ?? ""}`,
87+
FAKE_DEFAULT_BRANCH: "master",
88+
},
89+
stdio: "pipe",
90+
});
91+
92+
const clusterPlan = JSON.parse(fs.readFileSync(path.join(runDir, "cluster-plan.json"), "utf8"));
93+
assert.deepEqual(clusterPlan.main, {
94+
name: "master",
95+
sha: "master-sha",
96+
url: "https://github.com/openclaw/openclaw/tree/master",
97+
});
98+
});
99+
52100
test("plan-cluster allows security repair for adopted PR autofix jobs", () => {
53101
const tmp = fs.mkdtempSync(path.join(os.tmpdir(), "clawsweeper-plan-security-autofix-"));
54102
const jobPath = path.join(tmp, "job.md");
@@ -372,8 +420,13 @@ if (args[0] !== "api") {
372420
process.exit(1);
373421
}
374422
const endpoint = args[1];
375-
if (endpoint === "repos/openclaw/openclaw/branches/main") {
376-
write({ commit: { sha: "main-sha" }, _links: { html: "https://github.com/openclaw/openclaw/tree/main" } });
423+
const defaultBranch = process.env.FAKE_DEFAULT_BRANCH || "main";
424+
if (endpoint === "repos/openclaw/openclaw") {
425+
write({ default_branch: defaultBranch });
426+
process.exit(0);
427+
}
428+
if (endpoint === "repos/openclaw/openclaw/branches/" + encodeURIComponent(defaultBranch)) {
429+
write({ commit: { sha: defaultBranch + "-sha" }, _links: { html: "https://github.com/openclaw/openclaw/tree/" + defaultBranch } });
377430
process.exit(0);
378431
}
379432
if (isPaged()) {

‎test/repair/run-worker.test.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ test("run-worker starts Codex in the target checkout when one is available", ()
2424
[
2525
"#!/usr/bin/env node",
2626
"const args = process.argv.slice(2);",
27+
"if (args[0] === 'api' && args[1] === 'repos/openclaw/openclaw') {",
28+
" process.stdout.write(JSON.stringify({ default_branch: 'main' }));",
29+
" process.exit(0);",
30+
"}",
2731
"if (args[0] === 'api' && args[1] === 'repos/openclaw/openclaw/branches/main') {",
2832
" process.stdout.write(JSON.stringify({ commit: { sha: '1111111111111111111111111111111111111111' } }));",
2933
" process.exit(0);",

0 commit comments

Comments
 (0)