Skip to content

Conversation

@devin-ai-integration
Copy link

@devin-ai-integration devin-ai-integration bot commented Sep 22, 2025

Summary

Fixes SonarQube high-severity issues related to string literal duplication (go:S1192) and cognitive complexity (go:S3776) by implementing comprehensive refactoring of search functionality and CLI utilities.

Key Changes:

  • String duplication elimination: Added constants for repeated string literals in both files
  • Structural refactoring: Introduced generic helper functions to eliminate duplicated code patterns
  • Cognitive complexity reduction: Extracted complex logic into focused, reusable functions

Files Changed

cmd/mcpcurl/main.go

  • Added stdioServerCmdFlag constant to eliminate "stdio-server-cmd" string duplication
  • Extracted loadSchemaAndAddCommands() function to reduce complexity in main()

pkg/github/search.go

  • Added error message constants (errFailedToReadResponseBody, errFailedToMarshalResponse)
  • New helper functions:
    • buildSearchTool() - Generic tool definition builder (eliminates SearchUsers/SearchOrgs duplication)
    • buildSearchHandler() - Generic handler function builder
    • handleSearchResponse() - Centralized response processing
    • marshalAndReturnResult() - Centralized result marshaling
    • extractSearchParams() - Common parameter extraction
    • processSearchUsers() - User processing logic extraction
  • Refactored functions:
    • SearchRepositories() - Now uses generic handler pattern
    • userOrOrgHandler() - Now uses buildSearchHandler
    • SearchUsers()/SearchOrgs() - Now use buildSearchTool (major duplication reduction)

Impact

  • Duplication reduced: From 6.5% to 0.0% (well below 3% SonarCloud threshold)
  • Cognitive complexity reduced: Complex functions broken into focused helpers
  • Code maintainability improved: Common patterns centralized in reusable functions

Testing

  • ✅ All existing tests pass
  • ✅ Linting passes (0 issues)
  • ✅ Build succeeds
  • ✅ SonarCloud CI check passes

Human Review Checklist

Critical Areas to Verify:

  1. API Compatibility: Verify that MCP tool definitions (parameters, descriptions, enums) are identical before/after refactoring
  2. Error Handling: Confirm error responses and status codes match original behavior
  3. SearchCode Inconsistency: Validate why SearchCode uses inline handler while others use buildSearchHandler - ensure behavior is identical
  4. Parameter Processing: Test that extractSearchParams works correctly across all search types, especially the "q" vs "query" parameter difference
  5. User/Org Search: Verify MinimalUser result processing and type filtering ("type:user", "type:org") work correctly

Potential Regression Areas:

  • Parameter validation and error messages
  • Search result transformation and filtering
  • Response marshaling and status code handling
  • Pagination behavior consistency

Link to Devin run: https://app.devin.ai/sessions/fbfcc7c2b53b44e2afc44653733e6cbc
Requested by: @eashansinha

Closes: SonarQube duplication and complexity issues in GitHub MCP Server

- Extract constants for duplicated error messages in search.go
- Extract constant for stdio-server-cmd flag in mcpcurl/main.go
- Refactor main() function to reduce cognitive complexity from 18 to <15
- Extract processSearchUsers() helper to reduce userOrOrgHandler complexity from 40 to <15

Addresses 4 high-severity SonarQube issues:
- 2 string duplication issues (go:S1192)
- 2 cognitive complexity issues (go:S3776)

Co-Authored-By: Eashan Sinha <eashan.sinha@codeium.com>
@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

devin-ai-integration bot and others added 2 commits September 22, 2025 17:41
- Extract common patterns into helper functions:
  - handleSearchResponse() for API response handling
  - marshalAndReturnResult() for JSON marshaling
  - extractSearchParams() for parameter extraction
- Refactor SearchRepositories, SearchCode, and userOrOrgHandler
- Reduce code duplication from 6.5% to below 3% threshold
- Maintain all existing functionality and cognitive complexity fixes

Co-Authored-By: Eashan Sinha <eashan.sinha@codeium.com>
- Added buildSearchTool() helper to eliminate tool definition duplication between SearchUsers and SearchOrgs
- Added buildSearchHandler() helper to eliminate handler logic duplication across all search functions
- Refactored SearchRepositories to use generic handler pattern
- Refactored userOrOrgHandler to use buildSearchHandler pattern
- Maintained all existing functionality while reducing code duplication
- All tests and lint checks pass

Co-Authored-By: Eashan Sinha <eashan.sinha@codeium.com>
@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