Skip to content

chore: Add advisory code complexity checks#1402

Merged
hatayama merged 3 commits into
v3-betafrom
feature/add-code-analytics
Jun 25, 2026
Merged

chore: Add advisory code complexity checks#1402
hatayama merged 3 commits into
v3-betafrom
feature/add-code-analytics

Conversation

@hatayama

@hatayama hatayama commented Jun 25, 2026

Copy link
Copy Markdown
Owner

Summary

  • Adds advisory cyclomatic complexity reporting for both the Go CLI and Unity package C# code.
  • Keeps the initial rollout non-blocking while still surfacing JSON artifacts and human-readable output for reviewers.

User Impact

  • Maintainers can see complexity hotspots during pull request review without blocking existing work immediately.
  • Future threshold tightening can be enabled through the same script once the warning list is small enough to enforce.

Changes

  • Adds a dedicated golangci-lint complexity config using cyclop at threshold 25.
  • Adds a C# CA1502-based analysis host that avoids editing Unity-generated project files.
  • Adds a combined complexity check script, CI workflow, documentation, and focused tests for the new C# host.

Verification

  • dotnet test tests/UnityCliLoop.CodeComplexity.Tests/UnityCliLoop.CodeComplexity.Tests.csproj --configuration Release
  • dotnet test tests/UnityCliLoop.DeadCodeScanner.Tests/UnityCliLoop.DeadCodeScanner.Tests.csproj --configuration Release
  • scripts/check-go-cli.sh
  • scripts/check-code-complexity.sh
  • CODE_COMPLEXITY_FAIL_ON_EXCEEDED=true scripts/check-code-complexity.sh exits non-zero with the current advisory findings, as expected.
  • autoreview --mode branch --base origin/v3-beta reported no accepted/actionable findings.

Review in cubic

Introduce cyclop configuration for the Go CLI and a CA1502-based C# analysis host so complexity hotspots can be reported without editing Unity-generated project files. Wire the advisory check into CI and document threshold and fail-mode operation.
@coderabbitai

coderabbitai Bot commented Jun 25, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Warning

Review limit reached

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

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

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

⌛ 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.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based credits.

🚦 How do rate limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please see our Fair Usage Limits Policy for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 4b6db0e5-9e1d-4a91-b247-daef96acb060

📥 Commits

Reviewing files that changed from the base of the PR and between 4729b3f and b3d7243.

📒 Files selected for processing (2)
  • tests/UnityCliLoop.CodeComplexity.Tests/CodeComplexityAnalyzerRunnerTests.cs
  • tools/UnityCliLoop.CodeComplexity/CommandLineOptions.cs
📝 Walkthrough

Walkthrough

Adds a code-complexity checker for Go and C# with shared models, Roslyn analysis, CLI output, shell execution, CI wiring, tests, and documentation.

Changes

Code complexity toolchain

Layer / File(s) Summary
Contracts and source discovery
tools/UnityCliLoop.CodeComplexity/CodeComplexityModels.cs, tools/UnityCliLoop.CodeComplexity/CodeMetricsAdditionalText.cs, tools/UnityCliLoop.CodeComplexity/SourceFileCollector.cs
Defines the report format, options, issue, and source-file set types, and adds Roslyn additional-text and source-file collection helpers.
Analysis runner
tools/UnityCliLoop.CodeComplexity/CodeComplexityAnalyzerRunner.cs, tests/UnityCliLoop.CodeComplexity.Tests/CodeComplexityAnalyzerRunnerTests.cs
Builds the CA1502 Roslyn compilation, maps diagnostics to normalized issues, and verifies production and non-production source filtering in tests.
CLI parsing and output
tools/UnityCliLoop.CodeComplexity/CommandLineOptions.cs, tools/UnityCliLoop.CodeComplexity/CodeComplexityReporter.cs, tools/UnityCliLoop.CodeComplexity/Program.cs, tests/UnityCliLoop.CodeComplexity.Tests/CodeComplexityAnalyzerRunnerTests.cs
Parses command-line options, writes JSON or table reports, and returns help or exit codes from the console entry point.
Shell, workflow, and docs
cli/.golangci-complexity.yml, scripts/check-code-complexity.sh, .github/workflows/code-complexity.yml, docs/code-complexity.md
Adds the shell wrapper, Go linter config, GitHub Actions workflow, and documentation for running the complexity checks.

Sequence Diagram(s)

sequenceDiagram
  participant Program
  participant CommandLineOptions
  participant CodeComplexityAnalyzerRunner
  participant CodeComplexityReporter
  Program->>CommandLineOptions: HasHelpOption(args)
  alt help requested
    Program->>CommandLineOptions: CreateHelpText()
  else run analysis
    Program->>CommandLineOptions: Parse(args)
    Program->>CodeComplexityAnalyzerRunner: AnalyzeAsync(options, ct)
    Program->>CodeComplexityReporter: Write(issues, options)
  end
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~60 minutes

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 11.63% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly matches the main change: adding advisory code complexity checks.
Description check ✅ Passed The description accurately summarizes the complexity-check workflow, tooling, tests, and non-blocking rollout.
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 feature/add-code-analytics

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.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🧹 Nitpick comments (2)
tests/UnityCliLoop.CodeComplexity.Tests/CodeComplexityAnalyzerRunnerTests.cs (1)

127-177: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Extend the fixture to cover the remaining discovery rules.

CreateSampleRepository() only exercises Packages/src and Assets, so the new <root>/tests path and the /.agents/ / /.claude/ exclusions can regress silently.

🤖 Prompt for 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.

In `@tests/UnityCliLoop.CodeComplexity.Tests/CodeComplexityAnalyzerRunnerTests.cs`
around lines 127 - 177, CreateSampleRepository only covers Packages/src and
Assets, so add fixture content that also exercises the remaining discovery rules
in CodeComplexityAnalyzerRunnerTests. Extend the sample repo setup to include a
tests directory under the root and hidden .agents and .claude directories so the
discovery logic is validated against both the new root/tests path and the
exclusion filters. Keep the changes localized to CreateSampleRepository and use
WriteFile to add representative files in those locations.
.github/workflows/code-complexity.yml (1)

47-61: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Exercise the blocking-mode path somewhere in CI.

This workflow only covers advisory mode, so the documented non-zero exit behavior can drift unnoticed until someone enables CODE_COMPLEXITY_FAIL_ON_EXCEEDED=true for real. Add a small smoke test or fixture that is expected to fail in blocking mode.

🤖 Prompt for 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.

In @.github/workflows/code-complexity.yml around lines 47 - 61, The
code-complexity workflow only runs advisory checks, so add a CI smoke check that
explicitly exercises the blocking-mode path and expects a non-zero exit when
complexity is exceeded. Update the existing code-complexity workflow around
Report complexity / Write complexity artifacts to include a small fixture or
temporary check that runs scripts/check-code-complexity.sh with
CODE_COMPLEXITY_FAIL_ON_EXCEEDED=true and verifies failure, using the same
complexity tooling paths already present.
🤖 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 `@tools/UnityCliLoop.CodeComplexity/CodeComplexityAnalyzerRunner.cs`:
- Around line 24-31: Expand the hardcoded DefaultPreprocessorSymbols in
CodeComplexityAnalyzerRunner so it includes all repository-used defines
currently missing from the analyzer: ULOOP_DEBUG, ULOOP_HAS_TEST_FRAMEWORK,
UNITY_6000_3_OR_NEWER, and UNITY_6000_4_OR_NEWER. Keep the existing symbol list
in the same static array and add these entries alongside the other Unity/ULOOP
symbols so conditional branches in the analyzer are parsed correctly.

In `@tools/UnityCliLoop.CodeComplexity/Program.cs`:
- Around line 19-37: The CLI entry point in RunAsync currently lets
CommandLineOptions.Parse throw and print a stack trace for invalid arguments.
Wrap the parse path in Program.RunAsync so expected validation failures are
caught, emit a clean usage/error message, and return a non-zero exit code, while
leaving CodeComplexityAnalyzerRunner.AnalyzeAsync and other unexpected
exceptions unhandled so real bugs still fail fast.

---

Nitpick comments:
In @.github/workflows/code-complexity.yml:
- Around line 47-61: The code-complexity workflow only runs advisory checks, so
add a CI smoke check that explicitly exercises the blocking-mode path and
expects a non-zero exit when complexity is exceeded. Update the existing
code-complexity workflow around Report complexity / Write complexity artifacts
to include a small fixture or temporary check that runs
scripts/check-code-complexity.sh with CODE_COMPLEXITY_FAIL_ON_EXCEEDED=true and
verifies failure, using the same complexity tooling paths already present.

In
`@tests/UnityCliLoop.CodeComplexity.Tests/CodeComplexityAnalyzerRunnerTests.cs`:
- Around line 127-177: CreateSampleRepository only covers Packages/src and
Assets, so add fixture content that also exercises the remaining discovery rules
in CodeComplexityAnalyzerRunnerTests. Extend the sample repo setup to include a
tests directory under the root and hidden .agents and .claude directories so the
discovery logic is validated against both the new root/tests path and the
exclusion filters. Keep the changes localized to CreateSampleRepository and use
WriteFile to add representative files in those locations.
🪄 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: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 69719729-887a-47e5-a8a8-5b4594451f10

📥 Commits

Reviewing files that changed from the base of the PR and between bd3d7ed and b3a36ca.

⛔ Files ignored due to path filters (3)
  • tests/UnityCliLoop.CodeComplexity.Tests/UnityCliLoop.CodeComplexity.Tests.csproj is excluded by none and included by none
  • tools/UnityCliLoop.CodeComplexity/CodeMetricsConfig.txt is excluded by none and included by none
  • tools/UnityCliLoop.CodeComplexity/UnityCliLoop.CodeComplexity.csproj is excluded by none and included by none
📒 Files selected for processing (12)
  • .github/workflows/code-complexity.yml
  • cli/.golangci-complexity.yml
  • docs/code-complexity.md
  • scripts/check-code-complexity.sh
  • tests/UnityCliLoop.CodeComplexity.Tests/CodeComplexityAnalyzerRunnerTests.cs
  • tools/UnityCliLoop.CodeComplexity/CodeComplexityAnalyzerRunner.cs
  • tools/UnityCliLoop.CodeComplexity/CodeComplexityModels.cs
  • tools/UnityCliLoop.CodeComplexity/CodeComplexityReporter.cs
  • tools/UnityCliLoop.CodeComplexity/CodeMetricsAdditionalText.cs
  • tools/UnityCliLoop.CodeComplexity/CommandLineOptions.cs
  • tools/UnityCliLoop.CodeComplexity/Program.cs
  • tools/UnityCliLoop.CodeComplexity/SourceFileCollector.cs

Comment thread tools/UnityCliLoop.CodeComplexity/CodeComplexityAnalyzerRunner.cs
Comment thread tools/UnityCliLoop.CodeComplexity/Program.cs

@cubic-dev-ai cubic-dev-ai 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.

5 issues found across 15 files

Reply with feedback, questions, or to request a fix.

Re-trigger cubic

Comment thread .github/workflows/code-complexity.yml Outdated
Comment thread .github/workflows/code-complexity.yml
Comment thread tools/UnityCliLoop.CodeComplexity/CommandLineOptions.cs Outdated
Comment thread scripts/check-code-complexity.sh Outdated
Comment thread tools/UnityCliLoop.CodeComplexity/CodeComplexityAnalyzerRunner.cs Outdated
Keep advisory complexity reporting robust by preserving real golangci-lint failures, broadening the C# analyzer preprocessor context, and returning clean validation failures for expected CLI input errors.

@cubic-dev-ai cubic-dev-ai 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.

1 issue found across 7 files (changes from recent commits).

Reply with feedback, questions, or to request a fix.

Re-trigger cubic

Comment thread tools/UnityCliLoop.CodeComplexity/CommandLineOptions.cs Outdated
Reject malformed --root values before calling Path.GetFullPath so expected command-line validation failures keep returning a deterministic parse result instead of an unhandled exception.
@hatayama
hatayama merged commit 7107fa3 into v3-beta Jun 25, 2026
7 checks passed
@hatayama
hatayama deleted the feature/add-code-analytics branch June 25, 2026 11:21
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