Fix high-severity SonarQube issues (S1192, S3776) in notifications.go - #148
eashansinha wants to merge 1 commit into
Conversation
Resolve S1192 (duplicated string literals) and S3776 (cognitive complexity) in pkg/github/notifications.go: - Add package-level constants for repeated error formats and tool parameter names. - Extract handler logic out of the tool closures into named functions and add shared helpers (marshalToolResult, responseErrorResult, parseOptionalTimestamp, parseListNotificationsParams, setThreadSubscription, setRepositorySubscription) to bring every function's cognitive complexity well under the threshold. Behavior and error messages are unchanged; existing tests pass. Co-Authored-By: Eashan Sinha <eashan.sinha@cognition.ai>
Original prompt from Eashan
|
🤖 Devin AI EngineerI'll be helping with this pull request! Here's what you should know: ✅ I will automatically:
Note: I can only respond to comments from users who have write access to this repository. ⚙️ Control Options:
|
|
| lastReadTime, errResult := parseOptionalTimestamp(lastReadAt, paramLastReadAt) | ||
| if errResult != nil { | ||
| return errResult, nil | ||
| } | ||
| if lastReadTime.IsZero() { | ||
| lastReadTime = time.Now() |
There was a problem hiding this comment.
🟡 Zero RFC3339 timestamps are treated as omitted
parseOptionalTimestamp returns the zero time.Time both when a timestamp argument is omitted and when the caller provides the valid RFC3339 value 0001-01-01T00:00:00Z; the handlers then use IsZero() as the “not provided” check. For mark_all_notifications_read, that means an explicit lastReadAt of 0001-01-01T00:00:00Z is replaced with time.Now(), so the tool marks current notifications as read instead of honoring the supplied cutoff. The same sentinel pattern also causes explicit zero values for since/before in list_notifications to be silently dropped at pkg/github/notifications.go:165-178.
Prompt for agents
The notification timestamp parsing now uses a zero time as the sentinel for an omitted argument, but a parsed RFC3339 zero timestamp is also a zero time. Update the timestamp handling in pkg/github/notifications.go so callers distinguish “argument not provided” from “argument provided and parsed to time.Time{}”. For markAllNotificationsReadHandler, only default to time.Now() when lastReadAt is the empty string. For listNotificationsHandler, set opts.Since/opts.Before whenever the corresponding raw parameter is non-empty and parsing succeeded, even if the parsed time is IsZero(). Consider changing parseOptionalTimestamp to return an additional boolean indicating whether a value was provided.
Was this helpful? React with 👍 or 👎 to provide feedback.



Summary
Resolves the high-severity SonarQube findings in
pkg/github/notifications.go:No behavior or error-message changes — purely a refactor. All existing tests pass.
What changed
S1192 — constants for repeated literals:
errFailedToGetClient,errFailedToReadBody,errFailedToMarshal.paramOwner,paramRepo,paramAction,paramNotificationID,paramSince,paramBefore,paramLastReadAt.S3776 — reduce cognitive complexity:
The tool-factory functions previously inlined all logic inside the returned handler closure, which inflated cognitive complexity (nested-function nesting penalty). Each handler body is now a named package-level function (e.g.
listNotificationsHandler), and shared logic is extracted into helpers:marshalToolResult(v)— JSON-marshal + text result.responseErrorResult(resp, message)— read body + error result on unexpected status.parseOptionalTimestamp(value, field)— optional RFC3339 parsing.parseListNotificationsParams(request)— extracts thelist_notificationsparams.setThreadSubscription(...)/setRepositorySubscription(...)— theignore/watch/deleteswitch.Cognitive complexity (gocognit) before → after:
All handlers are now ≤ 12.
Verification
go build ./...,go vet ./pkg/github/— cleango test -race ./...— passgolangci-lint run ./pkg/github/(v2.1) — 0 issuesTradeoffs / Alternatives
owner,repo) are file-local to this change to match SonarQube's per-file duplication scope; broader codebase adoption is out of scope here.Link to Devin session: https://app.devin.ai/sessions/ad357e445c824ab194f9ab6c7a782484
Requested by: @eashansinha
Devin Review
1dfcc2b