Skip to content

ci(pr-hygiene): count commits from the merge commit's first parent - #39073

Closed
leonlaiyc wants to merge 1 commit into
kirodotdev:mainfrom
leonlaiyc:claude/pr-hygiene-commit-count-merge-parent
Closed

leonlaiyc wants to merge 1 commit into
kirodotdev:mainfrom
leonlaiyc:claude/pr-hygiene-commit-count-merge-parent

Conversation

@leonlaiyc

Copy link
Copy Markdown
Contributor

Problem / Motivation

Goal: PR Hygiene's commit-count gate counts only the PR's own commits, even when the event's base.sha is older than the base the branch was rebased onto.

On #30034 a one-commit PR failed Enforce commit count with PR must contain 1 or 2 commits (has 62), and re-running gave the same result. The step counted git rev-list --count "$BASE".."$HEAD" with BASE = the event's pull_request.base.sha (758622b385), while the branch sat on bddefa36fb, 61 main commits later. The run's checkout was the current merge commit Merge 51af78ce1d into 654781f6c2; only base.sha was stale.

Why it matters

A contributor who did exactly what the gate asks (rebase, one commit) is told to squash commits that are not theirs, and a re-run cannot clear it because it replays the same event. PR Hygiene is a PR Readiness lane, so the red holds the PR.

Not a goal

  • Not changing the limit (MAX_COMMITS) or the remedy text.
  • Not changing how the diff-base step (code-review-history.sh) resolves its merge-base, or which base the Proof line / Sibling sweep steps read.
  • Not changing the other workflows' use of base.sha.

What changed (motivation → approach → change)

  • Symptom: a rebased one-commit PR counted as 62 commits.
  • Root cause: BASE..HEAD from a stale base.sha includes every main commit between that base.sha and the commit the branch was rebased onto. The ci(review): diff base is the base-branch tip, so gates scan files the PR never touched #4968 note on this step ("a non-ancestor base still yields only the PR's own commits") holds when the base is ahead of the branch, not when the branch is ahead of the base.
  • Change, matching what ci: measure diff-scoped gates from the pr merge commit's first parent #38678 did for the diff-scoped gates: the job already checks out GitHub's merge commit, whose first parent is the base tip it was built on, and the preceding Resolve diff base step has unshallowed the history. When HEAD's second parent is the PR head, the step now counts from that first parent and prints a notice saying so. Otherwise (no merge commit, or a different second parent) it keeps base.sha. git cat-file -p HEAD reads the parents from the commit object, as resolve-i18n-base.sh does. The BASE env comment now says base.sha is the fallback.

Backwards compatibility

Compatible: when base.sha already equals the merge commit's first parent the count is unchanged; when it differs, the first parent is the newer base tip, so the range can only lose main commits that were never the PR's.

Removes nothing: the gate, its limit and its message are unchanged; only the base of the range moves.

Tests

test/test_code_review_history_fetch.py:

  • New world case rebased-past-stale-base: the event's base_tip is the old fork point, the branch has one commit on top of a main that moved 31 commits, and the merge commit's first parent is that moved main.
  • test_the_commit_range_and_the_rule_greps_run_on_the_fetched_history now runs Enforce commit count on that case, handed the stale base.sha as BASE, after the real diff-base step, and asserts it counts 1. The new case also runs through test_every_read_matches_a_full_clone_with_no_further_fetch.

Negative control: with pr-hygiene.yml reverted to main, the test fails with ('rebased-past-stale-base', "::error::PR must contain 1 or 2 commits (has 32). ..."), the #30034 shape.

Proof: not provable -- other: the fix is a workflow shell step, which prove.py does not revert; the manual negative control above is the equivalent.

These tests skip on Windows. I ran them on Windows with the skip lifted, under Git Bash. -k "commit_range or every_read" passes (5 passed) with the fix. The same file fully forced on Windows has 11 unrelated failures (shim and path quoting), identical on unmodified main. Linux CI runs it normally. With the normal skip, test_code_review_history_fetch.py, test_pr_hygiene_guidance.py and the other pr-hygiene workflow test files pass: 1072 passed.

Manual verification

N/A — unit coverage sufficient: the test executes the workflow's own Resolve diff base and Enforce commit count steps against a real shallow merge-commit checkout.

Real-machine verification

Machine: own computer, Windows 11 Home 10.0.26200, x86_64, Git Bash
Before (main ade7524): pytest test/test_code_review_history_fetch.py -k commit_range (Windows skip lifted) -> PR must contain 1 or 2 commits (has 32) for rebased-past-stale-base, 1 failed
After (head 438b937): same command -> passed
Cleanup: own computer

Related Issues

Fixes #39016

Checklist

  • At most two commits (one is the norm), with a Conventional Commits title (feat|fix|docs|style|refactor|perf|test|chore|ci|build|revert: ...)
  • Existing tests pass and new tests added for new functionality
  • Self-review completed; code follows project style guidelines
  • Documentation updated (if applicable)
  • No secrets, credentials, or internal references in the diff

Contribution License Agreement

🤖 Generated with Claude Code

The commit-count step measured BASE..HEAD from the event's
pull_request.base.sha. A re-run replays the original event, so once the
branch is rebased past that base, the range spans every main commit in
between: kirodotdev#30034's one commit was counted as 62.

The checkout is GitHub's merge commit, whose first parent is the base
tip it was built on, and the diff-base step has already fetched its
history. Count from that parent when the second parent is the PR head,
as the diff-scoped gates do since kirodotdev#38678; otherwise keep base.sha.

Fixes kirodotdev#39016

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@leonlaiyc
leonlaiyc requested review from a team and KiroCrewCaptain as code owners November 18, 2026 10:25
@github-actions github-actions Bot added fork Pull request from a fork (external contributor) readiness: action required A blocking check or review needs attention merge conflict Branch has merge conflicts with its base — author must resolve before merge labels Nov 18, 2026
@leonlaiyc

Copy link
Copy Markdown
Contributor Author

Closing as superseded: #38997 (merged) dropped the PR Hygiene commit-count check this PR corrected, so there is nothing left to fix.

@leonlaiyc leonlaiyc closed this Nov 19, 2026
@github-actions github-actions Bot removed the readiness: action required A blocking check or review needs attention label Nov 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fork Pull request from a fork (external contributor) merge conflict Branch has merge conflicts with its base — author must resolve before merge

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ci: PR Hygiene commit count reads a stale base.sha and counts main's commits

1 participant