Skip to content

Fix high-severity SonarQube issues in pkg/github/notifications.go - #141

Open
devin-ai-integration[bot] wants to merge 2 commits into
mainfrom
devin/1776834189-sonar-notifications-fixes
Open

devin-ai-integration[bot] wants to merge 2 commits into
mainfrom
devin/1776834189-sonar-notifications-fixes

Conversation

@devin-ai-integration

Copy link
Copy Markdown

Summary

Refactors pkg/github/notifications.go to resolve the high-severity SonarQube issues reported against the file:

  • S1192 — the duplicated error format strings

    • "failed to get GitHub client: %w" (6×)
    • "failed to read response body: %w" (4×)
    • "failed to marshal response: %w" (4×)

    are now defined as package-level constants (errFailedGitHubClient, errFailedReadResponse, errFailedMarshalResponse) and reused everywhere.

  • S3776 — cognitive complexity of ListNotifications (37), DismissNotification (19), MarkAllNotificationsRead (26), ManageNotificationSubscription (17), and ManageRepositoryNotificationSubscription (22) is reduced by:

    • Extracting each tool's inline handler closure into a named helper (listNotificationsHandler, dismissNotificationHandler, markAllNotificationsReadHandler, manageNotificationSubscriptionHandler, manageRepositoryNotificationSubscriptionHandler).
    • Extracting the per-action switch blocks into helpers (markNotificationThread, applyThreadSubscriptionAction, applyRepositorySubscriptionAction).
    • Extracting the owner/repo branching into fetchNotifications and markAllRead.
    • Introducing small shared helpers used across handlers:
      • extractOptionalStringParams / extractRequiredStringParams — batch-read string params and collapse N if err { return } blocks into one.
      • parseOptionalTime / applyOptionalTime — parse optional RFC3339 time params.
      • readErrorBodyResult — read resp.Body and turn it into a "<prefix>: <body>" tool-error result.
      • marshalToolResult — JSON-marshal a value into a text tool result.

GetNotificationDetails (not flagged) was left structurally unchanged but opportunistically uses the new constants/helpers for consistency.

This is intended to be a pure refactor — no behavior changes, same error messages, same status-code handling (including the existing nil-resp special case in ManageRepositoryNotificationSubscription and the pre-API-error-check early-return on a bad threadID in DismissNotification).

Validation

  • go build ./... — passes
  • go vet ./... — clean
  • go test ./... — all packages pass, including pkg/github notification tests

Reviewer checklist / things worth a close look

  • Behavior preservation in the extracted switches. Please confirm the three new helpers preserve original semantics, especially:
    • markNotificationThread: an invalid threadID int still returns the "invalid threadID format" tool error without hitting the downstream ghErrors.NewGitHubAPIErrorResponse branch.
    • applyThreadSubscriptionAction / applyRepositorySubscriptionAction: the default case (invalid action) still returns before the apiErr/resp handling.
  • ManageRepositoryNotificationSubscription nil-resp handling is retained (if resp != nil { defer … } and if resp != nil && (…)) — worth a second look since the call-site now receives resp via a helper return.
  • Shared helpers live in notifications.go. If any of extractOptionalStringParams, extractRequiredStringParams, parseOptionalTime, readErrorBodyResult, or marshalToolResult would be better placed in server.go / a shared file for reuse by other tools, happy to move them.
  • SonarQube threshold. Complexity was reduced by hand against Sonar's rules but not re-measured by the scanner; if any function still trips the 15 threshold, let me know and I'll push a follow-up.

Closes:

Link to Devin session: https://app.devin.ai/sessions/3b5e549e0be14ef09767ef3d337bd5eb
Requested by: @parkerduff

…rror strings

Addresses SonarQube issues on pkg/github/notifications.go:
- S1192: extract 'failed to get GitHub client', 'failed to read response body',
  and 'failed to marshal response' error format strings into package-level
  constants.
- S3776: reduce cognitive complexity of ListNotifications,
  DismissNotification, MarkAllNotificationsRead, ManageNotificationSubscription,
  and ManageRepositoryNotificationSubscription by extracting the tool handler
  closures into named functions and introducing shared helpers for
  parameter extraction, optional time parsing, error-body reading, and
  JSON marshalling of tool results.

Co-Authored-By: parker.duff@codeium.com <pwjduff@gmail.com>
@devin-ai-integration

Copy link
Copy Markdown
Author

🤖 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 and CI monitoring

manageRepositoryNotificationSubscriptionHandler was still at 17 per
SonarQube (target: 15) because it combined a deferred body close with
two separate 'resp != nil' branches for the status check. Extract the
close + 2xx check into a shared helper and reuse it in
manageNotificationSubscriptionHandler, which also removes a duplicated
status-code error-body pattern.

Co-Authored-By: parker.duff@codeium.com <pwjduff@gmail.com>
@sonarqubecloud

Copy link
Copy Markdown

Quality Gate Passed Quality Gate passed

Issues
0 New issues
0 Accepted issues

Measures
0 Security Hotspots
0.0% Coverage on New Code
0.0% Duplication on New Code

See analysis details on SonarQube Cloud

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.

0 participants