Skip to content

refactor: reduce cognitive complexity of userOrOrgHandler (40 → ~12) - #144

Open
devin-ai-integration[bot] wants to merge 1 commit into
mainfrom
devin/1777981971-refactor-search-handler
Open

devin-ai-integration[bot] wants to merge 1 commit into
mainfrom
devin/1777981971-refactor-search-handler

Conversation

@devin-ai-integration

@devin-ai-integration devin-ai-integration Bot commented May 5, 2026

Copy link
Copy Markdown

Closes: SonarQube issue go:S3776 on pkg/github/search.go:171

Summary

The userOrOrgHandler function had a cognitive complexity of 40 (allowed: 15) due to nested nil-check conditionals when converting GitHub user objects. This PR extracts focused helper functions.

Extracted Functions

Function Responsibility
toMinimalUser Converts a single github.User to MinimalUser, handling nil fields
convertUsersToMinimal Filters users without login and converts the slice
buildMinimalSearchResult Constructs the MinimalSearchUsersResult response

Tradeoffs

  • The main handler now reads linearly: parse params → search → convert → marshal.
  • Uses user.GetLogin() (nil-safe getter) instead of raw pointer check for cleaner code.

Alternatives

  • Considered inlining just the loop body, but that only reduced CC to ~25.
  • Considered a method on MinimalUser but the conversion is a one-way transform, not behavior on the type.

Link to Devin session: https://app.devin.ai/sessions/dc69091bf9e24bd2871c88e47fa1cb98
Requested by: @VedantKh


Devin Review

Status Commit
⚪ Not started

Run Devin Review

💡 Connect your GitHub account to enable automatic code reviews.

Open in Devin Review (Staging)
Open in Devin Review

Extract helper functions from userOrOrgHandler to reduce cognitive
complexity from 40 to within the allowed threshold of 15.

Extracted functions:
- toMinimalUser: converts github.User to MinimalUser
- convertUsersToMinimal: filters and converts user slice
- buildMinimalSearchResult: constructs the search response

Fixes SonarQube issue go:S3776 on search.go:171

Co-Authored-By: Vedant Khanna <vedantkhanna@gmail.com>
@devin-ai-integration

Copy link
Copy Markdown
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

sonarqubecloud Bot commented May 5, 2026

Copy link
Copy Markdown

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no potential bugs to report.

View in Devin Review to see 2 additional findings.

Open in Devin Review

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