Skip to content

refactor: reduce code duplication flagged by SonarQube#24

Merged
omattsson merged 4 commits intomainfrom
fix/sonarqube-maintainability
Mar 29, 2026
Merged

refactor: reduce code duplication flagged by SonarQube#24
omattsson merged 4 commits intomainfrom
fix/sonarqube-maintainability

Conversation

@omattsson
Copy link
Copy Markdown
Owner

@omattsson omattsson commented Mar 29, 2026

Summary

Addresses all 12 SonarQube maintainability issues (duplicated string literals) and reduces overall code duplication from 3.2% to 0.8%.

Changes

Constants for duplicated string literals

  • cli/pkg/client/client.go: Extract API path format strings into constants (pathDefinition, pathOverride, pathBranchOverride, pathQuotaOverride)
  • cli/cmd/bulk.go: Extract flagDescIDs constant
  • cli/cmd/override.go: Extract msgAborted, flagDescSkipConfirm constants
  • cli/cmd/definition.go: Extract flagFromFile, msgPathTraversal constants; add readFileErr() helper for duplicated file-read error wrapping

Shared helper functions to reduce structural duplication

  • cli/cmd/root.go: Add confirmAction() — extracts the 8-instance confirmation prompt pattern (parse --yes flag, prompt, read stdin, check answer)
  • cli/cmd/root.go: Add deleteByID() — extracts the- cli/cmd/root.go: Add deleteByID() — extracts the- **cli� - **cli/�� output) use- cli/cmd/root.go: Add deleteByID() — extracts the- cli/cmd/root.go: Add deleteByID() — extraccts the shared two-ID override del- cli/cmd/root.go: Add deleteByID()ov- cli/cmd/root.go: Add deleteByID() — extracts the- cli/cmd/root.go: Add deleteByID() — extracts the- **cli� - **cli/�� output) use- **cli/cmd/us- cli/cmd/root.go: Add deleteByID() — extraclp- **cli/cmi/cmd/override.go` — constants + use helpers
  • cli/pkg/client/client.g- cli/pkg/client/client.g- cli/pkg/client/client.g- cli/pkg/client/client.g- cli/pkg/client/client.g- cli/pkg/client/client.g- `cli/pkg/client/K** |
    | New code coverage | — | 90.8% |
    | New code duplication | — | 0.0% |
    | Overall duplication | 3.2% | 0.8% |

Testing

  • All unit, integration, and e2e tests pass
  • go vet and go build clean
  • No behavioral changes — pure refactoring

Fix 11 SonarQube maintainability issues by deduplicating string literals:
- client.go: API path patterns (4 constants)
- bulk.go: flag description (1 constant)
- override.go: confirmation messages (3 constants)
- definition.go: file handling strings (3 constants)
Copilot AI review requested due to automatic review settings March 29, 2026 07:50
Copy link
Copy Markdown

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

Refactors the stackctl CLI to reduce duplicated string literals (SonarQube maintainability) by introducing shared constants for repeated API paths, error messages, and flag descriptions.

Changes:

  • Extract repeated API endpoint path strings into constants in the HTTP client.
  • Extract repeated confirmation/error message strings and flag descriptions into constants in commands.
  • Standardize repeated flag description text (e.g., bulk --ids) via a shared constant.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.

File Description
cli/pkg/client/client.go Introduces constants for frequently used API path templates and reuses them in definition/override-related methods.
cli/cmd/override.go Extracts repeated confirmation error/message strings and the --yes flag description into constants.
cli/cmd/definition.go Extracts repeated --from-file flag name and repeated file/path error strings into constants.
cli/cmd/bulk.go Extracts the repeated --ids flag description into a single constant.

Fix staticcheck SA1006: fmt.Errorf with dynamic format string and no
args should use errors.New; format strings with args stay inline.
Copy link
Copy Markdown

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 4 out of 4 changed files in this pull request and generated 2 comments.

Comment thread cli/cmd/definition.go Outdated

const (
flagFromFile = "from-file"
errPathTraversal = "file path must not contain '..' segments"
Copy link

Copilot AI Mar 29, 2026

Choose a reason for hiding this comment

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

errPathTraversal is a string constant, but the err* name reads like an error sentinel. Consider either renaming it to msgPathTraversal/errMsgPathTraversal, or changing it to a package-level var error (e.g., var ErrPathTraversal = errors.New(...)) so the name matches the type and callers can use errors.Is.

Suggested change
errPathTraversal = "file path must not contain '..' segments"
msgPathTraversal = "file path must not contain '..' segments"

Copilot uses AI. Check for mistakes.
Comment thread cli/cmd/override.go Outdated
Comment on lines +20 to +22
errReadConfirmation = "reading confirmation: %w"
msgAborted = "Aborted."
flagDescSkipConfirm = "Skip confirmation prompt"
Copy link

Copilot AI Mar 29, 2026

Choose a reason for hiding this comment

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

errReadConfirmation is a format string (it contains %w), not an error value. Renaming it to something like errReadConfirmationFmt (or defining a sentinel error when appropriate) would make its intent/type clearer and prevent accidental misuse (e.g., passing it where an error is expected).

Suggested change
errReadConfirmation = "reading confirmation: %w"
msgAborted = "Aborted."
flagDescSkipConfirm = "Skip confirmation prompt"
errReadConfirmationFmt = "reading confirmation: %w"
msgAborted = "Aborted."
flagDescSkipConfirm = "Skip confirmation prompt"

Copilot uses AI. Check for mistakes.
- errPathTraversal → msgPathTraversal (not an error format string)
- errReadConfirmation → errReadConfirmationFmt (contains format verb)
Copy link
Copy Markdown

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 4 out of 4 changed files in this pull request and generated no new comments.

- Add confirmAction() to root.go for the confirmation prompt pattern
- Add deleteByID() to root.go for single-ID delete commands
- Add deleteChartOverride() to override.go for two-ID override deletes
- Add readFileErr() to definition.go for file read error wrapping
- Replace 8 duplicated confirmation blocks across 4 files
- Replace 3 duplicated delete handlers (definition, stack, override×2)
- Removes errReadConfirmationFmt constant (now in confirmAction)

SonarQube: 0 open issues, quality gate OK, duplication 0.8%
Copy link
Copy Markdown

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 6 out of 6 changed files in this pull request and generated 1 comment.

Comment thread cli/cmd/root.go
Comment on lines +183 to +187
// confirmAction prompts the user for confirmation unless --yes is set.
// Returns true if the action should proceed, false if aborted.
func confirmAction(cmd *cobra.Command, message string) (bool, error) {
yes, _ := cmd.Flags().GetBool("yes")
if yes {
Copy link

Copilot AI Mar 29, 2026

Choose a reason for hiding this comment

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

PR title/description says this change is only about extracting duplicated string literals into constants across 4 files, but this hunk also introduces new shared helper functions (confirmAction/deleteByID) and the PR touches additional files. Please update the PR description (or split the refactor) so reviewers understand the behavioral/structural changes being made.

Copilot uses AI. Check for mistakes.
@omattsson omattsson changed the title refactor: extract duplicated string literals into constants refactor: reduce code duplication flagged by SonarQube Mar 29, 2026
@omattsson omattsson merged commit 888faae into main Mar 29, 2026
10 checks passed
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