Also fix repository-free message.send validation (#2046) and add a single-binary development smoke path (#1977). Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
110 lines
2.8 KiB
Go
110 lines
2.8 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] = resultFields(s)
|
|
}
|
|
}
|
|
|
|
// resultFields are the result fields a step's type declares, or nil when it
|
|
// declares none.
|
|
func resultFields(s Step) map[string]bool {
|
|
if s.Type != typeAgent {
|
|
return nil
|
|
}
|
|
var settings agentSettings
|
|
if decodeSettings(s.Settings, &settings) != nil {
|
|
return nil
|
|
}
|
|
return settings.resultFields()
|
|
}
|
|
|
|
// controls validates the common step settings: 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)
|
|
}
|
|
}
|