| 1 | package taskplan |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "reflect" |
| 6 | "strings" |
| 7 | "unicode/utf8" |
| 8 | ) |
| 9 | |
| 10 | var ErrInvalid = errors.New("plan_invalid") |
| 11 | var ErrLocked = errors.New("plan_locked_step") |
| 12 | |
| 13 | func Validate(p Plan) error { |
| 14 | if p.ProjectID <= 0 || p.Revision < 0 || len(p.Steps) == 0 || len(p.Steps) > MaxSteps { |
| 15 | return ErrInvalid |
| 16 | } |
| 17 | if !Publication(p.Kind) && p.Kind != Recommendation && p.Kind != Investigation { |
| 18 | return ErrInvalid |
| 19 | } |
| 20 | seen := map[string]int{} |
| 21 | review, checks, person, publish, lastCandidate := -1, -1, -1, -1, -1 |
| 22 | for i, s := range p.Steps { |
| 23 | 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) { |
| 24 | return ErrInvalid |
| 25 | } |
| 26 | if _, ok := seen[s.Key]; ok { |
| 27 | return ErrInvalid |
| 28 | } |
| 29 | seen[s.Key] = i |
| 30 | for _, dep := range s.DependsOn { |
| 31 | if _, ok := seen[dep]; !ok || dep == s.Key { |
| 32 | return ErrInvalid |
| 33 | } |
| 34 | } |
| 35 | switch s.Action { |
| 36 | case "review": |
| 37 | if s.Key != "review" || s.Specialist != "review" { |
| 38 | return ErrInvalid |
| 39 | } |
| 40 | review = i |
| 41 | case "checks": |
| 42 | if s.Key != "verify" || s.Model != "" || s.ConnectionID != "" { |
| 43 | return ErrInvalid |
| 44 | } |
| 45 | checks = i |
| 46 | case "person": |
| 47 | if s.Key != "merge" || s.Model != "" || s.ConnectionID != "" { |
| 48 | return ErrInvalid |
| 49 | } |
| 50 | person = i |
| 51 | case "publish": |
| 52 | if s.Key != "publish" || s.Model != "" || s.ConnectionID != "" { |
| 53 | return ErrInvalid |
| 54 | } |
| 55 | publish = i |
| 56 | case "implement", "copy", "design": |
| 57 | lastCandidate = i |
| 58 | case "reproduce", "scope", "investigate", "recommend": |
| 59 | default: |
| 60 | return ErrInvalid |
| 61 | } |
| 62 | if !Publication(p.Kind) && s.Action != "person" && s.Action != "scope" && s.Action != "investigate" && s.Action != "recommend" { |
| 63 | return ErrInvalid |
| 64 | } |
| 65 | } |
| 66 | if person != len(p.Steps)-1 { |
| 67 | return ErrInvalid |
| 68 | } |
| 69 | if Publication(p.Kind) { |
| 70 | if review < 0 || checks < 0 || review <= lastCandidate || checks <= lastCandidate { |
| 71 | return ErrInvalid |
| 72 | } |
| 73 | if publish >= 0 && (publish < review || publish < checks) { |
| 74 | return ErrInvalid |
| 75 | } |
| 76 | if publish < 0 && len(p.Steps)+1 > MaxSteps { |
| 77 | return ErrInvalid |
| 78 | } |
| 79 | } |
| 80 | return nil |
| 81 | } |
| 82 | func ValidateEdit(before, after Plan, started map[string]bool) error { |
| 83 | if err := Validate(after); err != nil { |
| 84 | return err |
| 85 | } |
| 86 | if before.TaskID != after.TaskID || before.ProjectID != after.ProjectID || before.Revision != after.Revision { |
| 87 | return ErrInvalid |
| 88 | } |
| 89 | for i, s := range before.Steps { |
| 90 | if started[s.Key] { |
| 91 | if i >= len(after.Steps) || !reflect.DeepEqual(s, after.Steps[i]) { |
| 92 | return ErrLocked |
| 93 | } |
| 94 | // Ordered plans depend on their complete prefix, including implicit edges. |
| 95 | for j := 0; j < i; j++ { |
| 96 | if before.Steps[j].Key != after.Steps[j].Key { |
| 97 | return ErrLocked |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | return nil |
| 103 | } |
| 104 | func Normalize(p Plan) Plan { |
| 105 | p.Steps = append([]Step(nil), p.Steps...) |
| 106 | for i := range p.Steps { |
| 107 | s := &p.Steps[i] |
| 108 | s.Required = s.Action == "person" || (Publication(p.Kind) && (s.Action == "review" || s.Action == "checks")) |
| 109 | } |
| 110 | return p |
| 111 | } |
| 112 | |