Skip to content

Conversation

@devin-ai-integration
Copy link

Summary

This PR addresses two High severity SonarQube issues in pkg/github/discussions.go:

  • go:S1192: String literals should not be duplicated
  • go:S3776: Cognitive Complexity of functions should not be too high

Changes

String Duplication Fix

Added constants for repeated string literals that appeared 3-4 times across the file:

  • descRepositoryOwner - "Repository owner" (4 occurrences)
  • descRepositoryName - "Repository name" (4 occurrences)
  • errFailedToGetGQLClient - "failed to get GitHub GQL client: %v" (4 occurrences)
  • categoryLabelFormat - "category:%s" (3 occurrences)

Cognitive Complexity Reduction

Refactored ListDiscussions function (complexity 27 → ~10) by extracting shared logic:

  • Created discussionNode type for GraphQL response structure
  • Extracted convertDiscussionNodeToIssue() helper to eliminate duplicate mapping code
  • Extracted queryDiscussionsWithCategory() and queryDiscussionsWithoutCategory() helpers to separate query logic
  • Simplified main function flow to use helper functions

Testing

  • ✅ All tests pass (./script/test)
  • ✅ Linting passes with 0 issues (./script/lint)

Review Checklist

Critical items to verify:

  1. GraphQL structure: Confirm the discussionNode struct correctly matches both query responses (with/without category filter)
  2. Field mapping: Verify convertDiscussionNodeToIssue preserves all original field mappings and formatting
  3. Behavior equivalence: The refactored logic should produce identical results to the original nested if-else structure
  4. Error handling: Ensure error paths are equivalent to the original implementation

Tradeoffs

  • Added helper functions increase file length slightly (+68 net lines) but significantly improve maintainability
  • Extracted shared struct type (discussionNode) couples the two query paths, but this is acceptable since they query the same data structure

Link to Devin run: https://app.devin.ai/sessions/d642e178617b4b5897327bf05d009d45
Requested by: Shawn Azman (@ShawnAzman)

- Add constants for repeated string literals (Repository owner, Repository name, error messages, category format)
- Refactor ListDiscussions function to reduce cognitive complexity from 27 to below 15
- Extract helper functions: convertDiscussionNodeToIssue, queryDiscussionsWithCategory, queryDiscussionsWithoutCategory
- Create discussionNode type to eliminate code duplication

Fixes:
- go:S1192: String literals should not be duplicated
- go:S3776: Cognitive Complexity of functions should not be too high
@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

@ShawnAzman
Copy link

Update the readme

@devin-ai-integration
Copy link
Author

Could you please clarify what you'd like me to update in the README? The PR only modifies pkg/github/discussions.go to fix SonarQube issues.

Are you asking me to:

  1. Add a section about code quality improvements?
  2. Document the refactoring changes?
  3. Update something else specific?

Please let me know what changes you'd like to see!

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.

2 participants