A step is id/type/settings with when and on_failure; the five kinds become
the types agent.run (inline mission), connector.action, approval.wait,
run.notify and workflow.sleep. Documents declare inputs, repository,
instructions, identity and profile; references are {{ task|inputs|steps }}
checked at save. Any number of approval steps, each asking only about what
was held since; actions still held at the end are put to people before the
run finishes. Triggers are refused for a workflow they can't start, and the
workflows API lists, reads and saves documents beside a step-type catalog.
Copied from internal/domain/workflow: definition.go, step_refs.go,
step_flow.go's checks, control_steps.go's when parser, vocabulary.go and
task/interface.go. stableid moves to internal/platform.
98 lines
2.5 KiB
Go
98 lines
2.5 KiB
Go
package runs
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
)
|
|
|
|
// checker collects the problems found in a document.
|
|
type checker struct {
|
|
problems []Problem
|
|
}
|
|
|
|
func (c *checker) add(path, format string, args ...any) {
|
|
c.problems = append(c.problems, Problem{Path: path, Message: fmt.Sprintf(format, args...)})
|
|
}
|
|
|
|
func (d Document) validate() []Problem {
|
|
c := &checker{problems: d.problems()}
|
|
if len(d.Steps) == 0 {
|
|
c.add("steps", "a workflow needs steps")
|
|
}
|
|
earlier := newRefScope(d.Inputs)
|
|
for i, s := range d.Steps {
|
|
c.step(fmt.Sprintf("steps[%d]", i), s, d.mode(), earlier)
|
|
}
|
|
return c.problems
|
|
}
|
|
|
|
// step validates one step against the vocabulary and the steps before it, and
|
|
// adds the id it declares to earlier.
|
|
func (c *checker) step(at string, s Step, mode RepositoryMode, earlier *refScope) {
|
|
if err := checkStepID(s.ID, earlier); err != nil {
|
|
c.add(at+".id", "%s", err)
|
|
}
|
|
c.controls(at, s, earlier)
|
|
switch {
|
|
case s.Switch != nil:
|
|
c.add(at+".switch", "switch steps are not supported yet")
|
|
case s.Parallel != nil:
|
|
c.add(at+".parallel", "parallel steps are not supported yet")
|
|
default:
|
|
c.typed(at, s, mode, earlier)
|
|
}
|
|
if s.ID != "" {
|
|
earlier.steps[s.ID] = nil
|
|
}
|
|
}
|
|
|
|
// controls validates the settings every step carries: when, on_failure and
|
|
// retry.
|
|
func (c *checker) controls(at string, s Step, earlier *refScope) {
|
|
if s.OnFailure != "" && s.OnFailure != onFailurePark && s.OnFailure != onFailureContinue {
|
|
c.add(at+".on_failure", "park or continue, not %q", s.OnFailure)
|
|
}
|
|
if s.Retry != nil {
|
|
c.add(at+".retry", "retrying a step is not supported yet")
|
|
}
|
|
if s.When == "" {
|
|
return
|
|
}
|
|
cond, err := parseCondition(s.When)
|
|
if err != nil {
|
|
c.add(at+".when", "%s", err)
|
|
return
|
|
}
|
|
if err := checkReference(cond.path, earlier); err != nil {
|
|
c.add(at+".when", "%s", err)
|
|
}
|
|
}
|
|
|
|
func (c *checker) typed(at string, s Step, mode RepositoryMode, earlier *refScope) {
|
|
if s.Type == "" {
|
|
c.add(at+".type", "a step needs a type")
|
|
return
|
|
}
|
|
t, ok := stepTypes[s.Type]
|
|
if !ok {
|
|
c.add(at+".type", "unknown type %q", s.Type)
|
|
return
|
|
}
|
|
if mode != RepositoryRequired && t.NeedsRepository {
|
|
c.add(at+".type", "%q needs a repository, but the workflow's repository is %s", s.Type, mode)
|
|
}
|
|
for _, err := range checkReferences(s.Settings, earlier) {
|
|
c.add(at+".settings", "%s", err)
|
|
}
|
|
err := t.check(s.Settings)
|
|
var joined interface{ Unwrap() []error }
|
|
switch {
|
|
case errors.As(err, &joined):
|
|
for _, e := range joined.Unwrap() {
|
|
c.add(at+".settings", "%s", e)
|
|
}
|
|
case err != nil:
|
|
c.add(at+".settings", "%s", err)
|
|
}
|
|
}
|