Skip to content

Conversation

@devin-ai-integration
Copy link

Summary

This PR fixes two high-severity SonarQube issues related to code quality:

  1. String literal duplication (go:S1192): Eliminated duplicated error messages by extracting them into constants
  2. Cognitive complexity (go:S3776): Reduced complexity of the GetFileContents function by breaking it into smaller, focused helper functions

Changes

1. String Duplication Fix

  • Created pkg/github/constants.go with common error message templates
  • Replaced 20+ instances of hardcoded strings like "failed to get GitHub client: %w" and "failed to marshal response: %w" across actions.go and repositories.go

2. Cognitive Complexity Reduction

  • Refactored the large GetFileContents function (complexity 86) into smaller helper functions:
    • extractFileContentParams - parameter extraction and validation
    • resolvePullRequestRef - pull request reference resolution
    • createResourceURI - resource URI generation
    • tryGetRawContent - raw content fetching with proper content type handling
    • getContentViaAPI - GitHub API fallback for content retrieval
  • Added FileContentParams struct to encapsulate function parameters

Key Considerations for Review

⚠️ Critical Areas to Verify:

  1. GetFileContents behavior preservation - The function refactoring is extensive. Please verify that:
    • Binary content is still base64-encoded correctly
    • Text content maintains proper MIME type handling
    • Pull request reference resolution works as expected
    • Directory listing functionality is preserved
  2. Error message consistency - Confirm that the extracted constants produce meaningful error messages in all contexts
  3. Test coverage - All existing tests should pass without modification

Tradeoffs:

  • Pro: Significantly improved code maintainability and reduced duplication
  • Pro: Lower cognitive load for future developers working on file content logic
  • Con: Slightly more complex call stack due to function extraction
  • Con: Additional indirection through constants for error messages

Alternatives Considered:

  • Could have used a more granular approach with separate constants files per domain, but chose a single file for simplicity
  • Could have used error wrapping instead of format strings, but maintained consistency with existing patterns

Testing

  • ✅ All existing tests pass
  • ✅ Linting passes with 0 issues
  • ✅ Build succeeds without warnings
  • ✅ No behavioral changes to public APIs

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

Closes: N/A (SonarQube code quality improvement)

…complexity

- Create constants.go with common error message templates to eliminate string duplication (go:S1192)
- Extract helper functions from GetFileContents to reduce cognitive complexity from 86 (go:S3776)
- Replace 'failed to get GitHub client: %w' and 'failed to marshal response: %w' with constants
- Refactor GetFileContents into smaller functions: extractFileContentParams, resolvePullRequestRef, createResourceURI, tryGetRawContent, getContentViaAPI
- All tests pass and linting is clean

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 3 commits September 22, 2025 16:24
- Replace all instances of 'failed to get GitHub client: %w' with ErrFailedToGetGitHubClient constant
- Replace all instances of 'failed to marshal response: %w' with ErrFailedToMarshalResponse constant
- Updated 8 files: actions.go, code_scanning.go, issues.go, notifications.go, pullrequests.go, repository_resource.go, search.go, secret_scanning.go
- This should resolve SonarCloud CI failure showing 5.8% duplication on new code
- All tests pass and linting is clean

Co-Authored-By: Eashan Sinha <eashan.sinha@codeium.com>
- Remove duplicate constants from actions.go that were causing increased duplication
- Add comprehensive error message and description constants to constants.go
- Replace all instances of duplicated strings with constants across the codebase
- Fix string duplications in error messages, descriptions, and other literals
- Ensure consistent usage of constants for Repository owner/name, Sort order, etc.
- Address remaining duplications identified in SonarQube analysis

Co-Authored-By: Eashan Sinha <eashan.sinha@codeium.com>
…onstants used 3+ times

- Reduced constants.go from 31 to 5 constants focusing on most duplicated strings
- Reverted low-value constants back to inline strings to reduce duplication overhead
- Fixed undefined ErrFailedToGetLatestReview references in pullrequests.go
- Completed string replacements in docs/error-handling.md and internal/ghmcp/server.go
- Successfully replaced 'repo://' with RepoURIPrefix constant in repository_resource.go
- All lint and test checks pass locally

Co-Authored-By: Eashan Sinha <eashan.sinha@codeium.com>
@sonarqubecloud
Copy link

Quality Gate Failed Quality Gate failed

Failed conditions
27.8% Duplication on New Code (required ≤ 3%)

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.

1 participant