policy

package
v1.26.1 Latest Latest
Warning

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

Go to latest
Published: Jul 13, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package policy loads .go-code.yaml from a repo root and evaluates simple team rules against a review.DeltaResult.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Filters

type Filters struct {
	Include []string `yaml:"include"`
	Exclude []string `yaml:"exclude"`
}

Filters controls which paths the policy considers.

type Finding

type Finding struct {
	Path     string
	Line     int
	Severity string
	Rule     string
	Message  string
}

Finding is one policy violation.

type ForbiddenImport

type ForbiddenImport struct {
	Pattern string `yaml:"pattern"`
	Reason  string `yaml:"reason"`
}

ForbiddenImport blocks a specific import path (substring match).

type Policy

type Policy struct {
	Version     int     `yaml:"version"`
	Severity    string  `yaml:"severity"` // warning | error
	PathFilters Filters `yaml:"path_filters"`
	Rules       RuleSet `yaml:"rules"`
}

Policy is the parsed team policy.

func Load

func Load(root string) (*Policy, error)

Load reads .go-code.yaml from root. Returns (nil, nil) when the file is absent — policy is opt-in.

func LoadWithDefaults

func LoadWithDefaults(repoRoot, defaultsPath string) (*Policy, error)

LoadWithDefaults reads a server-wide defaults file (may be empty path), then overlays the repo-local .go-code.yaml on top. Repo rules take precedence on scalar fields; list fields (forbidden_imports, required_when_touching, path_filters) are concatenated. Returns nil only when both files are missing.

func Merge

func Merge(base, local *Policy) *Policy

Merge overlays local on top of base. Nil inputs are handled: Merge(nil, x) == x, Merge(x, nil) == x, Merge(nil, nil) == nil. Scalars: local wins when non-zero. Lists: concatenated (base first).

func (*Policy) Apply

func (p *Policy) Apply(r *review.DeltaResult, readFile func(path string) string) []Finding

Apply runs rules against r. The readFile closure lets callers provide file bodies either from disk (local repo) or from a git blob (remote cloned repo).

type RequiredWhen

type RequiredWhen struct {
	Path          string `yaml:"path"`
	MustAlsoTouch string `yaml:"must_also_touch"`
	Reason        string `yaml:"reason"`
}

RequiredWhen enforces that touching path X also touches path Y.

type RuleSet

type RuleSet struct {
	ForbiddenImports  []ForbiddenImport `yaml:"forbidden_imports"`
	RequiredWhen      []RequiredWhen    `yaml:"required_when_touching"`
	MaxFuncLines      int               `yaml:"max_func_lines"`
	NoMagicHTTPStatus bool              `yaml:"no_magic_http_status"`
}

RuleSet is the set of supported rules. Keep this small and explicit — do not build a generic rule DSL.

Jump to

Keyboard shortcuts

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