plan

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Nov 29, 2026 License: BSD-3-Clause Imports: 16 Imported by: ? 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 plan decides which workflows an event triggers and turns a parsed workflow into an ordered plan of job instances: filter matching, matrix expansion, dependency ordering and the job-level conditions. It runs nothing and holds no secret: secrets are never evaluated here, and any value the caller declares sensitive is masked in everything the plan carries.

Index

Constants

View Source
const (
	MaxPattern = 1024
	MaxSubject = 4096
)

Bounds on glob matching. A pattern or a subject over them is an error, not a slow match.

View Source
const (
	EventBase    = 1 << 20
	EventPerByte = 256
	// 2^28 units at 2.9 ns (the star loop, the slowest shape measured: a call is
	// 12 ns for 6 units) is about 0.8 s of one core on the reference machine,
	// whatever the input; 1.5 to 3.4 s measured at a load of 60, a hosted runner
	// is 2 to 4 times slower (TestRound13CeilingTime).
	EventCeiling = 1 << 28
)

The meter of an EVENT. It is created once for the event (NewEventMeter) and shared by every workflow and every filter that event is matched against, so what a push costs does not grow with the number of workflows it is read by.

It holds Base + PerByte units for each byte of the event and of the workflows, up to the Ceiling; a unit is one byte examined by a matcher, at most a few nanoseconds. Spending more is an error (errPlanBudget): the caller runs the workflow or fails, never skips it.

View Source
const (
	// WorkBase and WorkPerByte size the default work budget of a plan.
	WorkBase    = 1 << 22
	WorkPerByte = 1 << 10
)
View Source
const MaxEventBytes = 16 << 20

MaxEventBytes bounds the event payload a filter reads: the bytes of the changed paths, refs and names together. A larger one is an error, so the work a filter does is linear in a quantity the caller has already capped.

View Source
const (

	// MaxFiles bounds the changed-file list a path filter reads (GitHub's own
	// push payload stops at 3000).
	MaxFiles = 3000
)
View Source
const MaxInputValue = 64 << 10

MaxInputValue bounds one dispatch input value.

Variables

View Source
var ErrBudget = errPlanBudget

ErrBudget is the error Build and Expand return, as is, when a workflow costs more than Limits.MaxWork to plan.

Functions

func ResolveInputs

func ResolveInputs(w *workflow.Workflow, given map[string]string) (map[string]string, error)

ResolveInputs checks the values given to a workflow_dispatch against the declared inputs and returns them with the defaults filled in. Errors name the input, never its value.

Types

type Combo

type Combo struct {
	Keys   []string
	Values map[string]any
}

Combo is one matrix combination: Keys in a deterministic order (the axes in declaration order, then keys added by include entries in order).

type Event

type Event struct {
	Name    string   // push, pull_request, workflow_dispatch, schedule, ...
	Ref     string   // push: the pushed ref (refs/heads/x or refs/tags/x); others: the head or run ref
	BaseRef string   // pull_request*: the target branch
	Action  string   // activity type: opened, synchronize, published, ...
	Files   []string // changed paths; nil means unknown: path filters are then not evaluated
	Cron    string   // schedule: the expression that fired
}

Event is what happened, reduced to what trigger filters read.

type EventMeter

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

EventMeter is the work meter and the deadline of one event.

func NewEventMeter

func NewEventMeter(ev Event, plans ...*Plan) *EventMeter

NewEventMeter sizes the meter of ev for the plans it will be matched against: EventBase + EventPerByte x (bytes of the event + bytes of the workflows), at most EventCeiling units. Its deadline is the shortest Limits.Timeout of the plans (default 30 s) from now, within the context of the first plan. Close it when the event is done.

func (*EventMeter) Close

func (e *EventMeter) Close()

Close releases the deadline.

func (*EventMeter) Limit

func (e *EventMeter) Limit() int64

Limit is the units the event may cost.

func (*EventMeter) Spent

func (e *EventMeter) Spent() int64

Spent is the units the event has cost so far.

type Job

type Job struct {
	ID              string         `json:"id"`
	Key             string         `json:"key"` // unique in the plan: the id, or id/index with a matrix
	Name            string         `json:"name"`
	Needs           []string       `json:"needs,omitempty"` // job ids
	Level           int            `json:"level"`           // 0: nothing to wait for
	RunsOn          []string       `json:"runs_on,omitempty"`
	RunsOnGroup     string         `json:"runs_on_group,omitempty"`
	If              string         `json:"if,omitempty"` // raw, evaluated by ShouldRun
	Matrix          map[string]any `json:"matrix,omitempty"`
	MatrixKeys      []string       `json:"matrix_keys,omitempty"`
	Index           int            `json:"index"`
	Total           int            `json:"total"`
	FailFast        bool           `json:"fail_fast"`
	MaxParallel     int            `json:"max_parallel,omitempty"` // 0: no limit
	ContinueOnError bool           `json:"continue_on_error,omitempty"`
	TimeoutMinutes  int            `json:"timeout_minutes"`
	// Deferred is set when the matrix needs outputs of other jobs that are
	// not known yet: call Expand again once they are, with needs in Contexts.
	Deferred bool          `json:"deferred,omitempty"`
	Source   *workflow.Job `json:"-"`
	// contains filtered or unexported fields
}

Job is one job instance: a job of the workflow, or one combination of its matrix. Expression-valued fields are resolved; the steps, env and the rest stay with Source for the runner, which has the secrets.

func (*Job) ShouldRun

func (j *Job) ShouldRun(results map[string]Result, contexts map[string]any) (ok bool, err error)

ShouldRun evaluates the job's if: against the results of the jobs it needs (by job id; a need without a result counts as skipped). Without an if:, the job runs when every need succeeded; with one, success() is implied unless it calls a status function. A skipped need is neither success nor failure, so only always() and failure()-free-of-skip conditions can run past it.

The condition is parsed and evaluated under the work budget of the plan that made the job, together with everything else done about the workflow: a Job that no plan made is refused, it has no budget to draw from. The contexts given are added to the plan's own.

type Limits

type Limits struct {
	MaxMatrix int // combinations per job (GitHub: 256)
	MaxJobs   int // job instances per plan
	// MaxWork is the work of the WHOLE plan: template parses, evaluations,
	// strings built and values copied, however many jobs, labels and
	// combinations multiply them.
	//
	// Zero (the default) sizes it FROM THE WORKFLOW: WorkBase + WorkPerByte per
	// byte of its text (Workflow.Size). A workflow the planner can read is then
	// always plannable, and an attacker buys work only in proportion to what the
	// workflow already costs the author to write. Trigger filters are not drawn
	// from it: each filter call has a meter sized from its own input.
	MaxWork int
	// MaxRetained bounds the bytes an accepted plan keeps (default 64 MiB).
	MaxRetained int
	// Timeout is the wall-clock deadline of one call into the plan (default
	// 30 s), a second line of defence behind the work budget.
	Timeout time.Duration
	Expr    expr.Limits
}

Limits bound what a plan may grow to.

type Options

type Options struct {
	// Contexts feeds the expressions the planner evaluates (matrix, job name,
	// runs-on, timeouts): github, inputs, vars, needs... The secrets context is
	// never read, whatever is passed.
	Contexts map[string]any
	// Mask lists values that must not appear in a plan: every string the
	// planner evaluates has them replaced by ***, as the Actions log does. The
	// spellings an error text or an encoder gives a value (Go and JSON quoting,
	// with or without HTML escaping, invalid bytes as U+FFFD) are hidden too.
	// One limit by design: timeout-minutes is an integer, so a masked value that
	// reads as an integer is the timeout, and is not replaced there.
	Mask   []string
	Limits Limits
	// Context cancels every call into the plan (default Background); each call
	// also carries Limits.Timeout.
	Context context.Context
}

Options are the inputs of a plan besides the workflow.

type Plan

type Plan struct {
	Workflow string `json:"workflow"`
	Jobs     []*Job `json:"jobs"` // dependency order, ties by declaration
	// contains filtered or unexported fields
}

Plan is the ordered job instances of a workflow, and the owner of the ONE work budget of everything done about that workflow: the trigger filters (Matches, Glob, Filter), the expansion of its jobs (Build, Expand) and the conditions of its job instances (Job.ShouldRun). Limits.MaxWork bounds them together. New is the only constructor of that budget: no exported function creates one from nothing, and a Job that no plan made cannot be evaluated.

func Build

func Build(w *workflow.Workflow, opt Options) (*Plan, error)

Build plans every job of the workflow: New, then Resolve.

func New

func New(w *workflow.Workflow, opt Options) *Plan

New is a plan with no job yet, holding the budget of the workflow: call Matches, Resolve and Expand on it.

func (*Plan) Expand

func (p *Plan) Expand(id string, contexts map[string]any) (jobs []*Job, err error)

Expand plans one job: its matrix combinations, with every expression-valued field resolved. It is the call to repeat for a Deferred job once the outputs it waited for are in contexts["needs"]. The contexts given are added to the plan's own (the secrets one is never read), and the work is the plan's.

func (*Plan) FilterIn

func (p *Plan) FilterIn(m *EventMeter, patterns []string, s string) (ok bool, err error)

Filter applies an ordered pattern list to s: a pattern starting with '!' removes a previous match, a plain one adds it; the last pattern that matches decides. A list of only negations is an error (nothing could ever match), as on GitHub.

func (*Plan) GlobIn

func (p *Plan) GlobIn(m *EventMeter, pattern, s string) (ok bool, err error)

Glob reports whether s matches the pattern, with the GitHub filter syntax: * any run of characters except '/', ** any run, ? zero or one of the preceding character, + one or more of it, [a-z] a class, \ an escape. The work is charged to the meter of the event (NewEventMeter): there is no spelling of a match that carries a meter of its own.

func (*Plan) MatchesIn

func (p *Plan) MatchesIn(m *EventMeter, ev Event) (ok bool, err error)

MatchesIn reports whether the event triggers the workflow of the plan, under the meter of the event (NewEventMeter): the filters of every workflow matched through the same EventMeter draw from the same units and stop at the same deadline, so what a push costs does not grow with the number of workflows. There is no spelling of a match without a meter: GlobIn and FilterIn take one too. Exhausting it is an error, which a caller reads as run-or-fail, never as no match: the result is false whenever the error is not nil.

func (*Plan) Resolve

func (p *Plan) Resolve() (err error)

Resolve expands every job of the workflow, in dependency order, under the plan's budget.

func (*Plan) String

func (p *Plan) String() string

String is a deterministic one-line-per-job dump. It carries only what the plan carries, so it holds no secret and no masked value.

type Result

type Result string

Result is the outcome of a job.

const (
	Success   Result = "success"
	Failure   Result = "failure"
	Cancelled Result = "cancelled"
	Skipped   Result = "skipped"
)

Jump to

Keyboard shortcuts

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