package taskplan import ( "errors" "reflect" "strings" "unicode/utf8" ) var ErrInvalid = errors.New("plan_invalid") var ErrLocked = errors.New("plan_locked_step") func Validate(p Plan) error { if p.ProjectID <= 0 || p.Revision < 0 || len(p.Steps) == 0 || len(p.Steps) > MaxSteps { return ErrInvalid } if !Publication(p.Kind) && p.Kind != Recommendation && p.Kind != Investigation { return ErrInvalid } seen := map[string]int{} review, checks, person, publish, lastCandidate := -1, -1, -1, -1, -1 for i, s := range p.Steps { if s.Key == "" || strings.TrimSpace(s.Key) != s.Key || len(s.Key) > 128 || !utf8.ValidString(s.Key) || len(s.Instructions) > 16384 || !utf8.ValidString(s.Instructions) { return ErrInvalid } if _, ok := seen[s.Key]; ok { return ErrInvalid } seen[s.Key] = i for _, dep := range s.DependsOn { if _, ok := seen[dep]; !ok || dep == s.Key { return ErrInvalid } } switch s.Action { case "review": if s.Key != "review" || s.Specialist != "review" { return ErrInvalid } review = i case "checks": if s.Key != "verify" || s.Model != "" || s.ConnectionID != "" { return ErrInvalid } checks = i case "person": if s.Key != "merge" || s.Model != "" || s.ConnectionID != "" { return ErrInvalid } person = i case "publish": if s.Key != "publish" || s.Model != "" || s.ConnectionID != "" { return ErrInvalid } publish = i case "implement", "copy", "design": lastCandidate = i case "reproduce", "scope", "investigate", "recommend": default: return ErrInvalid } if !Publication(p.Kind) && s.Action != "person" && s.Action != "scope" && s.Action != "investigate" && s.Action != "recommend" { return ErrInvalid } } if person != len(p.Steps)-1 { return ErrInvalid } if Publication(p.Kind) { if review < 0 || checks < 0 || review <= lastCandidate || checks <= lastCandidate { return ErrInvalid } if publish >= 0 && (publish < review || publish < checks) { return ErrInvalid } if publish < 0 && len(p.Steps)+1 > MaxSteps { return ErrInvalid } } return nil } func ValidateEdit(before, after Plan, started map[string]bool) error { if err := Validate(after); err != nil { return err } if before.TaskID != after.TaskID || before.ProjectID != after.ProjectID || before.Revision != after.Revision { return ErrInvalid } for i, s := range before.Steps { if started[s.Key] { if i >= len(after.Steps) || !reflect.DeepEqual(s, after.Steps[i]) { return ErrLocked } // Ordered plans depend on their complete prefix, including implicit edges. for j := 0; j < i; j++ { if before.Steps[j].Key != after.Steps[j].Key { return ErrLocked } } } } return nil } func Normalize(p Plan) Plan { p.Steps = append([]Step(nil), p.Steps...) for i := range p.Steps { s := &p.Steps[i] s.Required = s.Action == "person" || (Publication(p.Kind) && (s.Action == "review" || s.Action == "checks")) } return p }