| 1 | package library |
| 2 | |
| 3 | import ( |
| 4 | "crypto/rand" |
| 5 | "encoding/json" |
| 6 | "errors" |
| 7 | "slices" |
| 8 | "strings" |
| 9 | "unicode/utf8" |
| 10 | |
| 11 | "amadan.net/oficina/equipo/internal/equipo/kernel" |
| 12 | "amadan.net/oficina/equipo/internal/taskplan" |
| 13 | "gorm.io/gorm" |
| 14 | ) |
| 15 | |
| 16 | type PlaybookInput struct { |
| 17 | ID, Name string |
| 18 | Kind taskplan.Kind |
| 19 | Steps []taskplan.Step |
| 20 | ProjectIDs []int64 |
| 21 | Template TemplateRef |
| 22 | } |
| 23 | type Playbook struct { |
| 24 | ID string `gorm:"primaryKey"` |
| 25 | TeamID, Creator, Name string |
| 26 | Kind taskplan.Kind |
| 27 | Steps []taskplan.Step `gorm:"serializer:json"` |
| 28 | ProjectIDs []int64 `gorm:"serializer:json"` |
| 29 | Template TemplateRef `gorm:"embedded;embeddedPrefix:template_"` |
| 30 | TemplateBase string |
| 31 | Revision int64 |
| 32 | Archived bool |
| 33 | } |
| 34 | type PlaybookRevision struct { |
| 35 | ID string `gorm:"primaryKey"` |
| 36 | Revision int64 `gorm:"primaryKey;autoIncrement:false"` |
| 37 | Value string |
| 38 | } |
| 39 | type PlaybookDefault struct { |
| 40 | TeamID string `gorm:"primaryKey"` |
| 41 | ProjectID int64 `gorm:"primaryKey;autoIncrement:false"` |
| 42 | Kind taskplan.Kind `gorm:"primaryKey"` |
| 43 | PlaybookID string |
| 44 | Revision int64 |
| 45 | } |
| 46 | type PlaybookSnapshot struct { |
| 47 | TaskID string `gorm:"primaryKey"` |
| 48 | PlaybookID string `gorm:"primaryKey"` |
| 49 | Plan string |
| 50 | } |
| 51 | type playbookRequest request |
| 52 | |
| 53 | func (playbookRequest) TableName() string { return "playbook_requests" } |
| 54 | |
| 55 | func PlaybookFields(row Playbook) PlaybookInput { |
| 56 | steps := slices.Clone(row.Steps) |
| 57 | for i := range steps { |
| 58 | steps[i].DependsOn = slices.Clone(steps[i].DependsOn) |
| 59 | } |
| 60 | return PlaybookInput{ID: row.ID, Name: row.Name, Kind: row.Kind, Steps: steps, ProjectIDs: slices.Clone(row.ProjectIDs), Template: row.Template} |
| 61 | } |
| 62 | func (s *Store) Playbook(actor kernel.Actor, id string) (Playbook, error) { |
| 63 | a, err := s.deps.Authorize(s.tx, actor) |
| 64 | if err != nil { |
| 65 | return Playbook{}, err |
| 66 | } |
| 67 | return s.playbook(a, id) |
| 68 | } |
| 69 | func (s *Store) playbook(a Access, id string) (Playbook, error) { |
| 70 | var row Playbook |
| 71 | err := s.tx.Where("id=? AND team_id=?", id, a.TeamID).First(&row).Error |
| 72 | if errors.Is(err, gorm.ErrRecordNotFound) { |
| 73 | err = s.deps.NotFound |
| 74 | } |
| 75 | return row, err |
| 76 | } |
| 77 | func (s *Store) Playbooks(actor kernel.Actor) ([]Playbook, error) { |
| 78 | a, err := s.deps.Authorize(s.tx, actor) |
| 79 | if err != nil { |
| 80 | return nil, err |
| 81 | } |
| 82 | var rows []Playbook |
| 83 | err = s.tx.Where("team_id=? AND archived=?", a.TeamID, false).Order("name,id").Find(&rows).Error |
| 84 | return rows, err |
| 85 | } |
| 86 | func (s *Store) playbookEditor(actor kernel.Actor, id string) (Access, Playbook, error) { |
| 87 | a, err := s.deps.Authorize(s.tx, actor) |
| 88 | if err != nil { |
| 89 | return a, Playbook{}, err |
| 90 | } |
| 91 | if id == "" { |
| 92 | return a, Playbook{}, nil |
| 93 | } |
| 94 | row, err := s.playbook(a, id) |
| 95 | if err != nil { |
| 96 | return a, row, err |
| 97 | } |
| 98 | if !a.Owner && row.Creator != actor.Address { |
| 99 | return a, row, s.deps.Forbidden |
| 100 | } |
| 101 | return a, row, nil |
| 102 | } |
| 103 | func (s *Store) validatePlaybook(a Access, in PlaybookInput) error { |
| 104 | if strings.TrimSpace(in.Name) == "" || len(in.Name) > 120 || !utf8.ValidString(in.Name) || len(in.ProjectIDs) > 100 || taskplan.ValidatePlaybook(in.Kind, in.Steps) != nil { |
| 105 | return s.deps.Invalid |
| 106 | } |
| 107 | ids := map[int64]bool{} |
| 108 | for _, id := range in.ProjectIDs { |
| 109 | if id <= 0 || ids[id] { |
| 110 | return s.deps.Invalid |
| 111 | } |
| 112 | ids[id] = true |
| 113 | var p kernel.Project |
| 114 | if err := s.tx.First(&p, id).Error; err != nil { |
| 115 | return err |
| 116 | } |
| 117 | } |
| 118 | for _, st := range in.Steps { |
| 119 | // A shared playbook cannot capture a person's account selection. |
| 120 | if st.ConnectionID != "" || st.Model != "" || st.SelectionReason != "" { |
| 121 | return s.deps.Invalid |
| 122 | } |
| 123 | if st.Action == "person" || st.Action == "checks" || st.Action == "publish" { |
| 124 | if st.Specialist != "" { |
| 125 | return s.deps.Invalid |
| 126 | } |
| 127 | continue |
| 128 | } |
| 129 | key := st.Specialist |
| 130 | if key == "" { |
| 131 | key = "engineering" |
| 132 | } |
| 133 | sp, err := s.get(a, key) |
| 134 | if err != nil { |
| 135 | return err |
| 136 | } |
| 137 | if sp.Archived { |
| 138 | return s.deps.Forbidden |
| 139 | } |
| 140 | if len(sp.ProjectIDs) > 0 { |
| 141 | if len(in.ProjectIDs) == 0 { |
| 142 | return s.deps.Forbidden |
| 143 | } |
| 144 | for _, id := range in.ProjectIDs { |
| 145 | if !slices.Contains(sp.ProjectIDs, id) { |
| 146 | return s.deps.Forbidden |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | } |
| 151 | return nil |
| 152 | } |
| 153 | func (s *Store) playbookReplay(a Access, actor kernel.Actor, op, id string, m kernel.Mutation, input any) (playbookRequest, Playbook, bool, error) { |
| 154 | var result Playbook |
| 155 | if !m.Valid() { |
| 156 | return playbookRequest{}, result, false, s.deps.Invalid |
| 157 | } |
| 158 | hash, err := kernel.InputHash(struct { |
| 159 | Input any |
| 160 | Revision int64 |
| 161 | }{input, m.Revision}) |
| 162 | if err != nil { |
| 163 | return playbookRequest{}, result, false, err |
| 164 | } |
| 165 | q := playbookRequest{TeamID: a.TeamID, Principal: actor.Address, Operation: op, Resource: id, RequestID: m.RequestID, InputHash: hash} |
| 166 | var old playbookRequest |
| 167 | err = s.tx.Where("team_id=? AND principal=? AND operation=? AND resource=? AND request_id=?", a.TeamID, actor.Address, op, id, m.RequestID).First(&old).Error |
| 168 | if errors.Is(err, gorm.ErrRecordNotFound) { |
| 169 | return q, result, false, nil |
| 170 | } |
| 171 | if err != nil { |
| 172 | return q, result, false, err |
| 173 | } |
| 174 | if old.InputHash != hash { |
| 175 | return q, result, true, s.deps.Conflict |
| 176 | } |
| 177 | err = json.Unmarshal([]byte(old.Result), &result) |
| 178 | return q, result, true, err |
| 179 | } |
| 180 | func (s *Store) finishPlaybook(actor kernel.Actor, q playbookRequest, row Playbook) (Playbook, error) { |
| 181 | if err := s.deps.Audit(s.tx, actor, q.Operation, row.ID); err != nil { |
| 182 | return Playbook{}, err |
| 183 | } |
| 184 | raw, err := json.Marshal(row) |
| 185 | if err != nil { |
| 186 | return Playbook{}, err |
| 187 | } |
| 188 | q.Result = string(raw) |
| 189 | return row, s.tx.Create(&q).Error |
| 190 | } |
| 191 | func (s *Store) SavePlaybook(actor kernel.Actor, in PlaybookInput, m kernel.Mutation) (Playbook, error) { |
| 192 | a, row, err := s.playbookEditor(actor, in.ID) |
| 193 | if err != nil { |
| 194 | return Playbook{}, err |
| 195 | } |
| 196 | q, old, replay, err := s.playbookReplay(a, actor, "playbook.save", in.ID, m, in) |
| 197 | if err != nil || replay { |
| 198 | return old, err |
| 199 | } |
| 200 | // Provenance is assigned only by an explicit install or accepted update. |
| 201 | if in.Template != row.Template { |
| 202 | return Playbook{}, s.deps.Invalid |
| 203 | } |
| 204 | return s.writePlaybook(a, actor, row, in, m, q) |
| 205 | } |
| 206 | func (s *Store) writePlaybook(a Access, actor kernel.Actor, row Playbook, in PlaybookInput, m kernel.Mutation, q playbookRequest) (Playbook, error) { |
| 207 | if row.Archived || m.Revision != row.Revision { |
| 208 | return Playbook{}, s.deps.Conflict |
| 209 | } |
| 210 | if err := s.validatePlaybook(a, in); err != nil { |
| 211 | return Playbook{}, err |
| 212 | } |
| 213 | fresh := row.ID == "" |
| 214 | if fresh { |
| 215 | row.ID = rand.Text() |
| 216 | row.Creator = actor.Address |
| 217 | row.TeamID = a.TeamID |
| 218 | } |
| 219 | row.Name = in.Name |
| 220 | row.Kind = in.Kind |
| 221 | row.Steps = PlaybookFields(Playbook{Steps: in.Steps}).Steps |
| 222 | row.ProjectIDs = append([]int64{}, in.ProjectIDs...) |
| 223 | row.Revision++ |
| 224 | for i := range row.Steps { |
| 225 | row.Steps[i].Required = slices.Contains([]string{"review", "checks", "person"}, row.Steps[i].Action) |
| 226 | } |
| 227 | if fresh && row.Template.ID != "" { |
| 228 | raw, _ := json.Marshal(playbookValues(row)) |
| 229 | row.TemplateBase = string(raw) |
| 230 | } |
| 231 | var err error |
| 232 | if fresh { |
| 233 | err = s.tx.Create(&row).Error |
| 234 | } else { |
| 235 | r := s.tx.Model(&Playbook{}).Where("id=? AND team_id=? AND revision=?", row.ID, a.TeamID, m.Revision).Select("Name", "Kind", "Steps", "ProjectIDs", "Revision", "TemplateID", "TemplateVersion", "TemplateBase").Updates(&row) |
| 236 | err = r.Error |
| 237 | if err == nil && r.RowsAffected != 1 { |
| 238 | err = s.deps.Conflict |
| 239 | } |
| 240 | } |
| 241 | if err != nil { |
| 242 | return Playbook{}, err |
| 243 | } |
| 244 | raw, _ := json.Marshal(row) |
| 245 | if err = s.tx.Create(&PlaybookRevision{ID: row.ID, Revision: row.Revision, Value: string(raw)}).Error; err != nil { |
| 246 | return Playbook{}, err |
| 247 | } |
| 248 | return s.finishPlaybook(actor, q, row) |
| 249 | } |
| 250 | func (s *Store) DeletePlaybook(actor kernel.Actor, id string, m kernel.Mutation) error { |
| 251 | a, row, err := s.playbookEditor(actor, id) |
| 252 | if err != nil { |
| 253 | return err |
| 254 | } |
| 255 | if id == "" { |
| 256 | return s.deps.Invalid |
| 257 | } |
| 258 | q, _, replay, err := s.playbookReplay(a, actor, "playbook.archive", id, m, id) |
| 259 | if err != nil || replay { |
| 260 | return err |
| 261 | } |
| 262 | if row.Archived || row.Revision != m.Revision { |
| 263 | return s.deps.Conflict |
| 264 | } |
| 265 | var n int64 |
| 266 | if err = s.tx.Model(&PlaybookDefault{}).Where("team_id=? AND playbook_id=?", a.TeamID, id).Count(&n).Error; err != nil { |
| 267 | return err |
| 268 | } |
| 269 | if n > 0 { |
| 270 | return s.deps.Conflict |
| 271 | } |
| 272 | row.Archived = true |
| 273 | row.Revision++ |
| 274 | if err = s.tx.Model(&Playbook{}).Where("id=? AND team_id=? AND revision=?", id, a.TeamID, m.Revision).Select("Archived", "Revision").Updates(&row).Error; err != nil { |
| 275 | return err |
| 276 | } |
| 277 | _, err = s.finishPlaybook(actor, q, row) |
| 278 | return err |
| 279 | } |
| 280 | |