Skip to content

Conversation

@JAORMX
Copy link
Collaborator

@JAORMX JAORMX commented Oct 14, 2025

Summary

Refactor the buildRunnerConfig function to reduce its cyclomatic complexity from 16 to below the gocyclo threshold of 15.

Changes

  • Extract middleware configuration logic into configureMiddlewareAndOptions helper function
  • Extract remote authentication logic into configureRemoteAuth helper function
  • Improve code organization and maintainability
  • All existing functionality is preserved

Test plan

  • Linter passes with 0 issues (task lint-fix)
  • Unit tests pass (task test)
  • No behavioral changes - purely refactoring for code quality

Technical Details

The buildRunnerConfig function at cmd/thv/app/run_flags.go:390 had a cyclomatic complexity of 16, which exceeded the configured threshold of 15. This was causing the gocyclo linter check to fail.

The refactoring extracts conditional logic into focused helper functions:

  1. configureMiddlewareAndOptions - Handles middleware setup, authorization config loading, OIDC/telemetry configuration, and environment file processing
  2. configureRemoteAuth - Handles remote server metadata authentication and direct remote URL authentication

This reduces complexity while maintaining clear separation of concerns and improving code readability.

🤖 Generated with Claude Code

Extract middleware configuration and remote authentication logic into
separate helper functions to reduce the cyclomatic complexity of
buildRunnerConfig from 16 to below the gocyclo threshold of 15.

Changes:
- Add configureMiddlewareAndOptions helper function to handle middleware
  setup, authorization config loading, and environment file processing
- Add configureRemoteAuth helper function to handle remote server
  metadata and direct remote URL authentication configuration
- Maintain all existing functionality while improving code organization

Fixes linter error: "cyclomatic complexity 16 of func buildRunnerConfig
is high (> 15)"

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
@codecov
Copy link

codecov bot commented Oct 14, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 53.08%. Comparing base (33c5421) to head (2518fd4).
⚠️ Report is 3 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2186      +/-   ##
==========================================
- Coverage   53.13%   53.08%   -0.05%     
==========================================
  Files         222      222              
  Lines       28904    28904              
==========================================
- Hits        15359    15345      -14     
- Misses      12402    12420      +18     
+ Partials     1143     1139       -4     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR refactors the buildRunnerConfig function to reduce its cyclomatic complexity from 16 to 15 by extracting conditional logic into two helper functions. The refactoring improves code organization without changing behavior.

Key Changes:

  • Extracted middleware configuration logic into configureMiddlewareAndOptions helper function
  • Extracted remote authentication logic into configureRemoteAuth helper function

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@JAORMX
Copy link
Collaborator Author

JAORMX commented Oct 14, 2025

@claude please review this

@claude
Copy link
Contributor

claude bot commented Oct 14, 2025

Claude finished @JAORMX's task —— View job


Code Review Complete

Tasks

  • Read the modified file and understand the refactoring
  • Analyze the changes and verify the compilation issue reported by copilot
  • Provide comprehensive review feedback

Review Summary

This PR successfully achieves its goal of reducing cyclomatic complexity through well-structured extraction of helper functions. The refactoring improves code organization and readability. However, there is a critical compilation error that needs to be addressed.

Critical Issue: Compilation Error ❌

Location: cmd/thv/app/run_flags.go:551, cmd/thv/app/run_flags.go:595, cmd/thv/app/run_flags.go:599, cmd/thv/app/run_flags.go:639

Problem: The function getRemoteAuthFromRemoteServerMetadata relies on a global variable runFlags (defined in run.go:100), but it's being called from the new helper function configureRemoteAuth which receives runFlags as a parameter. This creates a mismatch where:

  1. Line 551 calls getRemoteAuthFromRemoteServerMetadata(remoteServerMetadata) without passing runFlags
  2. Inside getRemoteAuthFromRemoteServerMetadata at lines 595, 599, and 639, it references the global runFlags variable
  3. The local parameter runFlags in configureRemoteAuth shadows the global variable in that scope, but getRemoteAuthFromRemoteServerMetadata doesn't have access to the parameter

Impact: This code will not compile correctly when the global runFlags is not in the same package scope, or could lead to unexpected behavior where the global variable is used instead of the parameter passed to the function.

Solution: Update getRemoteAuthFromRemoteServerMetadata to accept runFlags as a parameter:

// Line 551 - update the call site
remoteAuthConfig := getRemoteAuthFromRemoteServerMetadata(remoteServerMetadata, runFlags)

// Line 593 - update the function signature
func getRemoteAuthFromRemoteServerMetadata(remoteServerMetadata *registry.RemoteServerMetadata, runFlags *RunFlags) *runner.RemoteAuthConfig {

This was correctly identified by the Copilot reviewer as well.


Positive Aspects ✅

  1. Clear Separation of Concerns: The extraction of configureMiddlewareAndOptions and configureRemoteAuth follows good software engineering principles
  2. Improved Readability: The main buildRunnerConfig function is now much easier to understand at a glance
  3. Descriptive Naming: Helper function names clearly describe their purpose
  4. Preserved Functionality: The refactoring maintains the same logical flow
  5. Good Documentation: Helper functions have clear comments explaining their purpose

Additional Observations

  1. Cyclomatic Complexity: The refactoring does reduce complexity in buildRunnerConfig, but I notice that configureMiddlewareAndOptions is now quite lengthy (lines 469-544, ~75 lines). While this is acceptable, consider whether some of its sub-sections (like OIDC/telemetry legacy config setup) could be further extracted if complexity grows in the future.

  2. Error Handling: Error handling is consistent and properly propagated through the helper functions.

  3. Test Coverage: According to Codecov, there's a slight coverage decrease (-0.02%), though the report notes "All modified and coverable lines are covered by tests." The decrease appears to be in unrelated areas (misses +2, partials +1), which might be a statistical fluctuation.


Recommendation

Request Changes: The compilation error must be fixed before this can be merged. Once the runFlags parameter is properly passed to getRemoteAuthFromRemoteServerMetadata, this refactoring will be ready to merge.


Job Run | Branch: fix-linter-cyclomatic-complexity

amirejaz
amirejaz previously approved these changes Oct 14, 2025
Copy link
Member

@rdimitrov rdimitrov left a comment

Choose a reason for hiding this comment

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

blocking until we fix the issue

@rdimitrov rdimitrov requested a review from Copilot October 14, 2025 12:11
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 1 comment.


Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@JAORMX JAORMX enabled auto-merge (squash) October 14, 2025 12:15
@JAORMX JAORMX merged commit 47e5fd8 into main Oct 14, 2025
33 checks passed
@JAORMX JAORMX deleted the fix-linter-cyclomatic-complexity branch October 14, 2025 12:20
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.

4 participants