Skip to content

Refactor guard initialization to centralize noop fallback construction#5885

Merged
lpcox merged 3 commits into
mainfrom
copilot/duplicate-code-fix-noop-guard
May 17, 2026
Merged

Refactor guard initialization to centralize noop fallback construction#5885
lpcox merged 3 commits into
mainfrom
copilot/duplicate-code-fix-noop-guard

Conversation

Copilot AI commented May 17, 2026

Copy link
Copy Markdown
Contributor

internal/server/guard_init.go repeated guard.NewNoopGuard() in multiple fallback branches across registerGuard and requireGuardPolicyIfGuardEnabled. This duplicated the same defaulting behavior in five locations and increased maintenance risk if noop guard construction changes.

  • What changed

    • Added a single local helper in guard_init.go:
      • newNoopGuard() guard.Guard
    • Replaced all repeated noop fallback constructions in the file with this helper.
    • Kept existing branch behavior and logging messages unchanged; this is a structural deduplication of fallback construction only.
  • Affected fallback paths

    • Guard creation from config failure
    • Registered guard lookup failure
    • No guard configured path
    • Policy-required downgrade path in requireGuardPolicyIfGuardEnabled
    • createGuardFromConfig noop-type/default branch
  • Example of the refactor

    func newNoopGuard() guard.Guard {
        return guard.NewNoopGuard()
    }
    
    // before: g = guard.NewNoopGuard()
    g = newNoopGuard()
    
    // before: return guard.NewNoopGuard(), nil
    return newNoopGuard(), nil

Copilot AI changed the title [WIP] Refactor duplicate NoopGuard fallback in guard_init.go Refactor guard initialization to centralize noop fallback construction May 17, 2026
Copilot finished work on behalf of lpcox May 17, 2026 16:11
Copilot AI requested a review from lpcox May 17, 2026 16:11
@lpcox lpcox marked this pull request as ready for review May 17, 2026 17:12
Copilot AI review requested due to automatic review settings May 17, 2026 17:12
@lpcox

lpcox commented May 17, 2026

Copy link
Copy Markdown
Collaborator

@copilot merge main

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 internal/server/guard_init.go to centralize construction of the noop guard used across multiple fallback branches, reducing duplication while preserving existing behavior and logging.

Changes:

  • Added a local helper newNoopGuard() guard.Guard to wrap guard.NewNoopGuard().
  • Replaced repeated noop-guard fallback constructions in registerGuard, requireGuardPolicyIfGuardEnabled, and createGuardFromConfig with the helper.
Show a summary per file
File Description
internal/server/guard_init.go Deduplicates noop guard fallback construction via a single helper without changing control flow or log messages.

Copilot's findings

Tip

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

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

…e-fix-noop-guard

# Conflicts:
#	internal/server/guard_init.go

Copilot AI commented May 17, 2026

Copy link
Copy Markdown
Contributor Author

@copilot merge main

Done. Merged main into this branch (commit 2dccd18). The only conflict was in guard_init.go — resolved by keeping the newNoopGuard() helper from this PR alongside the legacyPolicySource constant added in main. All checks pass.

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 NewNoopGuard() Fallback in guard_init.go

3 participants