Skip to content

lint: enable detekt rule: NestedScopeFunctions#334

Open
AdrianLeeElder wants to merge 11 commits intodevelopfrom
fix/issue-298
Open

lint: enable detekt rule: NestedScopeFunctions#334
AdrianLeeElder wants to merge 11 commits intodevelopfrom
fix/issue-298

Conversation

@AdrianLeeElder
Copy link
Copy Markdown
Contributor

@AdrianLeeElder AdrianLeeElder commented Mar 31, 2026

Closes #298

Automated by auto-agent.

Summary by CodeRabbit

  • Refactor

    • Simplified null-handling and variable assignment across backend services.
    • Reworked email assembly and retry-validation control flow for clearer processing.
    • Reorganized dashboard translation logic, including display, mappings, thresholds and transformations.
    • Clarified JWT token claim handling.
  • Documentation

    • Added inline documentation for email and synthetics components.
  • Tests

    • Simplified a deduplication fingerprint test for clarity.
  • Chores

    • Enabled an additional code-quality linting rule.

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Mar 31, 2026

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review
📝 Walkthrough

Walkthrough

Enabled the Detekt complexity.NestedScopeFunctions rule and refactored several Kotlin files to remove nested scope-function usage, replacing it with explicit locals and null-guards; added KDoc comments and relocated some local constructions. No public API or exported signatures were changed.

Changes

Cohort / File(s) Summary
Detekt configuration
backend/detekt.yml, ee/backend/detekt.yml
Re-enabled complexity.NestedScopeFunctions (active: falseactive: true).
Translation logic
backend/src/main/kotlin/com/moneat/dashboards/translation/GrafanaTranslator.kt
Refactored extractDisplayConfig into helper methods; replaced nested scope-functions with stepwise local extraction and explicit null checks; reorganized value-mapping, thresholds, custom display, legend, gauge, and transformation parsing into discrete helpers (logic preserved).
Email sending
backend/src/main/kotlin/com/moneat/notifications/services/EmailService.kt
Moved MimeBodyPart creation outside the message/multipart builder and added KDoc for EmailService, sendEmail, trackEmailSent, escapeHtml(), and the SMTP Authenticator override; behavioral flow unchanged.
Synthetics service
backend/src/main/kotlin/com/moneat/synthetics/routes/SyntheticsService.kt
Rewrote retry-parameter validation from nested let usage to cached local variables with independent if checks; added KDoc for class and related methods.
Tests / Helpers
backend/src/test/kotlin/com/moneat/services/EventServiceTest.kt, backend/src/test/kotlin/com/moneat/testsupport/RouteTestSupport.kt
Simplified fingerprint construction in test and made JWT orgId claim conditional via explicit if (removed nested scope-functions); behavior preserved.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐇 I nudged out nested lets with care,
Pulling locals from tangled air.
Docs tucked in pockets, parts placed neat,
Code hops lighter on its feet. 🥕

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 35.48% 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
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: enabling the NestedScopeFunctions Detekt rule across both backend and enterprise configurations.
Linked Issues check ✅ Passed The PR successfully completes all three requirements from issue #298: enables NestedScopeFunctions in both detekt.yml files, fixes all reported violations through refactorings, and maintains zero remaining violations.
Out of Scope Changes check ✅ Passed All changes are directly related to fixing NestedScopeFunctions violations. The refactorings in GrafanaTranslator, EmailService, and SyntheticsService extract nested scope functions into helper methods as required by the rule.

✏️ 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 fix/issue-298

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.

Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
backend/src/test/kotlin/com/moneat/services/EventServiceTest.kt (1)

43-43: ⚠️ Potential issue | 🟡 Minor

Avoid wildcard imports.

Line 43 uses java.util.* which violates the coding guideline requiring explicit imports. Replace with specific imports for the classes actually used.

Proposed fix
-import java.util.*
+import java.util.UUID

As per coding guidelines: "Never use wildcard imports; always use explicit imports (e.g., import com.moneat.models.User instead of import com.moneat.models.*)"

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@backend/src/test/kotlin/com/moneat/services/EventServiceTest.kt` at line 43,
Replace the wildcard import `java.util.*` with explicit imports for only the
classes used in EventServiceTest (e.g., `java.util.UUID`, `java.util.Date`,
`java.util.List` or whichever specific types appear in the file); update the
import statement(s) so there are no wildcard imports and only the concrete
java.util types referenced by the file (look for usages of UUID, Date, List,
etc., to determine which explicit imports to add).
🧹 Nitpick comments (1)
backend/src/main/kotlin/com/moneat/synthetics/routes/SyntheticsService.kt (1)

142-149: Refactor is correct, with minor redundancy.

The logic correctly validates retry parameters when either retryCount or retryIntervalMs is provided, using defaults for the missing value. However, when both parameters are non-null, validateRetryParams is called twice with identical arguments.

Consider consolidating to a single validation call:

Optional: Consolidate validation
         val rc = request.retryCount
         val ri = request.retryIntervalMs
-        if (rc != null) {
-            validateRetryParams(rc, ri ?: RETRY_INTERVAL_MS_DEFAULT)
-        }
-        if (ri != null) {
-            validateRetryParams(rc ?: RETRY_COUNT_DEFAULT, ri)
+        if (rc != null || ri != null) {
+            validateRetryParams(rc ?: RETRY_COUNT_DEFAULT, ri ?: RETRY_INTERVAL_MS_DEFAULT)
         }
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@backend/src/main/kotlin/com/moneat/synthetics/routes/SyntheticsService.kt`
around lines 142 - 149, The current code calls validateRetryParams twice when
both request.retryCount (rc) and request.retryIntervalMs (ri) are set; replace
the two conditional calls with a single validation: if either rc or ri is
non-null, call validateRetryParams(rc ?: RETRY_COUNT_DEFAULT, ri ?:
RETRY_INTERVAL_MS_DEFAULT) so you only validate once and still apply defaults
when one value is missing (refer to request.retryCount, request.retryIntervalMs,
validateRetryParams, RETRY_COUNT_DEFAULT, RETRY_INTERVAL_MS_DEFAULT).
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Outside diff comments:
In `@backend/src/test/kotlin/com/moneat/services/EventServiceTest.kt`:
- Line 43: Replace the wildcard import `java.util.*` with explicit imports for
only the classes used in EventServiceTest (e.g., `java.util.UUID`,
`java.util.Date`, `java.util.List` or whichever specific types appear in the
file); update the import statement(s) so there are no wildcard imports and only
the concrete java.util types referenced by the file (look for usages of UUID,
Date, List, etc., to determine which explicit imports to add).

---

Nitpick comments:
In `@backend/src/main/kotlin/com/moneat/synthetics/routes/SyntheticsService.kt`:
- Around line 142-149: The current code calls validateRetryParams twice when
both request.retryCount (rc) and request.retryIntervalMs (ri) are set; replace
the two conditional calls with a single validation: if either rc or ri is
non-null, call validateRetryParams(rc ?: RETRY_COUNT_DEFAULT, ri ?:
RETRY_INTERVAL_MS_DEFAULT) so you only validate once and still apply defaults
when one value is missing (refer to request.retryCount, request.retryIntervalMs,
validateRetryParams, RETRY_COUNT_DEFAULT, RETRY_INTERVAL_MS_DEFAULT).

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 26c133e0-9e00-4c4b-b3e7-7ab87b1d6d7c

📥 Commits

Reviewing files that changed from the base of the PR and between fb80968 and 6f2e766.

📒 Files selected for processing (6)
  • backend/detekt.yml
  • backend/src/main/kotlin/com/moneat/dashboards/translation/GrafanaTranslator.kt
  • backend/src/main/kotlin/com/moneat/notifications/services/EmailService.kt
  • backend/src/main/kotlin/com/moneat/synthetics/routes/SyntheticsService.kt
  • backend/src/test/kotlin/com/moneat/services/EventServiceTest.kt
  • backend/src/test/kotlin/com/moneat/testsupport/RouteTestSupport.kt

Add concise KDoc on refactored helpers and related entry points
(extractDisplayConfig, escapeHtml, sendEmail, updateTest, EventServiceTest.setup)
to address docstring coverage feedback alongside existing class docs.

Made-with: Cursor
Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

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 the current code and only fix it if needed.

Inline comments:
In
`@backend/src/main/kotlin/com/moneat/dashboards/translation/GrafanaTranslator.kt`:
- Around line 250-251: The code is dropping non-integer numeric values by using
intOrNull on JSON nodes (e.g., valuePrim -> value in GrafanaTranslator), so
replace intOrNull with doubleOrNull (or parse contentOrNull with toDoubleOrNull)
and propagate a Double (or BigDecimal if higher precision is required) through
the mapNotNull blocks where value/valuePrim is used; update the parsing sites
that currently use valuePrim?.intOrNull (the three occurrences around value, and
the blocks handling thresholds/gauge/fill-opacity) to attempt double parsing
first and only convert to Int where downstream APIs explicitly require integers.
Ensure you handle the same null/empty logic (keep the contentOrNull checks and
the return@mapNotNull null behavior) so behavior is unchanged for missing
values.
🪄 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: c7e96a94-aecc-4609-b208-e5f9f8f8429a

📥 Commits

Reviewing files that changed from the base of the PR and between 3784f4c and 41912e8.

📒 Files selected for processing (1)
  • backend/src/main/kotlin/com/moneat/dashboards/translation/GrafanaTranslator.kt

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 3, 2026

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.

Enable detekt rule: NestedScopeFunctions

1 participant