Skip to content

Conversation

@devin-ai-integration
Copy link

Closes: MBA-471

Summary

This PR resolves two High severity SonarQube violations in the codebase:

  1. go:S1192 - String literals should not be duplicated
  2. go:S3776 - Cognitive Complexity of functions should not be too high

Changes

1. String Duplication Fix (go:S1192)

Created pkg/errors/constants.go with three error message constants:

  • ErrContextMissingGitHubCtxErrors - eliminates 4 duplicate instances in error.go
  • ErrMissingRequiredParameter - eliminates 2 duplicate instances in server.go
  • ErrFailedToGetGitHubClient - added for future consistency

Updated pkg/errors/error.go:

  • Replaced hardcoded string literals at lines 75, 83, 99, 107 with the constant
  • Changed from fmt.Errorf() to errors.New() to fix staticcheck SA1006 warning (no format verbs in the string)

Updated pkg/github/server.go:

  • Added import for pkg/errors (aliased as pkgerrors to avoid conflict with stdlib)
  • Replaced hardcoded string literals at lines 74, 84 with the constant

2. Cognitive Complexity Reduction (go:S3776)

Refactored get_file_contents handler in pkg/github/repositories.go:

Extracted three helper functions to reduce nesting from 5+ levels to 2-3:

  • tryRawContentFetch() - handles raw content API call and client initialization
  • buildResourceURI() - constructs resource URI based on sha/ref parameters
  • createResourceContent() - creates appropriate resource content type (text vs binary)

The refactoring improves code readability and maintainability while preserving all existing functionality.

Testing

  • ✅ All unit tests pass (./script/test)
  • ✅ Lint passes with 0 issues (./script/lint)
  • ✅ Existing tests using assert.Contains() work correctly with constant-based errors

Review Checklist

Key areas to review:

  • Verify the refactored get_file_contents logic behaves identically to the original, especially:
    • Raw content fetch flow and error handling
    • Resource URI construction for different scenarios (sha, ref, default)
    • Content type detection and response creation
  • Confirm error messages remain user-friendly and contextually appropriate
  • Validate that helper functions are appropriately scoped (package-private)

Tradeoffs

  • Cross-package dependency: pkg/github now imports pkg/errors for constants. This is a reasonable design choice that centralizes error messages and improves maintainability.
  • Helper function signatures: createResourceContent() returns (*mcp.CallToolResult, error) but always returns nil for error. This maintains consistency with Go patterns and handler expectations, though the error return is currently unused.

Session Details

…omplexity issues

- Extract error message constants to pkg/errors/constants.go
  - ErrContextMissingGitHubCtxErrors for context error messages
  - ErrFailedToGetGitHubClient for client initialization errors
  - ErrMissingRequiredParameter for parameter validation errors

- Update pkg/errors/error.go to use ErrContextMissingGitHubCtxErrors constant
  at lines 75, 83, 99, 107 (changed from fmt.Errorf to errors.New to fix
  staticcheck SA1006)

- Update pkg/github/server.go to use ErrMissingRequiredParameter constant
  at lines 74, 84

- Refactor get_file_contents handler in pkg/github/repositories.go to reduce
  cognitive complexity by extracting helper functions:
  - tryRawContentFetch: handles raw content API call
  - buildResourceURI: constructs resource URI based on sha/ref
  - createResourceContent: creates appropriate resource content type

This reduces nesting levels from 5+ to 2-3, improving code maintainability.

Addresses SonarQube High severity violations:
- go:S1192 - String literals should not be duplicated
- go:S3776 - Cognitive Complexity of functions should not be too high

All tests pass and lint is clean.

Co-Authored-By: Jia Wu <jia_wu@hotmail.ca>
@devin-ai-integration
Copy link
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

@sonarqubecloud
Copy link

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