Cancelling a run moved it and the runs it called to cancelled and wrote nothing to either trail, so a stopped run could not say when it stopped or who asked. The stop now returns the runs it stopped and records one cancel entry on each, in the same transaction, naming the principal that asked. The entry is a step named cancel with the caller's identity, so the run detail shows it with the rest of the trail.
166 lines
4.7 KiB
Go
166 lines
4.7 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)
|
|
}
|
|
c.covers(d.Steps)
|
|
return c.problems
|
|
}
|
|
|
|
// covers checks that an approval covers steps that come after it, so they can
|
|
// be judged when it asks, and that judgement can judge them: connector.action
|
|
// steps, each covered by one approval only and none of them reading another
|
|
// step the same approval covers.
|
|
func (c *checker) covers(steps []Step) {
|
|
covered := make(map[string]int, len(steps))
|
|
for i, s := range steps {
|
|
if s.Type != typeApproval {
|
|
continue
|
|
}
|
|
var settings approvalSettings
|
|
if decodeSettings(s.Settings, &settings) != nil {
|
|
continue
|
|
}
|
|
together := make(map[string]bool, len(settings.Covers))
|
|
for _, id := range settings.Covers {
|
|
together[id] = true
|
|
}
|
|
for j, id := range settings.Covers {
|
|
at := fmt.Sprintf("steps[%d].settings.covers[%d]", i, j)
|
|
target, k := stepAfter(steps, i, id)
|
|
switch {
|
|
case k < 0:
|
|
c.add(at, "no step after this one has id %q", id)
|
|
continue
|
|
case target.Type != typeAction:
|
|
c.add(at, "step %q is %s, and an approval covers connector.action steps only", id, target.Type)
|
|
continue
|
|
}
|
|
if other, twice := covered[id]; twice {
|
|
c.add(at, "step %q is already covered by the approval at steps[%d]", id, other)
|
|
}
|
|
covered[id] = i
|
|
// The approval judges every step it covers when it asks, so one that
|
|
// reads another of them has no value to judge yet.
|
|
for _, ref := range stepRefs(target.Settings) {
|
|
if together[ref] {
|
|
c.add(fmt.Sprintf("steps[%d].settings", k), "step %q reads step %q, which the same approval covers and which hasn't run yet when it asks", id, ref)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// stepAfter returns the step after the approval at from that has this id, and
|
|
// its index in steps, or -1.
|
|
func stepAfter(steps []Step, from int, id string) (Step, int) {
|
|
for i := from + 1; i < len(steps); i++ {
|
|
if id != "" && steps[i].ID == id {
|
|
return steps[i], i
|
|
}
|
|
}
|
|
return Step{}, -1
|
|
}
|
|
|
|
// 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)
|
|
}
|
|
}
|