Fix SonarQube high severity issues: string duplication and cognitive complexity #38
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.


Summary
Fixes two high-severity SonarQube issues in
pkg/github/repositories.goand related files:ListCommitsfunction to reduce complexity from 23 to approximately 10Changes
1. String Literal Deduplication (go:S1192)
Added three package-level constants in
repositories.go:Replaced ~100 occurrences of these duplicated strings across 8 files:
pkg/github/repositories.go(primary file with constants)pkg/github/actions.gopkg/github/code_scanning.gopkg/github/issues.gopkg/github/notifications.gopkg/github/repository_resource.gopkg/github/search.gopkg/github/secret_scanning.go2. Cognitive Complexity Reduction (go:S3776)
Refactored the
ListCommitsfunction by extracting two helper functions:extractListCommitsParams: Handles parameter validation and extraction (owner, repo, sha, author, pagination)processListCommitsResponse: Handles response processing, error checking, and marshalingThis reduces nesting levels and separates concerns, making the main handler logic clearer and easier to understand.
Tradeoffs
Constant Location: The error message constants are defined in
repositories.gobut used across multiple files in thepkg/githubpackage. While functional, a dedicatederrors.gofile might be more architecturally clean. Chose the simpler approach to minimize scope.Single-Use Helpers: The extracted helper functions are currently only used by
ListCommits. However, they demonstrate a pattern that could be applied to other high-complexity functions and make the code more testable.Testing
./script/test)./script/lint)Human Review Checklist
Please verify:
%ssubstitution works correctly (check a few examples)ListCommitsimprove readability and don't change behaviorpkg/githubpackage)ListCommits)Related