fix: prepare complete history for conflict repair

This commit is contained in:
lezi-fun committed 2026-08-14 11:57:41 +08:00
1 parent 0e914c6bc6
commit c2f39d452b
6 files changed
+78 -8

No files matched your search

+1 -1
View File
@@ -213,7 +213,7 @@ jobs:
repository: ${{ inputs.repository_owner }}/${{ inputs.repository_name }} repository: ${{ inputs.repository_owner }}/${{ inputs.repository_name }}
ref: refs/pull/${{ inputs.pull_number }}/head ref: refs/pull/${{ inputs.pull_number }}/head
path: pr-worktree path: pr-worktree
fetch-depth: 1 fetch-depth: 0
persist-credentials: false persist-credentials: false
- uses: actions/setup-node@v4 - uses: actions/setup-node@v4
with: with:
+5 -1
View File
@@ -2,5 +2,9 @@ import pino from "pino";
import { config } from "./config.js"; import { config } from "./config.js";
export const logger = pino({ export const logger = pino({
level: config.logLevel level: config.logLevel,
redact: {
paths: ["gitToken", "*.gitToken", "GHBOT_GIT_TOKEN", "*.GHBOT_GIT_TOKEN"],
censor: "[REDACTED]"
}
}); });
+27
View File
@@ -305,6 +305,33 @@ export function buildConflictPushArgs(params: {
]; ];
} }
export function describeConflictResolutionFailure(error: unknown): string {
const message = error instanceof Error ? error.message.toLowerCase() : "";
if (message.includes("refusing to merge unrelated histories") || message.includes("shallow")) {
return "Conflict repair could not prepare the merge because the PR checkout did not contain enough Git history. No commit was pushed.";
}
if (
message.includes("write access to repository not granted") ||
message.includes("permission denied") ||
message.includes("authentication failed") ||
message.includes("status code: 403")
) {
return "GitHub rejected the conflict-resolution push to the contributor fork. Confirm that ‘Allow edits from maintainers’ is still enabled and that the GitHub App can write through that permission. No commit was pushed.";
}
if (
message.includes("force-with-lease") ||
message.includes("stale info") ||
message.includes("fetch first") ||
message.includes("non-fast-forward")
) {
return "The contributor branch changed before the resolved commit could be pushed, so the safe force lease rejected the update. Run /conflict again on the latest head. No commit was pushed.";
}
if (message.includes("final goose confirmation rejected") || message.includes("validation command")) {
return "goose produced a candidate resolution, but the configured validation or final safety confirmation rejected it. No commit was pushed.";
}
return "goose could not safely complete and validate the conflict resolution. The Actions log contains the exact failure stage, and no commit was pushed.";
}
function isSafeGitHubRepository(value: string): boolean { function isSafeGitHubRepository(value: string): boolean {
return value.split("/").length === 2 && value.split("/").every((part) => ( return value.split("/").length === 2 && value.split("/").every((part) => (
part !== "." && part !== ".." && /^[A-Za-z0-9_.-]+$/.test(part) part !== "." && part !== ".." && /^[A-Za-z0-9_.-]+$/.test(part)
+24 -6
View File
@@ -23,7 +23,11 @@ import {
} from "./policy.js"; } from "./policy.js";
import { compactFilesForReview } from "./prompt.js"; import { compactFilesForReview } from "./prompt.js";
import { loadRepositoryKnowledge } from "../repository/knowledge.js"; import { loadRepositoryKnowledge } from "../repository/knowledge.js";
import { canAutoResolveConflicts, resolvePullRequestConflicts } from "./conflictResolver.js"; import {
canAutoResolveConflicts,
describeConflictResolutionFailure,
resolvePullRequestConflicts
} from "./conflictResolver.js";
const reviewer = new GooseReviewer(); const reviewer = new GooseReviewer();
const CHECK_RUN_NAME = "ghbot review"; const CHECK_RUN_NAME = "ghbot review";
@@ -210,13 +214,13 @@ export async function processPullRequest(
return; return;
} }
} catch (error) { } catch (error) {
logger.error({ error, owner, repo, pullNumber }, "Automatic conflict resolution failed safely."); logger.error({ err: error, owner, repo, pullNumber }, "Automatic conflict resolution failed safely.");
await withRetry("github.issues.createComment.conflictResolutionFailed", async () => { await withRetry("github.issues.createComment.conflictResolutionFailed", async () => {
return octokit.rest.issues.createComment({ return octokit.rest.issues.createComment({
owner, owner,
repo, repo,
issue_number: pullNumber, issue_number: pullNumber,
body: "Automated review passed, but goose could not safely resolve and validate the merge conflicts. No conflict-resolution commit was pushed." body: `Automated review passed, but ${describeConflictResolutionFailure(error)}`
}); });
}); });
return; return;
@@ -639,7 +643,14 @@ export async function processConflictComment(
}); });
if (!permission.permission || !new Set(["admin", "maintain", "write"]).has(permission.permission)) { if (!permission.permission || !new Set(["admin", "maintain", "write"]).has(permission.permission)) {
logger.info( logger.info(
{ ...params, commentBody: undefined, permission: permission.permission }, {
owner: params.owner,
repo: params.repo,
pullNumber: params.pullNumber,
commentId: params.commentId,
commenterLogin: params.commenterLogin,
permission: permission.permission
},
"Ignoring conflict command because commenter does not have write permission." "Ignoring conflict command because commenter does not have write permission."
); );
await postPermissionDeniedComment(octokit, { await postPermissionDeniedComment(octokit, {
@@ -720,11 +731,18 @@ export async function processConflictComment(
: "The PR changed or no resolvable conflict remained before push, so no commit was created." : "The PR changed or no resolvable conflict remained before push, so no commit was created."
); );
} catch (error) { } catch (error) {
logger.error({ error, ...params, commentBody: undefined }, "Manual conflict resolution failed safely."); logger.error({
err: error,
owner: params.owner,
repo: params.repo,
pullNumber: params.pullNumber,
commentId: params.commentId,
commenterLogin: params.commenterLogin
}, "Manual conflict resolution failed safely.");
await postConflictCommandComment( await postConflictCommandComment(
octokit, octokit,
params, params,
"goose could not safely resolve and validate the conflicts. No conflict-resolution commit was pushed." describeConflictResolutionFailure(error)
); );
} }
} }
+20
View File
@@ -3,6 +3,7 @@ import test from "node:test";
import { import {
buildConflictPushArgs, buildConflictPushArgs,
canAutoResolveConflicts, canAutoResolveConflicts,
describeConflictResolutionFailure,
diffSnapshotInventories diffSnapshotInventories
} from "../src/review/conflictResolver.js"; } from "../src/review/conflictResolver.js";
@@ -32,6 +33,25 @@ test("only a passing conflicted writable current head is eligible", () => {
assert.equal(canAutoResolveConflicts({ ...eligible, enabled: false }), false); assert.equal(canAutoResolveConflicts({ ...eligible, enabled: false }), false);
}); });
test("conflict failures are actionable without exposing raw command output", () => {
assert.match(
describeConflictResolutionFailure(new Error("fatal: refusing to merge unrelated histories")),
/did not contain enough Git history/
);
assert.match(
describeConflictResolutionFailure(new Error("remote: Write access to repository not granted")),
/Allow edits from maintainers/
);
assert.match(
describeConflictResolutionFailure(new Error("rejected: stale info")),
/Run \/conflict again/
);
assert.doesNotMatch(
describeConflictResolutionFailure(new Error("secret-token-value")),
/secret-token-value/
);
});
test("external fork conflict pushes use a head-SHA force lease", () => { test("external fork conflict pushes use a head-SHA force lease", () => {
assert.deepEqual(buildConflictPushArgs({ assert.deepEqual(buildConflictPushArgs({
baseRepository: "forumlify/public", baseRepository: "forumlify/public",
+1
View File
@@ -7,6 +7,7 @@ test("conflict repair checks out fork heads without persisting GitHub credential
const caller = await fs.readFile(".github/workflows/review.yml", "utf8"); const caller = await fs.readFile(".github/workflows/review.yml", "utf8");
assert.match(workflow, /inputs\.auto_resolve_conflicts == 'true'/); assert.match(workflow, /inputs\.auto_resolve_conflicts == 'true'/);
assert.doesNotMatch(workflow, /pull_head_repository == format\('\{0\}\/\{1\}'/); assert.doesNotMatch(workflow, /pull_head_repository == format\('\{0\}\/\{1\}'/);
assert.match(workflow, /fetch-depth:\s*0/);
assert.doesNotMatch(workflow, /allow-unsafe-pr-checkout:\s*true/); assert.doesNotMatch(workflow, /allow-unsafe-pr-checkout:\s*true/);
assert.match(workflow, /GHBOT_GOOSE_BINARY:\s*\$\{\{ runner\.temp \}\}\/goose\/bin\/goose/); assert.match(workflow, /GHBOT_GOOSE_BINARY:\s*\$\{\{ runner\.temp \}\}\/goose\/bin\/goose/);
assert.match(caller, /pull_head_repository:\s*\$\{\{ github\.event\.pull_request\.head\.repo\.full_name \|\| '' \}\}/); assert.match(caller, /pull_head_repository:\s*\$\{\{ github\.event\.pull_request\.head\.repo\.full_name \|\| '' \}\}/);