policy

package
v0.151.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 19, 2026 License: MIT Imports: 6 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package policy is the gk guard policy engine.

Rules implement the Rule interface and register themselves via the package Registry. The engine evaluates every registered rule in parallel against an Input (the repo + context being checked) and aggregates Violations.

Design notes:

  • Rules are stateless. Per-call state lives on Input / Violation.
  • Severity is a stable integer — lower numeric value means more severe so Violations sort naturally (Error < Warn < Info).
  • `gk guard check` (GD-08) is the CLI driver that builds Input, runs the Registry, and exits with a code derived from the worst Severity.

Index

Constants

This section is empty.

Variables

View Source
var Default = NewRegistry()

Default is a process-wide Registry. Built-in rules (max_commit_size, required_trailers, …) register themselves here via init(). Callers that need isolation (tests, subcommands with limited rule sets) should build a fresh Registry with NewRegistry and Register rules explicitly.

Functions

This section is empty.

Types

type Input

type Input struct {
	// Runner is a git runner scoped to the target repo. Rules that need
	// git data (commit lists, diff ranges, log output) use this.
	Runner git.Runner
	// WorkDir is the repo root; rules that spawn non-git subprocesses
	// (scanners, linters) pass this as cwd.
	WorkDir string
	// Staged restricts evaluation to staged changes only. Pre-commit hook
	// sets this true; pre-push sets false to scan the push range.
	Staged bool
	// PushRange, when non-empty, is the `<base>..<head>` ref range pushed
	// (pre-push hook scenario). Rules that care about history scoping read
	// this; otherwise they fall back to default behavior.
	PushRange string
}

Input is the context passed to every Rule's Evaluate method. Rules pick only the fields they need.

type Registry

type Registry struct {
	// contains filtered or unexported fields
}

Registry holds rule instances. A global Default registry exists for convenience; callers that need isolated rule sets (tests, scoped evaluations) can construct a fresh Registry via NewRegistry.

func NewRegistry

func NewRegistry() *Registry

NewRegistry returns an empty Registry.

func (*Registry) Evaluate

func (r *Registry) Evaluate(ctx context.Context, in Input) ([]Violation, []error)

Evaluate runs every registered rule in parallel and returns all Violations merged. Rule errors are collected separately — one failing rule does not abort the others. The Violations slice is sorted by (Severity asc, RuleID asc, File asc, Line asc) so the worst items come first and output is deterministic.

func (*Registry) Get

func (r *Registry) Get(name string) (Rule, bool)

Get returns the Rule registered under name, or (nil, false) if absent.

func (*Registry) Names

func (r *Registry) Names() []string

Names returns every registered rule name sorted alphabetically.

func (*Registry) Register

func (r *Registry) Register(rule Rule) error

Register adds a Rule. Re-registering a Rule with an existing Name returns an error so callers catch accidental duplication. Rules written in tests can bypass this by calling Replace.

func (*Registry) Replace

func (r *Registry) Replace(rule Rule)

Replace installs a Rule under its Name, overwriting any existing entry. Useful for tests that stub out a built-in rule.

type Rule

type Rule interface {
	// Name returns a stable identifier used as RuleID in Violations and
	// surfaced in config (.gk.yaml `policies.<name>:`).
	Name() string
	// Evaluate runs the rule against Input and returns zero or more
	// Violations. An error is returned only for infrastructure failures
	// (git command failed, filesystem error); policy breaches are
	// expressed as Violations, not errors.
	Evaluate(ctx context.Context, in Input) ([]Violation, error)
}

Rule is the contract every policy implementation satisfies.

type Severity

type Severity int

Severity ranks a Violation. Lower values mean more severe so sorting a slice of Violations puts the most important items first.

const (
	SeverityError Severity = iota // 0 — blocks the check
	SeverityWarn                  // 1 — surfaced, does not block
	SeverityInfo                  // 2 — informational only
)

func (Severity) String

func (s Severity) String() string

String returns the stable lowercase name used in JSON output and logs.

type Violation

type Violation struct {
	RuleID   string   // stable identifier matching Rule.Name()
	Severity Severity // rule-assigned severity
	File     string   // empty when the violation is not file-local
	Line     int      // 0 when N/A
	Message  string   // human-readable one-liner
	Hint     string   // optional fix suggestion
}

Violation describes a single policy breach. Severity is assigned by the rule; the engine does not override it.

Directories

Path Synopsis
Package rules holds the built-in gk guard policy rule implementations.
Package rules holds the built-in gk guard policy rule implementations.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL