fix: require-pr-before-stop falsely denies on merged PR with stale origin#112
Conversation
…-before-stop When a PR is merged but origin/main hasn't been fetched, the policy incorrectly denies and asks Claude to create a new PR. Now fetches the base branch ref and re-verifies before denying on MERGED state. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
📝 WalkthroughWalkthroughThis PR fixes the Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
__tests__/hooks/builtin-policies.test.ts (1)
2190-2200: Assert the exact fetch target in the merged-PR tests.These mocks treat any
fetchas refreshingorigin/main, so they won’t catch a regression where the command fetches onlyFETCH_HEAD/uses the wrong refmap. Please assert the fetch updatesrefs/remotes/origin/${baseBranch}explicitly, especially if the production code switches to an explicit refspec.🧪 Example assertion
const result = await policy.fn(ctx); expect(result.decision).toBe("allow"); + expect(execFileSync).toHaveBeenCalledWith( + "git", + ["fetch", "origin", "+refs/heads/main:refs/remotes/origin/main"], + expect.objectContaining({ cwd: "/repo" }), + ); expect(result.reason).toContain("was merged");As per coding guidelines,
**/__tests__/**/*.{ts,tsx,js,jsx}: “Always add unit tests for new behaviour. Place tests in tests/.”Also applies to: 2218-2228, 2245-2251
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@__tests__/hooks/builtin-policies.test.ts` around lines 2190 - 2200, The execFileSync test mock treats any fetch as a refresh and should assert the exact refspec; update the mockImplementation for execFileSync so it only sets the fetched flag when the args include both "fetch" and the explicit ref target (refs/remotes/origin/${baseBranch} or the exact refspec your production code should send), i.e., check args.join(" ") contains the expected refspec before setting fetched = true; apply the same stricter check to the other similar mocks (the ones around lines referencing execFileSync/fetched noted in the comment) so tests will fail if code fetches FETCH_HEAD or the wrong refmap.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@src/hooks/builtin-policies.ts`:
- Around line 1071-1087: The git fetch call using just the branch name doesn't
update refs/remotes/origin/${baseBranch}, causing subsequent execFileSync checks
(the git log that builds freshAhead and the git diff that builds freshDiff) to
operate on a stale remote-tracking ref; change the execFileSync("git", ["fetch",
"origin", baseBranch], ...) invocation to fetch with an explicit refspec (e.g.
refs/heads/${baseBranch}:refs/remotes/origin/${baseBranch}) while preserving
cwd/encoding/timeout so that origin/${baseBranch} is guaranteed to be updated
before the git log and git diff checks that decide whether to call allow(`PR
#${pr.number} ...`).
---
Nitpick comments:
In `@__tests__/hooks/builtin-policies.test.ts`:
- Around line 2190-2200: The execFileSync test mock treats any fetch as a
refresh and should assert the exact refspec; update the mockImplementation for
execFileSync so it only sets the fetched flag when the args include both "fetch"
and the explicit ref target (refs/remotes/origin/${baseBranch} or the exact
refspec your production code should send), i.e., check args.join(" ") contains
the expected refspec before setting fetched = true; apply the same stricter
check to the other similar mocks (the ones around lines referencing
execFileSync/fetched noted in the comment) so tests will fail if code fetches
FETCH_HEAD or the wrong refmap.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: c94c1863-5395-46ce-bc19-bdb5211d137f
📒 Files selected for processing (3)
CHANGELOG.md__tests__/hooks/builtin-policies.test.tssrc/hooks/builtin-policies.ts
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Bare `git fetch origin <branch>` only writes to FETCH_HEAD, not refs/remotes/origin/<branch>. Use explicit refspec to guarantee the remote-tracking ref is updated before re-checking divergence. Also moves changelog entry under 0.0.6-beta.0 for release. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Summary
origin/mainhasn't been fetched since, therequire-pr-before-stoppolicy incorrectly denies and instructs Claude to create a new PRgit log/git diff) compare against a staleorigin/{baseBranch}ref that doesn't reflect the mergegh pr viewreturnsMERGEDstate, fetchorigin/{baseBranch}with an explicit refspec and re-verify the divergence checks before denyingTest plan
🤖 Generated with Claude Code