Fix high-severity SonarQube issues in notifications.go (S1192, S3776) - #151
eashansinha wants to merge 1 commit into
Conversation
S1192: extract repeated string literals into package-level constants (owner/action/notificationID param names and shared error formats). S3776: reduce cognitive complexity of ListNotifications and MarkAllNotificationsRead by extracting helpers (buildNotificationListOptions, listNotificationsByScope, parseLastReadAt, notificationResponseError). 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:
|
|



Summary
Resolves the 8 high-severity SonarQube findings in
pkg/github/notifications.go(6× S1192 duplicated string literals, 2× S3776 cognitive complexity) without any behavioral change. All existing tests,go vet,gofmt, andgolangci-lint(v2.1) pass.S1192 — package-level constants for repeated literals
Each of these literals appeared 4–6 times in the file; SonarQube raises one issue per duplicated literal. Extracted to package-level
consts:The param-name constants replace the literals in
mcp.WithString(...)/RequiredParam/OptionalParamcall sites; the error formats replace thefmt.Errorfliterals.S3776 — reduce cognitive complexity
Only
ListNotifications(~20) andMarkAllNotificationsRead(~16) exceeded the threshold of 15. Extracted four helpers so each handler body now sits at ~11:buildNotificationListOptions(filter, since, before, pagination)— moves option assembly + RFC3339since/beforeparsing out of the handler.listNotificationsByScope(ctx, client, owner, repo, opts)— collapses the repo-vs-all branch.parseLastReadAt(lastReadAt)— defaults totime.Now()when empty, else parses RFC3339.notificationResponseError(resp, message)— reads the body and builds the error result, removing the repeated nestedif err := io.ReadAll(...)block from the four handlers that used it.Net effect on
ListNotificationshandler:Error strings and tool-result messages are byte-for-byte identical to before (e.g.
parseLastReadAt's error is still wrapped with the sameinvalid lastReadAt time format...text), so behavior and the existing test assertions are unchanged.Notes / tradeoffs
Dismiss,ManageNotificationSubscription,ManageRepositoryNotificationSubscription,GetNotificationDetails) were already under the complexity threshold; only the constant swaps and the sharednotificationResponseErrorhelper touch them, keeping the diff focused.gocognitreports higher numbers because it folds handler closures into the enclosing function with a nesting penalty; SonarQube analyzes each closure as its own function (nesting 0), which is why only the two handlers above were flagged.Verification
go build ./...go test ./pkg/github/— passgofmt -l .(clean) +go vet ./...golangci-lint run ./pkg/github/(v2.1.6) — 0 issuesLink to Devin session: https://app.devin.ai/sessions/a3f594efd7014724a96b1236c9d54964
Requested by: @eashansinha
Devin Review
d982358