Skip to content

ci(hooks): add a pre-push docstring coverage check - #1589

Open
lusoris wants to merge 6 commits into
mainfrom
ci/prepush-docstring-coverage
Open

lusoris wants to merge 6 commits into
mainfrom
ci/prepush-docstring-coverage

Conversation

@lusoris

@lusoris lusoris commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Pull Request

Summary

  • Pre-push now runs scripts/ci/docstring_coverage_policy.py on the pushed Python and shell files. A function this branch touches with cyclomatic complexity 2 or more must carry a docstring, or a # comment block directly above it for shell. Before this, the CodeRabbit "Docstring Coverage" pre-merge warning was the first place a missing docstring showed up.
  • Only lines changed since the merge base with origin/main count, read from the committed blob, so legacy functions stay quiet until a branch edits them. Every bound reports what it skips: paths past the file limit are named as not checked, an oversized file is a file-too-large finding, and a diff past the hunk limit counts every function in the file as touched.
  • DocstringCoveragePolicyTest has 29 cases: planted Python and shell findings, documented and simple functions, untouched and legacy code, deletions, nested functions and methods, committed blob versus worktree, Windows paths, the path, hunk, span and file bounds, parse and git failures, the lefthook wiring and a self-check over the policy itself. Nine mutants, one per bound or rule, each turn their test red.

Type of change

  • CI / tooling / DX

Checklist

  • make ci passes locally (0 lint · 0 gosec · 0 govulncheck · race-green)
  • New packages have an AGENTS.md
  • New modules expose an fx.Module and have an fxtest.New smoke test
  • New dependencies are justified against awesome-go alternatives (PR comment or ADR)
  • ADR added to docs/adr/ if this records an architecture decision
  • docs/architecture/container.puml updated if a new top-level package landed
  • README.md module catalog + capabilities.yaml updated (if a package was added or moved)
  • AGENTS.md layout tree updated (if a new top-level package was added)

Local gates: make verify-all exited 0 on 1efcfbac with the pinned engine. The merge of main in c7bd50e0 adds the 10 one-line persona Markdown insertions from #1586, and on c7bd50e0 the pre-push hook passed all seven steps (audit, build-test, docstring-coverage, merge-comments, python-tests, security, shellcheck).

End to end through lefthook 2.2.2 (lefthook run pre-push --command docstring-coverage --file <path>, one scratch clone per scenario): a planted undocumented branching function exits 1, the same function with a docstring exits 0, and the planted file under a Windows-separator path next to README.md exits 1. On the base commit the job does not exist, so the same planted function passes the push gate.

Migration (breaking changes only)

// Before

// After

References

  • Go and C functions are out of scope. Go doc comments on exported symbols stay with revive's exported and package-comments rules in .golangci.yml; C has no docstring check.

Summary by Sourcery

Enforce branch-touched docstring coverage for complex Python and shell functions during pre-push checks.

New Features:

  • Add a pre-push check that enforces docstrings or qualifying shell comments on branch-touched Python and shell functions with cyclomatic complexity of at least two.
  • Report bounded, fail-closed findings for oversized files, path or diff limits, parse failures, and Git errors while checking committed HEAD content against the merge-base diff.

Enhancements:

  • Limit coverage enforcement to functions touched by the branch so legacy code is not reported until edited.

CI:

  • Wire the docstring coverage policy into Lefthook pre-push and the Python test suites.

Tests:

  • Add comprehensive policy tests covering Python and shell rules, touched-line behavior, bounds, newline formats, Git failures, wiring, and self-validation.

Signed-off-by: lusoris <lusoris@proton.me>
The pre-push docstring check now reports every input it does not read
instead of dropping it. Raw path lists past MAX_RAW_PATHS count as not
checked, files past MAX_SOURCE_LINES return a file-too-large finding,
and a diff past MAX_HUNKS treats every function in the file as touched
rather than stopping at the cap.

Shell comment stripping only cuts at a hash that starts a word, so
`$#` and `${#x}` stay code. A git failure after the merge base resolves
names the failing git subcommand instead of the merge-base hint.

Tests cover the long-hunk, hunk-overflow, long-span and raw-path bounds
next to the earlier positive, negative and boundary cases.

Signed-off-by: lusoris <lusoris@proton.me>
…coverage

Signed-off-by: lusoris <lusoris@proton.me>
…ture

Adding docstring_coverage_policy_test.py to the required suite list in
scripts/ci/python-tests.sh made python-tests-policy_test.sh fail: its
fixture repositories had no stand-in for the new suite, so every
runner call stopped at "required Python test is missing".

Each fixture root now writes a passing stand-in for the suite, the
required_suites list includes it, and the run-once check expects 13
suites instead of 12.

Signed-off-by: lusoris <lusoris@proton.me>
…coverage

Signed-off-by: lusoris <lusoris@proton.me>
@sourcery-ai

sourcery-ai Bot commented Nov 23, 2026

Copy link
Copy Markdown

Sorry @lusoris, you've used your own review budget of 250,000 diff characters for the last 7 days.

You can request another review in 5 days and 17 hours by commenting @sourcery-ai review. Upgrade to get a review now.

@coderabbitai

coderabbitai Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 27 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

Run configuration

Commits

Reviewing files that changed from the base of the PR and between aa609dc and 78eb31d.

Files selected for processing: 5

  • lefthook.yml
  • scripts/ci/docstring_coverage_policy.py
  • scripts/ci/docstring_coverage_policy_test.py
  • scripts/ci/python-tests-policy_test.sh
  • scripts/ci/python-tests.sh
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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.

Share

@sourcery-ai

sourcery-ai Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

Reviewer's Guide

Implementation

Review the bounded, branch-touched Python and shell docstring policy that the hook will execute.

  • scripts/ci/docstring_coverage_policy.py L1-449

Configuration

Review the pre-push hook command that invokes the policy for pushed files.

  • lefthook.yml L80-81

Tests

Review the policy tests, including the Git fixtures and cases that verify findings, bounds, line mapping, failures, and hook wiring.

  • scripts/ci/docstring_coverage_policy_test.py L1-519

Review registration of the new policy suite in the Python test runner and its policy-test fixtures.

  • scripts/ci/python-tests-policy_test.sh L73-234 (6 small edits)
  • scripts/ci/python-tests.sh L137

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

A lone carriage return inside a pushed Python or shell file made the
docstring gate miss a touched function. Text mode turned the CR into
a newline, and the Python parser breaks lines there too, while git
numbers lines by LF only. The function then sat past every touched
hunk line and passed without a docstring.

Git output is now decoded from bytes. Shell lines and the size bound
split on LF, Python node lines go through a map from parser lines to
git lines, and hunk headers are read from LF-split diff text.

Tests b20 to b24, n10 and n11 cover lone CR and CRLF content for both
languages, a CR inside a diff line, an untouched function next to a
lone CR, and the bound at and past its limit. Six mutants, one per
change, each turn their test red.

Signed-off-by: lusoris <lusoris@proton.me>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant