| 1 | package library |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "slices" |
| 6 | "strconv" |
| 7 | |
| 8 | "amadan.net/oficina/equipo/internal/equipo/kernel" |
| 9 | "amadan.net/oficina/equipo/internal/taskplan" |
| 10 | ) |
| 11 | |
| 12 | // PlaybookCatalog returns fresh version-pinned definitions on each call. |
| 13 | func PlaybookCatalog() []Template { |
| 14 | rows := []Template{} |
| 15 | for _, id := range []string{"bug", "website", "infra", "investigation", "blank"} { |
| 16 | rows = append(rows, Template{Ref: TemplateRef{"playbook_" + id, 1}, Group: "playbooks", GroupKey: "playbooks.title", NameKey: "templates.playbook_" + id + ".name", DescriptionKey: "templates.playbook_" + id + ".description", InstructionsKey: "templates.playbook_" + id + ".instructions", Icon: "route"}) |
| 17 | } |
| 18 | return rows |
| 19 | } |
| 20 | func (s *Store) playbookCatalog() []Template { |
| 21 | if s.deps.PlaybookCatalog != nil { |
| 22 | return s.deps.PlaybookCatalog() |
| 23 | } |
| 24 | return PlaybookCatalog() |
| 25 | } |
| 26 | func (s *Store) playbookTemplate(ref TemplateRef) (Playbook, error) { |
| 27 | for _, t := range s.playbookCatalog() { |
| 28 | if t.Ref == ref { |
| 29 | p := taskplan.BugPlan("", 1) |
| 30 | switch ref.ID { |
| 31 | case "playbook_bug": |
| 32 | case "playbook_website": |
| 33 | p.Kind = taskplan.RenderedChange |
| 34 | p.Steps = append([]taskplan.Step{{Key: "design", Specialist: "design", Action: "design"}, {Key: "copy", Specialist: "copy", Action: "copy"}}, p.Steps[1:]...) |
| 35 | case "playbook_infra": |
| 36 | p.Kind = taskplan.Feature |
| 37 | p.Steps[0] = taskplan.Step{Key: "scope", Specialist: "product", Action: "scope"} |
| 38 | case "playbook_investigation": |
| 39 | p.Kind = taskplan.Investigation |
| 40 | p.Steps = append([]taskplan.Step{{Key: "research", Specialist: "engineering", Action: "investigate"}}, p.Steps[2:4]...) |
| 41 | p.Steps = append(p.Steps, taskplan.Step{Key: "merge", Action: "person", Required: true}) |
| 42 | case "playbook_blank": |
| 43 | p.Kind = taskplan.Feature |
| 44 | p.Steps = p.Steps[2:] |
| 45 | default: |
| 46 | return Playbook{}, s.deps.NotFound |
| 47 | } |
| 48 | p.Steps[0].Instructions = s.deps.Copy(t.InstructionsKey) |
| 49 | row := Playbook{ID: "template:" + ref.ID + ":" + strconv.FormatInt(ref.Version, 10), Name: s.deps.Copy(t.NameKey), Kind: p.Kind, Steps: p.Steps, ProjectIDs: []int64{}, Template: ref, Revision: ref.Version} |
| 50 | return row, nil |
| 51 | } |
| 52 | } |
| 53 | return Playbook{}, s.deps.NotFound |
| 54 | } |
| 55 | func playbookValues(row Playbook) map[string]json.RawMessage { |
| 56 | values := map[string]json.RawMessage{} |
| 57 | for k, v := range map[string]any{"name": row.Name, "kind": row.Kind, "steps": row.Steps, "project_ids": row.ProjectIDs} { |
| 58 | raw, _ := json.Marshal(v) |
| 59 | values[k] = raw |
| 60 | } |
| 61 | return values |
| 62 | } |
| 63 | func (s *Store) InstallPlaybookTemplate(actor kernel.Actor, ref TemplateRef, m kernel.Mutation) (Playbook, error) { |
| 64 | a, err := s.deps.Authorize(s.tx, actor) |
| 65 | if err != nil { |
| 66 | return Playbook{}, err |
| 67 | } |
| 68 | q, old, replay, err := s.playbookReplay(a, actor, "playbook.install", ref.ID, m, ref) |
| 69 | if err != nil || replay { |
| 70 | return old, err |
| 71 | } |
| 72 | row, err := s.playbookTemplate(ref) |
| 73 | if err != nil { |
| 74 | return Playbook{}, err |
| 75 | } |
| 76 | in := PlaybookFields(row) |
| 77 | in.ID = "" |
| 78 | return s.writePlaybook(a, actor, Playbook{Template: ref}, in, m, q) |
| 79 | } |
| 80 | func (s *Store) PlaybookTemplateDiff(actor kernel.Actor, id string, target TemplateRef) (TemplateDiff, error) { |
| 81 | row, err := s.Playbook(actor, id) |
| 82 | if err != nil { |
| 83 | return TemplateDiff{}, err |
| 84 | } |
| 85 | if row.Archived || row.Template.ID == "" || row.Template.ID != target.ID || target.Version < row.Template.Version { |
| 86 | return TemplateDiff{}, s.deps.Invalid |
| 87 | } |
| 88 | next, err := s.playbookTemplate(target) |
| 89 | if err != nil { |
| 90 | return TemplateDiff{}, err |
| 91 | } |
| 92 | var old map[string]json.RawMessage |
| 93 | if err = json.Unmarshal([]byte(row.TemplateBase), &old); err != nil { |
| 94 | return TemplateDiff{}, err |
| 95 | } |
| 96 | return TemplateDiff{From: row.Template, To: target, CurrentRevision: row.Revision, Old: old, Current: playbookValues(row), Suggested: playbookValues(next)}, nil |
| 97 | } |
| 98 | func (s *Store) ApplyPlaybookTemplate(actor kernel.Actor, id string, diff TemplateDiff, selected []string, m kernel.Mutation) (Playbook, error) { |
| 99 | a, row, err := s.playbookEditor(actor, id) |
| 100 | if err != nil { |
| 101 | return Playbook{}, err |
| 102 | } |
| 103 | q, old, replay, err := s.playbookReplay(a, actor, "playbook.template_update", id, m, struct { |
| 104 | Diff TemplateDiff |
| 105 | Selected []string |
| 106 | }{diff, selected}) |
| 107 | if err != nil || replay { |
| 108 | return old, err |
| 109 | } |
| 110 | current, err := s.PlaybookTemplateDiff(actor, id, diff.To) |
| 111 | if err != nil { |
| 112 | return Playbook{}, err |
| 113 | } |
| 114 | if m.Revision != diff.CurrentRevision || !equalDiff(current, diff) { |
| 115 | return Playbook{}, s.deps.Conflict |
| 116 | } |
| 117 | if len(selected) == 0 { |
| 118 | return Playbook{}, s.deps.Invalid |
| 119 | } |
| 120 | values := playbookValues(row) |
| 121 | seen := []string{} |
| 122 | for _, k := range selected { |
| 123 | v, ok := diff.Suggested[k] |
| 124 | if !ok || slices.Contains(seen, k) { |
| 125 | return Playbook{}, s.deps.Invalid |
| 126 | } |
| 127 | seen = append(seen, k) |
| 128 | values[k] = v |
| 129 | } |
| 130 | in := PlaybookFields(row) |
| 131 | for k, dst := range map[string]any{"name": &in.Name, "kind": &in.Kind, "steps": &in.Steps, "project_ids": &in.ProjectIDs} { |
| 132 | if json.Unmarshal(values[k], dst) != nil { |
| 133 | return Playbook{}, s.deps.Invalid |
| 134 | } |
| 135 | } |
| 136 | row.Template = diff.To |
| 137 | raw, _ := json.Marshal(diff.Suggested) |
| 138 | row.TemplateBase = string(raw) |
| 139 | return s.writePlaybook(a, actor, row, in, m, q) |
| 140 | } |
| 141 | |