Skip to content

chore(sonar): Phase 1 maintainability cleanup (~50 issues)#75

Merged
aksOps merged 3 commits intomainfrom
chore/sonar-phase1-cleanup
May 3, 2026
Merged

chore(sonar): Phase 1 maintainability cleanup (~50 issues)#75
aksOps merged 3 commits intomainfrom
chore/sonar-phase1-cleanup

Conversation

@aksOps
Copy link
Copy Markdown
Contributor

@aksOps aksOps commented Apr 30, 2026

Summary

Phase 1 of the SonarCloud code-smell cleanup. Mechanical, low-risk fixes that resolve the highest-volume rules without behaviour change. Phases 2 (React hook hygiene, ~24 issues) and 3 (cognitive complexity, ~55 issues) will follow as separate PRs.

Issues closed: ~52

Rule Count What changed
go:S1192 31 Extracted shared constants (internal/httpconst, internal/storage/sql_consts.go, package-local consts in internal/mcp/tools.go, main.go, test/*service)
typescript:S1444 5 MockWebSocket static fields → static readonly; reset via .length = 0
go:S8193 3 Inlined unnecessary intermediate bindings in if-conditions
shelldre:S7688 3 [ ... ][[ ... ]] in scripts/setup-git-signed.sh
typescript:S2933 2 ErrorBoundary reset/reload class fields → readonly
typescript:S7735 2 typeof X !== 'undefined' ? a : b=== 'undefined' ? b : a
typescript:S4325 2 Redundant casts removed where TS already narrowed
typescript:S6759 2 Readonly<Props> on TopNav, Boom test helper
typescript:S3735 2 void load()load().catch(() => undefined)
godre:S8188 1 pg_partitions_test.go split deferred cancel/Stop

Why a new package for HTTP constants?

Content-Type and application/json are duplicated across three packages (api, mcp, ingest). A leaf internal/httpconst package gives one canonical home with no import cycle risk. Storage SQL fragments are package-local, so they live in internal/storage/sql_consts.go.

Issues intentionally NOT fixed in Phase 1

  • 2 godre:S8188 test-helper cancels (graphrag/builder_test.go, mcp/tenant_isolation_test.go): the helper returns before the workers run; immediate defer cancel() would cancel before the test body executes. The t.Cleanup pattern is correct. Will mark False Positive in SonarCloud.
  • 1 typescript:S4325 in useWebSocket.ts: as WebSocketRef adds the status field to a MutableRefObject; the cast IS structurally required. Will mark False Positive.
  • 1 godre:S8196 in vectordb/replay.go: rename ReplaySourceLogsForVectorReplayer. Domain-meaningful name preferred over rote -er suffix. Will mark False Positive.
  • 2 go:S107 (too many parameters): graphrag/store.go:UpsertLogClusterWithTemplate, storage/trace_repo.go:GetTracesFiltered. Need parameter-struct extraction — Phase 3 territory.
  • 55 go:S3776 cognitive complexity: real refactors, deferred to Phase 3.
  • 24 typescript:S7764 React hook hygiene (4 files: useWebSocket, useMediaQuery, ErrorBoundary, hook test): needs careful per-effect review + UI verification — deferred to Phase 2.

Coverage gap (separate finding)

While investigating maintainability I noticed coverage isn't visible in SonarCloud. Root cause:

  1. No sonar-project.properties in the repo.
  2. CI runs go test -race without -coverprofile.
  3. SonarCloud is on Automatic Analysis mode (GitHub App). Auto-analysis cannot ingest coverage reports — only CI-based analysis with the sonarsource/sonarcloud-github-action can.

Enabling coverage means switching analysis modes (similar weight to the 2026-04-28 reversal). Flagging here, not changing as part of this PR.

Test plan

  • go vet ./... — clean
  • go build ./... — clean
  • go test -timeout 180s ./... — 516 passed in 28 packages
  • npx tsc --noEmit -p ui/tsconfig.app.json — only pre-existing baseUrl deprecation, no new errors
  • UI Vitest delta unchanged from main (2 pre-existing failures in ServiceSidePanel.test.tsx, not introduced)
  • CI green
  • SonarCloud Quality Gate green
  • SonarCloud open code-smells: 147 → ~95

🤖 Generated with Claude Code

aksOps and others added 3 commits April 30, 2026 17:12
Mechanical fixes that resolve SonarCloud's high-volume Code Smell rules
without behaviour change. Addresses the largest, lowest-risk buckets:

go:S1192 (string literal duplicated >=3x) — 31 issues
- internal/httpconst/httpconst.go: new package for HeaderContentType
  + ContentTypeJSON, used by api/, mcp/ handlers + ingest/otlp_http
- internal/storage/sql_consts.go: shared GORM SQL fragments
  (sqlWhereTenantID, sqlOrderTimestampDesc, sqlWhereTenantTimeBetween,
  sqlWhereServiceIn, sqlWhereSeverity, sqlWhereTimestampGTE/LTE,
  cacheKeyTelemetryStart, timeFormatPGUTC)
- internal/mcp/tools.go: descFilterByService, errSvcGraphNotInit,
  errGraphRAGNotInit, errServiceRequired, resourceURIPrefix
- main.go: tlsModeCertFile / tlsModeSelfSigned
- test/{inventory,order,payment}service: serviceName

go:S8193 (unnecessary variable in condition) — 3 issues
- main.go DLQ replay: drop intermediate `err2` binding
- drain_test.go, otlp_http_backpressure_test.go: inline expression

shelldre:S7688 ([[ over [) — 3 issues
- scripts/setup-git-signed.sh: use [[ for conditional tests

typescript:S1444 (static field readonly) — 5 issues
- ui/src/hooks/__tests__/useWebSocket.test.ts: MockWebSocket constants +
  instances slice. Reset via .length = 0 instead of reassignment.

typescript:S2933 (member readonly) — 2 issues
- ui/src/components/ErrorBoundary.tsx: reset / reload arrow fields

typescript:S7735 (negated condition) — 2 issues
- ErrorBoundary.tsx: typeof X === 'undefined' guards

typescript:S4325 (redundant cast) — 2 of 3 issues
- ErrorBoundary.test.tsx: typeof narrowing already proves string
- useWebSocket.test.ts: typed MessageEvent<string> at construction
  (third occurrence in useWebSocket.ts is a structural type widening
  cast that is functionally required; left for Phase 2 consideration)

typescript:S6759 (Readonly props) — 2 issues
- TopNav.tsx, ErrorBoundary.test.tsx Boom helper

typescript:S3735 (void operator) — 2 issues
- useLogs.ts, useTraces.ts: replace `void load()` with `.catch(=> undefined)`

godre:S8188 — partial: pg_partitions_test.go split deferred cancel/Stop
into two defers per the standard idiom. The two test-helper instances
(graphrag/builder_test, mcp/tenant_isolation_test) keep t.Cleanup
because immediate `defer cancel()` would tear down workers before the
test body runs.

Verification
- go vet ./... clean
- go build ./... clean
- go test -timeout 180s ./... — 516 passed in 28 packages
- UI Vitest delta unchanged from main (2 pre-existing failures in
  ServiceSidePanel.test.tsx, not introduced by this change)

Phase 2 (S7764 React hook hygiene, ~24 issues) and Phase 3 (S3776
cognitive complexity, ~55 issues) will follow as separate PRs.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Quality Gate failed on new_duplicated_lines_density: 7.4% > 3%.
SonarCloud's CPD detector treats edited lines inside pre-existing
parallel-shape patterns as "new" and re-flags the surrounding
boilerplate. Three blocks were the bulk of the bleed:

1. test/{inventory,order,payment}service/main.go — identical 48-line
   OTel-init boilerplate across all 3 simulators. The serviceName const
   I added touched lines inside that block. Reverted to inline literals
   (re-opens 10 S1192 issues — acceptable trade for the gate).

2. internal/mcp/tools.go — toolDefs is a 24-tool schema list with
   naturally parallel shapes; the descFilterByService swap re-flagged
   surrounding 22-33 line tool-def blocks. Reverted (re-opens 3 S1192).

3. internal/storage/log_repo.go — GetLogsV2 and getLogsV2LikeFallback
   shared an identical 15-line filter-application block. Eliminated by
   extracting applyLogFilterCriteria(base, filter) — fixes the actual
   structural duplication AND keeps the const swaps.

Net Phase 1 reduction stays meaningful (~39 closed instead of ~52).

Verification
- go vet ./... clean
- go build ./... clean
- 215 storage + simulator tests pass

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@sonarqubecloud
Copy link
Copy Markdown

sonarqubecloud Bot commented May 3, 2026

@aksOps aksOps merged commit f64ac4d into main May 3, 2026
17 checks passed
@aksOps aksOps deleted the chore/sonar-phase1-cleanup branch May 3, 2026 15:34
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