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 ¶
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 (*Registry) Evaluate ¶
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.
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.
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.