Skip to content

Fix high-severity SonarQube issues in notifications.go (S1192, S3776) - #151

Open
eashansinha wants to merge 1 commit into
mainfrom
devin/1781892966-sonarqube-notifications
Open

eashansinha wants to merge 1 commit into
mainfrom
devin/1781892966-sonarqube-notifications

Conversation

@eashansinha

@eashansinha eashansinha commented Jun 19, 2026

Copy link
Copy Markdown

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, and golangci-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:

const ( ownerParam = "owner"; actionParam = "action"; notificationIDParam = "notificationID" )
const (
    errFailedToGetClient       = "failed to get GitHub client: %w"
    errFailedToReadBody        = "failed to read response body: %w"
    errFailedToMarshalResponse = "failed to marshal response: %w"
)

The param-name constants replace the literals in mcp.WithString(...) / RequiredParam / OptionalParam call sites; the error formats replace the fmt.Errorf literals.

S3776 — reduce cognitive complexity

Only ListNotifications (~20) and MarkAllNotificationsRead (~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 + RFC3339 since/before parsing out of the handler.
  • listNotificationsByScope(ctx, client, owner, repo, opts) — collapses the repo-vs-all branch.
  • parseLastReadAt(lastReadAt) — defaults to time.Now() when empty, else parses RFC3339.
  • notificationResponseError(resp, message) — reads the body and builds the error result, removing the repeated nested if err := io.ReadAll(...) block from the four handlers that used it.

Net effect on ListNotifications handler:

opts, err := buildNotificationListOptions(filter, since, before, paginationParams)
if err != nil { return mcp.NewToolResultError(err.Error()), nil }

notifications, resp, err := listNotificationsByScope(ctx, client, owner, repo, opts)
...
if resp.StatusCode != http.StatusOK {
    return notificationResponseError(resp, "failed to get notifications")
}

Error strings and tool-result messages are byte-for-byte identical to before (e.g. parseLastReadAt's error is still wrapped with the same invalid lastReadAt time format... text), so behavior and the existing test assertions are unchanged.

Notes / tradeoffs

  • The other handlers (Dismiss, ManageNotificationSubscription, ManageRepositoryNotificationSubscription, GetNotificationDetails) were already under the complexity threshold; only the constant swaps and the shared notificationResponseError helper touch them, keeping the diff focused.
  • gocognit reports 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/ — pass
  • gofmt -l . (clean) + go vet ./...
  • golangci-lint run ./pkg/github/ (v2.1.6) — 0 issues

Link to Devin session: https://app.devin.ai/sessions/a3f594efd7014724a96b1236c9d54964
Requested by: @eashansinha


Devin Review

Status Commit
🟢 Reviewed d982358
Open in Devin Review (Staging)

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>
@devin-ai-integration

Copy link
Copy Markdown
Original prompt from Eashan

Fix the following high severity SonarQube issues in @COG-GTM/github-mcp-server :
Repository: COG-GTM/github-mcp-server File: pkg/github/notifications.go Issues (8 total):

Fix these issues by:
For S1192 (duplicated strings): Define constants at the package level for repeated string literals
For S3776 (cognitive complexity): Refactor complex methods by extracting helper functions
Run tests after fixes to ensure nothing breaks.

@devin-ai-integration

Copy link
Copy Markdown

🤖 Devin AI Engineer

I'll be helping with this pull request! Here's what you should know:

✅ I will automatically:

  • Address comments on this PR. Add '(aside)' to your comment to have me ignore it.
  • Look at CI failures and help fix them

Note: I can only respond to comments from users who have write access to this repository.

⚙️ Control Options:

  • Disable automatic comment, CI, and merge conflict monitoring

@sonarqubecloud

Copy link
Copy Markdown

@staging-devin-ai-integration staging-devin-ai-integration 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.

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no bugs or issues to report.

Open in Devin Review (Staging)
Debug

Playground

@devin-ai-integration devin-ai-integration 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.

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no bugs or issues to report.

Open in Devin Review

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