| 1 | package equipo |
| 2 | |
| 3 | import ( |
| 4 | "amadan.net/oficina/equipo/internal/equipo/personapi" |
| 5 | "amadan.net/oficina/equipo/internal/equipo/planning" |
| 6 | "context" |
| 7 | "gorm.io/gorm" |
| 8 | ) |
| 9 | |
| 10 | func (s *Store) saveAPIProjectSettings(ctx context.Context, a Actor, id int64, v personapi.ProjectSettingsInput, m Mutation, personal bool) (out personapi.Result, err error) { |
| 11 | err = s.write(ctx, func(tx *gorm.DB) error { |
| 12 | if _, e := currentRunnerMember(tx, a.Address); e != nil { |
| 13 | return e |
| 14 | } |
| 15 | if !personal { |
| 16 | if e := requireOwner(tx, a); e != nil { |
| 17 | return e |
| 18 | } |
| 19 | } |
| 20 | var ack personapi.Ack |
| 21 | if replay, e := personapi.Replay(tx, ctx, &ack, ErrConflict); e != nil || replay { |
| 22 | out = personapi.Result{Value: ack, Revision: ack.Revision} |
| 23 | return e |
| 24 | } |
| 25 | instance, e := runnerInstance(tx) |
| 26 | if e != nil { |
| 27 | return e |
| 28 | } |
| 29 | before, e := personapi.ProjectSettingsAt(tx, a, id, instance) |
| 30 | if e != nil { |
| 31 | return e |
| 32 | } |
| 33 | if personapi.Revision(before) != m.Revision { |
| 34 | return ErrConflict |
| 35 | } |
| 36 | sub := m |
| 37 | switch v.Action { |
| 38 | case "approval": |
| 39 | if v.Rules != "" || v.PlaybookID != "" || v.Kind != "" { |
| 40 | return ErrInvalid |
| 41 | } |
| 42 | mode := planning.ApprovalMode(v.ApprovalMode) |
| 43 | sub.Revision = before.ApprovalRevision |
| 44 | var ptr *planning.ApprovalMode = &mode |
| 45 | if personal { |
| 46 | sub.Revision = before.MyApprovalRevision |
| 47 | if mode == "" { |
| 48 | ptr = nil |
| 49 | } |
| 50 | } |
| 51 | e = s.planningAt(tx).SetApproval(a, id, ptr, sub, !personal) |
| 52 | case "default_playbook": |
| 53 | if personal || v.Rules != "" || v.ApprovalMode != "" { |
| 54 | return ErrInvalid |
| 55 | } |
| 56 | sub.Revision = 0 |
| 57 | for _, d := range before.Defaults { |
| 58 | if d.Kind == v.Kind { |
| 59 | sub.Revision = d.Revision |
| 60 | } |
| 61 | } |
| 62 | e = s.libraryAt(tx).SetPlaybookDefault(a, id, v.Kind, v.PlaybookID, sub) |
| 63 | case "rules": |
| 64 | if personal || v.ApprovalMode != "" || v.PlaybookID != "" || v.Kind != "" { |
| 65 | return ErrInvalid |
| 66 | } |
| 67 | sub.Revision = before.RulesRevision |
| 68 | e = s.planningAt(tx).SetProjectRules(a, id, v.Rules, sub) |
| 69 | default: |
| 70 | return ErrInvalid |
| 71 | } |
| 72 | if e != nil { |
| 73 | return e |
| 74 | } |
| 75 | after, e := personapi.ProjectSettingsAt(tx, a, id, instance) |
| 76 | if e != nil { |
| 77 | return e |
| 78 | } |
| 79 | ack.Revision = personapi.Revision(after) |
| 80 | out = personapi.Result{Value: ack, Revision: ack.Revision} |
| 81 | return personapi.SaveResult(tx, ctx, ack, ErrConflict) |
| 82 | }) |
| 83 | return |
| 84 | } |
| 85 | |