Skip to content

fix(python-precommit): detect repo state instead of hardcoding uv sync --frozen - #164

Merged
williaby merged 2 commits into
mainfrom
chore/python-precommit-detect-state
May 26, 2026
Merged

williaby merged 2 commits into
mainfrom
chore/python-precommit-detect-state

Conversation

@williaby

@williaby williaby commented May 25, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Pattern choices

  • Install (uv-locked) [line 104]: Pattern A inline with # NOSONAR(S8541) (literal --frozen makes S8544 inapplicable).
  • Install (uv-no-lock) [line 112]: Pattern A inline with # NOSONAR(S8541,S8544) (no --frozen by design).
  • Run pre-commit hooks [lines 124-135]: Pattern B (preceding-line # NOSONAR(S8541) inside run: | block, two branches both with literal --frozen).

Affected downstream repos

Per /tmp/ci-failure-inventory.tsv: pre-commit runs on most uv repos; not specifically tracked on the inventory but exercised by every uv-managed downstream caller.

Test plan

  • actionlint -shellcheck= .github/workflows/python-precommit.yml passes (zero errors; embedded shellcheck -shellcheck= flag per feedback_actionlint_shellcheck_embed).
  • CI on .github passes.
  • SonarCloud quality gate = OK with 0 open vulns.
  • After merge, one sample downstream caller workflow re-run succeeds.

Summary by CodeRabbit

  • Chores
    • Enhanced the Python pre-commit workflow to intelligently detect and adapt to different repository dependency-management configurations. The workflow now conditionally manages dependency installation based on the presence of lock files, with improved error handling for incompatible setups. Pre-commit hook execution is optimized based on repository state detection.

Review Change Stack

Copilot AI review requested due to automatic review settings May 25, 2026 23:47
@coderabbitai

coderabbitai Bot commented May 25, 2026 •

Copy link
Copy Markdown

Warning

Review limit reached

@williaby, we couldn't start this review because you've reached your PR review rate limit.

More reviews will be available in 46 minutes and 47 seconds. Learn how PR review limits work.

Your organization has run out of usage credits. Purchase more in the billing tab.

⌛ How to resolve this issue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans include higher PR review limits than trial, open-source, and free plans. In all cases, reviews become available again over time. During sustained high-volume PR review activity, CodeRabbit may temporarily slow when the next review becomes available.

Please see our Fair Usage Limits Policy for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: f2745d0a-e9e8-4230-ace0-894fa64c8a27

📥 Commits

Reviewing files that changed from the base of the PR and between f176855 and a4bdac7.

📒 Files selected for processing (1)
  • .github/workflows/python-precommit.yml
📝 Walkthrough

Walkthrough

The workflow now branches based on detected repository state: it checks for pyproject.toml and uv.lock presence, explicitly rejects Poetry-based repos, and conditionally runs dependency installation and pre-commit hooks only for uv-managed projects. When pyproject.toml is absent, pre-commit is skipped with a step summary notice.

Changes

Repository State-Aware Pre-Commit Workflow

Layer / File(s) Summary
Dependency detection and conditional pre-commit execution
.github/workflows/python-precommit.yml
A new "Detect repo state" step classifies repositories as skip (no pyproject.toml), poetry-not-supported (Poetry detected), uv-locked (uv.lock present), or uv-no-lock (uv.lock absent), then conditionally installs dependencies with uv sync --frozen or uv sync, writes a skip notice when pyproject.toml is missing, and gates pre-commit execution to only non-skip uv paths with configurable --show-diff-on-failure support.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐰 A workflow so clever, state-aware and spry,
Detects uv and Poetry with a discerning eye,
Skips when pyproject.toml takes its leave,
Then runs pre-commit hooks—no need to grieve! ✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and specifically describes the main change: detecting repo state instead of hardcoding uv sync --frozen, which is the primary objective.
Description check ✅ Passed The PR description is detailed and well-structured with clear sections (Summary, Pattern choices, Affected downstream repos, Test plan), exceeding the basic template requirements.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch chore/python-precommit-detect-state

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

Comment @coderabbitai help to get the list of available commands and usage tips.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Updates the reusable python-precommit.yml workflow to detect the caller repository state (no pyproject.toml, Poetry, uv with uv.lock, uv without uv.lock) and then conditionally install dependencies and run pre-commit in a way that avoids hardcoded uv sync --frozen failures across downstream repos.

Changes:

  • Added a Detect repo state step that classifies the repo as skip, poetry-not-supported, uv-locked, or uv-no-lock.
  • Split dependency installation into two guarded uv sync steps (locked vs no-lock) and added a skip notice for repos without pyproject.toml.
  • Guarded the Run pre-commit hooks step to only run for uv repos, and used literal --frozen with NOSONAR annotations for --no-build.

Comment on lines +115 to +120
- name: Skip notice (no pyproject.toml)
if: steps.detect.outputs.state == 'skip'
run: |
echo "## Pre-Commit Skipped" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "No pyproject.toml at repo root, nothing to run pre-commit against." >> $GITHUB_STEP_SUMMARY

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In @.github/workflows/python-precommit.yml:
- Around line 115-120: The final summary step that uses "if: always()" can
produce contradictory messaging when the earlier "Skip notice (no
pyproject.toml)" branch set steps.detect.outputs.state == 'skip'; update the
final summary step in .github/workflows/python-precommit.yml to be skip-aware by
gating it on a condition like "if: steps.detect.outputs.state != 'skip' &&
always()" or by reading steps.detect.outputs.state and outputting a skip-aware
message (i.e., check steps.detect.outputs.state and either print pass/fail or a
single "Pre-Commit Skipped" summary), referencing the existing conditional
output steps.detect.outputs.state and the summary step currently using "if:
always()".
🪄 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: f404881b-7627-495a-bf01-8bf16dada58e

📥 Commits

Reviewing files that changed from the base of the PR and between 7fdf943 and f176855.

📒 Files selected for processing (1)
  • .github/workflows/python-precommit.yml

Comment thread .github/workflows/python-precommit.yml Resolved
williaby added a commit that referenced this pull request May 26, 2026
… --frozen

Apply established NOSONAR detect-state pattern (see python-mutation.yml:159-169
and merged PRs #155-#160, #164-#166). Branches on pyproject.toml + lockfile
presence: skip when no pyproject, fail fast on poetry repos with actionable
error, use --frozen on uv-locked repos, drop --frozen on uv-no-lock repos.

Apply Wave 1C strengthened pattern (docs/sonarcloud-nosonar-patterns.md
strengthened 2026-05-26): only single-line run: shape reliably honors NOSONAR.
Split install into two if:-guarded steps with literal flags and inline Pattern
A NOSONAR. Collapse Check docstring coverage and Build MkDocs site multi-line
run: | blocks to single-line run: so SonarCloud can pattern-match Pattern A.

S8541 suppression is acceptable here: the \$NO_BUILD_FLAG env var is dynamic
by design (consumers with PEP 517 build backends like hatchling can pass
no-build: false). Step-level branching would just shuffle the finding.
williaby added a commit that referenced this pull request May 26, 2026
… --frozen (#171)

* fix(python-docs): detect repo state instead of hardcoding uv sync/run --frozen

Apply established NOSONAR detect-state pattern (see python-mutation.yml:159-169
and merged PRs #155-#160, #164-#166). Branches on pyproject.toml + lockfile
presence: skip when no pyproject, fail fast on poetry repos with actionable
error, use --frozen on uv-locked repos, drop --frozen on uv-no-lock repos.

Apply Wave 1C strengthened pattern (docs/sonarcloud-nosonar-patterns.md
strengthened 2026-05-26): only single-line run: shape reliably honors NOSONAR.
Split install into two if:-guarded steps with literal flags and inline Pattern
A NOSONAR. Collapse Check docstring coverage and Build MkDocs site multi-line
run: | blocks to single-line run: so SonarCloud can pattern-match Pattern A.

S8541 suppression is acceptable here: the \$NO_BUILD_FLAG env var is dynamic
by design (consumers with PEP 517 build backends like hatchling can pass
no-build: false). Step-level branching would just shuffle the finding.

* fix(python-docs): gate deploy on build state to avoid download-artifact failure

Expose steps.detect.outputs.state as a build job-level output and require
needs.build.outputs.state != 'skip' in the deploy job if: clause. Without
this, the build job succeeded in skip mode (only the skip-notice step ran;
the upload-artifact step was already conditionally skipped) and needs: build
falsely cleared deploy to run, where actions/download-artifact then failed
with "artifact not found" against the never-uploaded documentation-site.

Add CHANGELOG entry under [Unreleased] / Fixed.

Addresses Copilot inline review on PR #171.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.7 <noreply@anthropic.com>
@williaby

Copy link
Copy Markdown
Collaborator Author

PR Review

Status: CI green (19/19), SonarCloud QG passed, MergeState BEHIND (rebase needed). Copilot and CodeRabbit both reviewed.

Important (should fix)

  • [Copilot + CodeRabbit, convergent] .github/workflows/python-precommit.yml:137-148: The Pre-Commit Summary step uses if: always(), so on the state=skip path the user sees both "Pre-Commit Skipped" (line 115-120) and "All pre-commit hooks passed" (line 142). Skipped runs render as successful executions, which is misleading.

    Suggested fix:

    - name: Pre-Commit Summary
      if: always() && steps.detect.outputs.state != 'skip'

Suggested

  • .github/workflows/python-precommit.yml:115-120: Step ordering reads slightly unconventionally; the Skip notice step sits between the two install steps. Consider moving it immediately after Detect repo state for readability. No behavioral change.
  • PR body Test plan: only 1/4 checkboxes ticked. CI is green and SonarCloud QG passed, so the next two items are now verifiable. Tick them before merge.

Positive notes

Recommended action

  1. Apply the state != 'skip' guard on the summary step.
  2. Rebase on main to clear the BEHIND state.
  3. Tick verifiable test-plan checkboxes.
  4. Merge.

Generated with Claude Code

williaby and others added 2 commits May 25, 2026 19:22
…c --frozen

Apply the split-install detect-state pattern (see python-mutation.yml:159-169
for the original NOSONAR S8541 precedent, and PR #160 / python-release.yml for
the canonical split-install exemplar). Branches on pyproject.toml + lockfile
presence: skip when no pyproject, fail fast on poetry repos with actionable
error, install with literal --frozen on uv-locked path, install without
--frozen on uv-no-lock path (uv sync generates the lockfile).

Downstream pre-commit run step uses literal --frozen because the install
step guaranteed uv.lock exists by then; preceding-line NOSONAR(S8541) is
honored when the underlying line has a literal --frozen token (only the
--no-build flag remains dynamic).

Avoids the documented anti-pattern: dynamic \$FROZEN_FLAG inside run: |
block scalars is not suppressed by preceding-line NOSONAR (empirical
finding from PRs #157/#158/#159; see BWCPA/.github docs/sonarcloud-nosonar-patterns.md).

Verification: actionlint passes (with -shellcheck= per documented
embedded-shellcheck limitation in feedback_actionlint_shellcheck_embed).
When the detect step sets state=skip (no pyproject.toml), the workflow
should not emit the generic "All pre-commit hooks passed" summary; the
"Skip notice" step already provides the correct user-facing message and
the success line is misleading because nothing ran.

Gate the Pre-Commit Summary step on state != 'skip' so skipped runs
render only the skip notice. Behavior on uv-locked, uv-no-lock, and
poetry-not-supported paths is unchanged.

Flagged independently by Copilot and CodeRabbit on PR #164 review.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@williaby
williaby force-pushed the chore/python-precommit-detect-state branch from f176855 to a4bdac7 Compare May 26, 2026 02:24
@williaby

Copy link
Copy Markdown
Collaborator Author

PR Fix Summary

Addressed the Important finding from /pr-review (independently flagged by Copilot and CodeRabbit).

Branch rebased onto origin/main (was BEHIND; force-pushed with --force-with-lease).

Fix applied in commit a4bdac7:

       - name: Pre-Commit Summary
-        if: always()
+        if: always() && steps.detect.outputs.state != 'skip'

Why: When the detect step sets state=skip (no pyproject.toml), the workflow previously emitted both the "Pre-Commit Skipped" notice and the generic "All pre-commit hooks passed" summary, making skipped runs render as successful executions. The gate now suppresses the success line on the skip path; the "Skip notice" step remains the sole user-facing message. Behavior on uv-locked, uv-no-lock, and poetry-not-supported paths is unchanged.

Replies to bot threads:

  • @copilot: Fixed in a4bdac7. Gated the Pre-Commit Summary step on state != 'skip'.
  • @coderabbitai: Fixed in a4bdac7. Applied your suggested gate verbatim.

Verification:

  • actionlint -shellcheck= clean
  • pre-commit run --files passed (yamllint, no-em-dash, secret scans, commitizen)
  • CI re-run triggered by force-push

Suggested-tier items (not blocking; deferred unless requested):

  • Step ordering nit (Skip notice between installs) — readability only, no behavioral change.
  • PR body Test plan checkboxes — author to tick once CI re-runs are green.

Generated with Claude Code

@coderabbitai

coderabbitai Bot commented May 26, 2026

Copy link
Copy Markdown

Tip

For best results, initiate chat on the files or code changes.

@williaby: Thanks for applying the fix and the detailed summary. The if: always() && steps.detect.outputs.state != 'skip' gate on the Pre-Commit Summary step is exactly right — the skip path now has a single, unambiguous user-facing message ("Pre-Commit Skipped") without the misleading "All pre-commit hooks passed" summary appearing alongside it. Rebase and verification steps look solid too. The deferred suggested items are fine to leave for a follow-up if needed. ✅

🐇 ✨


If you found this review helpful, would you consider giving us a shout-out on X?

Thank you for using CodeRabbit!

@sonarqubecloud

Copy link
Copy Markdown

@williaby
williaby merged commit 7a5aeaa into main May 26, 2026
25 of 26 checks passed
@williaby
williaby deleted the chore/python-precommit-detect-state branch May 26, 2026 02:26
Copilot stopped work on behalf of williaby due to an error May 26, 2026 02:26
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.

2 participants