Skip to content

Refactor guard policy integrity normalization to remove duplicated validation paths#6915

Merged
lpcox merged 2 commits into
mainfrom
copilot/duplicate-code-pattern-fix-again
Jun 2, 2026
Merged

Refactor guard policy integrity normalization to remove duplicated validation paths#6915
lpcox merged 2 commits into
mainfrom
copilot/duplicate-code-pattern-fix-again

Conversation

Copilot AI commented Jun 2, 2026

Copy link
Copy Markdown
Contributor

NormalizeGuardPolicy had three near-identical integrity validation blocks (min-integrity, disapproval-integrity, endorser-min-integrity) with repeated trim/lowercase + allowed-values checks. This change centralizes that logic to keep field handling and error formatting consistent as integrity fields evolve.

  • Validation refactor

    • Added validateAndNormalizeIntegrityField(fieldPath, raw, optional) in internal/config/guard_policy_validation.go.
    • Replaced inline validation for:
      • required allow-only.min-integrity
      • optional allow-only.disapproval-integrity
      • optional allow-only.endorser-min-integrity
  • Behavior preserved, duplication removed

    • Required field still rejects empty/invalid values.
    • Optional fields still allow empty values and reject invalid non-empty values.
    • Error message shape remains centralized via the same allowed-values list.
  • Focused coverage for shared helper

    • Added TestValidateAndNormalizeIntegrityField in internal/config/guard_policy_test.go to cover:
      • trim + lowercase normalization
      • optional-empty acceptance
      • required-empty rejection
      • invalid optional value rejection
func validateAndNormalizeIntegrityField(fieldPath, raw string, optional bool) (string, error) {
	v := strings.ToLower(strings.TrimSpace(raw))
	if v == "" && optional {
		return "", nil
	}
	if _, ok := validMinIntegrityValues[v]; !ok {
		return "", fmt.Errorf("%s must be one of: %s", fieldPath, strings.Join(allowedGuardPolicyIntegrityLevels, ", "))
	}
	return v, nil
}

Copilot AI changed the title [WIP] Refactor duplicate code for optional integrity field validation Refactor guard policy integrity normalization to remove duplicated validation paths Jun 2, 2026
Copilot finished work on behalf of lpcox June 2, 2026 23:15
Copilot AI requested a review from lpcox June 2, 2026 23:15
@lpcox lpcox marked this pull request as ready for review June 2, 2026 23:21
Copilot AI review requested due to automatic review settings June 2, 2026 23:21

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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 NormalizeGuardPolicy integrity field handling by extracting duplicated trim/lowercase + allowed-values validation logic into a single helper, keeping validation behavior and error formatting consistent as the guard policy schema evolves.

Changes:

  • Added validateAndNormalizeIntegrityField(fieldPath, raw, optional) to centralize integrity normalization/validation.
  • Replaced inline validation for allow-only.min-integrity, allow-only.disapproval-integrity, and allow-only.endorser-min-integrity with the shared helper.
  • Added unit tests to cover normalization, required/optional empty handling, and invalid-value rejection.
Show a summary per file
File Description
internal/config/guard_policy_validation.go Consolidates integrity field normalization/validation into a helper and wires it into NormalizeGuardPolicy.
internal/config/guard_policy_test.go Adds targeted tests for the new shared integrity validation helper.

Copilot's findings

Tip

Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

  • Files reviewed: 2/2 changed files
  • Comments generated: 0

@lpcox lpcox merged commit 3870516 into main Jun 2, 2026
29 checks passed
@lpcox lpcox deleted the copilot/duplicate-code-pattern-fix-again branch June 2, 2026 23:23
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.

[duplicate-code] Duplicate Code Pattern: Repeated Optional Integrity Field Validation in guard_policy_validation.go

3 participants