Skip to content

Fix SonarQube issues: duplicated strings and cognitive complexity#71

Open
devin-ai-integration[bot] wants to merge 2 commits intomainfrom
devin/1767811183-sonarqube-fixes
Open

Fix SonarQube issues: duplicated strings and cognitive complexity#71
devin-ai-integration[bot] wants to merge 2 commits intomainfrom
devin/1767811183-sonarqube-fixes

Conversation

@devin-ai-integration
Copy link

@devin-ai-integration devin-ai-integration bot commented Jan 7, 2026

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

Changes

1. Duplicated String Literals (go:S1192)

Replaced hardcoded "Repository owner" and "Repository name" strings with existing constants DescriptionRepositoryOwner and DescriptionRepositoryName from actions.go across all discussion-related functions.

2. Cognitive Complexity (go:S3776)

Refactored ListDiscussions function to reduce cognitive complexity from 27 to approximately 10 by extracting:

  • discussionNode type - shared struct for GraphQL response
  • discussionNodeToIssue() - converts a discussion node to a GitHub Issue
  • fetchDiscussions() - consolidated helper that handles both category-filtered and unfiltered queries

Updates Since Last Revision

  • Consolidated fetchDiscussionsWithCategory() and fetchDiscussionsWithoutCategory() into a single fetchDiscussions() function to reduce code duplication

Human Review Checklist

  • Verify the extracted discussionNode type correctly matches the GraphQL response structure for both query variants
  • Confirm the fetchDiscussions() helper maintains behavioral equivalence with the original inline code
  • Check error handling paths are preserved correctly
  • Note: SonarCloud duplication check shows 4.9% (required ≤3%) - this is due to inherent duplication in GraphQL query struct definitions which cannot be unified due to different struct tags required by the githubv4 library. This check is not marked as required.

Tradeoffs

  • The two GraphQL query structs inside fetchDiscussions() have similar structure but cannot be unified due to different GraphQL query signatures (with/without categoryId parameter) - this is a constraint of the githubv4 library's struct tag-based query definition.
  • Net reduction of ~19 lines while improving code organization and reducing cognitive complexity.

Alternatives Considered

  • Attempted to create two separate fetch helper functions, but this increased duplication metrics. Consolidated into single function with internal branching.
  • Cannot use a single GraphQL query struct due to library constraints on struct tag definitions.

Link to Devin run: https://app.devin.ai/sessions/0e8426348b5a4cebb73a47f5441f8658
Requested by: parker.duff@codeium.com (@parkerduff)

- Replace hardcoded 'Repository owner' and 'Repository name' strings with
  existing constants DescriptionRepositoryOwner and DescriptionRepositoryName
- Refactor ListDiscussions function to reduce cognitive complexity from 27 to ~10
  by extracting helper functions: discussionNodeToIssue, fetchDiscussionsWithCategory,
  and fetchDiscussionsWithoutCategory

Fixes:
- go:S1192 (String literals should not be duplicated) - HIGH severity
- go:S3776 (Cognitive Complexity should not be too high) - HIGH severity

Co-Authored-By: parker.duff@codeium.com <pwjduff@gmail.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

Combine fetchDiscussionsWithCategory and fetchDiscussionsWithoutCategory into
a single fetchDiscussions function to reduce code duplication and address
SonarCloud duplication check failure.

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

sonarqubecloud bot commented Jan 7, 2026

Quality Gate Failed Quality Gate failed

Failed conditions
4.9% 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.

0 participants