| 1 | package planning |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "strconv" |
| 6 | |
| 7 | "amadan.net/oficina/equipo/internal/equipo/kernel" |
| 8 | "gorm.io/gorm" |
| 9 | ) |
| 10 | |
| 11 | type ApprovalMode string |
| 12 | |
| 13 | const ( |
| 14 | ApprovalImmediate ApprovalMode = "immediate" |
| 15 | ApprovalRequired ApprovalMode = "required" |
| 16 | ) |
| 17 | |
| 18 | type ApprovalSetting struct { |
| 19 | ProjectID int64 `gorm:"primaryKey;autoIncrement:false"` |
| 20 | Person string `gorm:"primaryKey"` |
| 21 | Mode ApprovalMode |
| 22 | Revision int64 |
| 23 | } |
| 24 | |
| 25 | func (ApprovalSetting) TableName() string { return "plan_approval_settings" } |
| 26 | func validMode(mode ApprovalMode) bool { return mode == ApprovalImmediate || mode == ApprovalRequired } |
| 27 | func (s *Store) Approval(person string, projectID int64) (ApprovalMode, error) { |
| 28 | for _, p := range []string{person, ""} { |
| 29 | var row ApprovalSetting |
| 30 | err := s.tx.First(&row, "project_id=? AND person=?", projectID, p).Error |
| 31 | if errors.Is(err, gorm.ErrRecordNotFound) { |
| 32 | continue |
| 33 | } |
| 34 | if err != nil { |
| 35 | return "", err |
| 36 | } |
| 37 | if row.Mode == "" && p != "" { |
| 38 | continue |
| 39 | } |
| 40 | if !validMode(row.Mode) { |
| 41 | return "", s.deps.Unavailable |
| 42 | } |
| 43 | return row.Mode, nil |
| 44 | } |
| 45 | return ApprovalImmediate, nil |
| 46 | } |
| 47 | func (s *Store) SetApproval(actor kernel.Actor, projectID int64, mode *ApprovalMode, m kernel.Mutation, project bool) error { |
| 48 | if err := s.deps.Member(s.tx, actor, project); err != nil { |
| 49 | return err |
| 50 | } |
| 51 | if err := s.deps.Project(s.tx, projectID); err != nil { |
| 52 | return err |
| 53 | } |
| 54 | if (project && mode == nil) || (mode != nil && !validMode(*mode)) { |
| 55 | return s.deps.Invalid |
| 56 | } |
| 57 | person, op := actor.Address, "plan.override" |
| 58 | if project { |
| 59 | person = "" |
| 60 | op = "plan.project_approval" |
| 61 | } |
| 62 | var result bool |
| 63 | q, replay, err := s.request(actor, op, strconv.FormatInt(projectID, 10), m, struct { |
| 64 | Mode *ApprovalMode |
| 65 | Revision int64 |
| 66 | }{mode, m.Revision}, &result) |
| 67 | if err != nil || replay { |
| 68 | return err |
| 69 | } |
| 70 | row := ApprovalSetting{ProjectID: projectID, Person: person} |
| 71 | err = s.tx.First(&row, "project_id=? AND person=?", projectID, person).Error |
| 72 | exists := err == nil |
| 73 | if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) { |
| 74 | return err |
| 75 | } |
| 76 | if row.Revision != m.Revision { |
| 77 | return s.deps.Conflict |
| 78 | } |
| 79 | row.Mode = "" |
| 80 | if mode != nil { |
| 81 | row.Mode = *mode |
| 82 | } |
| 83 | row.Revision++ |
| 84 | if exists { |
| 85 | err = s.tx.Model(&ApprovalSetting{}).Where("project_id=? AND person=?", projectID, person).Select("Mode", "Revision").Updates(&row).Error |
| 86 | } else { |
| 87 | err = s.tx.Create(&row).Error |
| 88 | } |
| 89 | if err != nil { |
| 90 | return err |
| 91 | } |
| 92 | if err = s.deps.Audit(s.tx, actor, op, strconv.FormatInt(projectID, 10)); err != nil { |
| 93 | return err |
| 94 | } |
| 95 | return s.save(q, true) |
| 96 | } |
| 97 | func (s *Store) Approve(actor kernel.Actor, id string, m kernel.Mutation) (Task, error) { |
| 98 | t, err := s.requesterTask(actor, id) |
| 99 | if err != nil { |
| 100 | return t, err |
| 101 | } |
| 102 | var result Task |
| 103 | q, replay, err := s.request(actor, "plan.approve", id, m, m, &result) |
| 104 | if err != nil || replay { |
| 105 | return result, err |
| 106 | } |
| 107 | p, err := s.Plan(t) |
| 108 | if err != nil { |
| 109 | return result, err |
| 110 | } |
| 111 | if m.Revision <= 0 || p.Revision != m.Revision { |
| 112 | return result, s.deps.Conflict |
| 113 | } |
| 114 | ready := s.deps.ApprovalReady |
| 115 | if ready == nil { |
| 116 | ready = s.deps.Ready |
| 117 | } |
| 118 | if err = ready(s.tx, t, p); err != nil { |
| 119 | return result, err |
| 120 | } |
| 121 | if t.PlanRevision == 0 { |
| 122 | t.PlanRevision = 1 |
| 123 | if err = s.history(&t, p, actor, nil); err != nil { |
| 124 | return result, err |
| 125 | } |
| 126 | } |
| 127 | t.ApprovalRevision = t.PlanRevision |
| 128 | if t.WaitCode == "plan.awaiting_approval" { |
| 129 | t.WaitCode = "" |
| 130 | } |
| 131 | t.Revision++ |
| 132 | if err = s.persist(t, actor, "plan.approve"); err != nil { |
| 133 | return result, err |
| 134 | } |
| 135 | return t, s.save(q, t) |
| 136 | } |
| 137 | func (s *Store) CheckDispatch(actor kernel.Actor, id string) error { |
| 138 | t, err := s.task(actor, id) |
| 139 | if err != nil { |
| 140 | return err |
| 141 | } |
| 142 | switch t.State { |
| 143 | case "cancelled", "cancel_requested", "merged", "succeeded", "failed", "merge": |
| 144 | return s.deps.Conflict |
| 145 | } |
| 146 | p, err := s.Plan(t) |
| 147 | if err != nil { |
| 148 | return err |
| 149 | } |
| 150 | if taskErr := taskPlanValid(p); taskErr != nil { |
| 151 | return s.deps.Invalid |
| 152 | } |
| 153 | mode, err := s.Approval(t.Requester, t.ProjectID) |
| 154 | if err != nil { |
| 155 | return err |
| 156 | } |
| 157 | if (mode == ApprovalRequired || t.ReplacesID != "") && t.ApprovalRevision != p.Revision { |
| 158 | return s.deps.AwaitingApproval |
| 159 | } |
| 160 | return s.deps.Ready(s.tx, t, p) |
| 161 | } |
| 162 | |