| 1 | package equipo |
| 2 | |
| 3 | import ( |
| 4 | "amadan.net/oficina/equipo/internal/equipo/github" |
| 5 | "amadan.net/oficina/equipo/internal/release" |
| 6 | "amadan.net/rastrillo/rastrillo/sessions" |
| 7 | "context" |
| 8 | "errors" |
| 9 | "fmt" |
| 10 | "log/slog" |
| 11 | "time" |
| 12 | |
| 13 | "gorm.io/gorm" |
| 14 | "gorm.io/plugin/dbresolver" |
| 15 | |
| 16 | "amadan.net/oficina/equipo/internal/equipo/assist" |
| 17 | "amadan.net/oficina/equipo/internal/equipo/library" |
| 18 | "amadan.net/oficina/equipo/internal/equipo/oficina" |
| 19 | "amadan.net/oficina/equipo/internal/equipo/personapi" |
| 20 | "amadan.net/oficina/equipo/internal/equipo/planning" |
| 21 | "amadan.net/oficina/equipo/internal/equipo/setup" |
| 22 | "amadan.net/oficina/equipo/internal/equipo/team" |
| 23 | "amadan.net/rastrillo/rastrillo/db" |
| 24 | "net/http" |
| 25 | ) |
| 26 | |
| 27 | var ( |
| 28 | //lint:ignore ST1005 User-facing errors preserve the reviewed sentences in copy.go. |
| 29 | ErrForbidden = errors.New(copyForbidden) |
| 30 | //lint:ignore ST1005 User-facing errors preserve the reviewed sentences in copy.go. |
| 31 | ErrNotFound = errors.New(copyNotFound) |
| 32 | //lint:ignore ST1005 User-facing errors preserve the reviewed sentences in copy.go. |
| 33 | ErrConflict = errors.New(copyConflict) |
| 34 | //lint:ignore ST1005 User-facing errors preserve the reviewed sentences in copy.go. |
| 35 | ErrInvalid = errors.New(copyInvalid) |
| 36 | //lint:ignore ST1005 User-facing errors preserve the reviewed sentences in copy.go. |
| 37 | ErrUnavailable = errors.New(copyUnavailable) |
| 38 | ErrDecisionOwnerAssigned = &refusal{copyDecisionOwnerAssigned, ErrConflict} |
| 39 | ErrBindingInUse = &refusal{copyBindingInUse, ErrConflict} |
| 40 | ErrLastOwner = &refusal{copyLastOwner, ErrForbidden} |
| 41 | ErrAccountLabelExists = &refusal{copyAccountLabelExists, ErrConflict} |
| 42 | ErrBootstrapOwners = &refusal{copyBootstrapOwners, ErrInvalid} |
| 43 | ) |
| 44 | |
| 45 | // A refusal keeps its specific reason while preserving the error category. |
| 46 | type refusal struct { |
| 47 | message string |
| 48 | kind error |
| 49 | } |
| 50 | |
| 51 | func (e *refusal) Error() string { return e.message } |
| 52 | func (e *refusal) Unwrap() error { return e.kind } |
| 53 | |
| 54 | type PlanApplication = planning.Application |
| 55 | type InvitationApplication = setup.Application |
| 56 | |
| 57 | // Store grants no access by itself. Mutators must check the trusted Actor |
| 58 | // against the current roster and append an AuditEvent in their transaction. |
| 59 | |
| 60 | type Store struct { |
| 61 | forge *github.Store |
| 62 | coordinatorRelease *release.Manifest |
| 63 | *NotificationApplication |
| 64 | *PlanApplication |
| 65 | *InvitationApplication |
| 66 | *library.Application |
| 67 | personBearer personapi.VerifyPersonBearer |
| 68 | personNativeSession func(*gorm.DB, string) (sessions.Session, error) |
| 69 | personCookie func(*http.Request) (personapi.PersonPrincipal, error) |
| 70 | invitationKey string |
| 71 | personOrigin string |
| 72 | personKey string |
| 73 | notifier JobNotifier |
| 74 | db *db.DB |
| 75 | execution *Execution |
| 76 | runnerClock team.Clock |
| 77 | verifiedIsolation []team.IsolationTuple |
| 78 | chat thinChat |
| 79 | // chatExecution is EQUIPO_EXECUTION_ENABLED: without it no runner |
| 80 | // receives coordinator turns. |
| 81 | chatExecution bool |
| 82 | // chatTestPeople are EQUIPO_TEST_PEOPLE's address suffixes. |
| 83 | chatTestPeople []string |
| 84 | // chatOrigin is EQUIPO_ORIGIN, for chat.Authority.Origin. |
| 85 | chatOrigin string |
| 86 | // chatAssistantHosts is EQUIPO_ASSISTANT_HOSTS. |
| 87 | chatAssistantHosts []string |
| 88 | // oficina is the oficina-connect client, nil while |
| 89 | // EQUIPO_OFICINA_CONNECT_ORIGIN is unset; assist the Assistant's |
| 90 | // registry, kept to forget a person's lists when they connect. |
| 91 | oficina *oficina.Connector |
| 92 | // oficinaPause is a test's pause inside oficinaCatchUp. |
| 93 | oficinaPause func() |
| 94 | assist *assist.Registry |
| 95 | } |
| 96 | |
| 97 | type StoreOption func(*Store) |
| 98 | |
| 99 | func WithRunnerClock(clock team.Clock) StoreOption { |
| 100 | return func(s *Store) { |
| 101 | if clock != nil { |
| 102 | s.runnerClock = clock |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | func NewStore(d *db.DB, options ...StoreOption) *Store { |
| 108 | s := &Store{db: d, runnerClock: team.RealClock{}} |
| 109 | for _, option := range options { |
| 110 | option(s) |
| 111 | } |
| 112 | s.NotificationApplication = s.notificationApplication() |
| 113 | s.InvitationApplication = &setup.Application{Write: s.write, Read: s.invitationRead, At: s.invitationsAt, Normalize: normalizeAddress, ValidAddress: validAddress, Invalid: ErrInvalid, Unavailable: ErrUnavailable} |
| 114 | s.PlanApplication = &planning.Application{Write: s.write, At: s.planningAt, Normalize: normalizeAddress} |
| 115 | s.Application = &library.Application{Write: s.write, At: s.libraryAt, Normalize: normalizeAddress, Read: func(ctx context.Context, a Actor, fn func(*gorm.DB) error) error { |
| 116 | return s.readTeam(ctx, a, func(tx *gorm.DB, _ Team) error { return fn(tx) }) |
| 117 | }} |
| 118 | return s |
| 119 | } |
| 120 | |
| 121 | func storeError(err error) error { |
| 122 | if err == nil { |
| 123 | return nil |
| 124 | } |
| 125 | if errors.Is(err, gorm.ErrRecordNotFound) { |
| 126 | return ErrNotFound |
| 127 | } |
| 128 | return fmt.Errorf("%w: %w", ErrUnavailable, err) |
| 129 | } |
| 130 | |
| 131 | func (s *Store) write(ctx context.Context, fn func(*gorm.DB) error) error { |
| 132 | if s == nil || s.db == nil { |
| 133 | return ErrUnavailable |
| 134 | } |
| 135 | original := fn |
| 136 | fn = func(tx *gorm.DB) error { |
| 137 | if err := s.currentPersonAPI(tx, ctx); err != nil { |
| 138 | return err |
| 139 | } |
| 140 | if err := original(tx); err != nil { |
| 141 | return err |
| 142 | } |
| 143 | return personapi.Record(tx, ctx, ErrConflict) |
| 144 | } |
| 145 | var err error |
| 146 | if s.execution != nil { |
| 147 | err = s.execution.Write(ctx, fn) |
| 148 | } else { |
| 149 | var notify func() |
| 150 | err = s.db.G.WithContext(ctx).Clauses(dbresolver.Write).Transaction(func(tx *gorm.DB) error { |
| 151 | raw, e := executionTx(tx) |
| 152 | if e != nil { |
| 153 | return e |
| 154 | } |
| 155 | observer, e := s.observeWrite(ctx, raw) |
| 156 | if e != nil { |
| 157 | observer = nil |
| 158 | observerFailed() |
| 159 | } |
| 160 | if e = fn(tx); e != nil { |
| 161 | return e |
| 162 | } |
| 163 | if observer != nil { |
| 164 | notify, e = observer(ctx, raw) |
| 165 | if e != nil { |
| 166 | notify = nil |
| 167 | observerFailed() |
| 168 | } |
| 169 | } |
| 170 | return nil |
| 171 | }) |
| 172 | if err == nil && notify != nil { |
| 173 | notify() |
| 174 | } |
| 175 | } |
| 176 | if err == nil || errors.Is(err, ErrForbidden) || errors.Is(err, ErrNotFound) || errors.Is(err, ErrConflict) || errors.Is(err, ErrInvalid) || errors.Is(err, ErrUnavailable) || errors.Is(err, ErrCursorExpired) { |
| 177 | return err |
| 178 | } |
| 179 | return storeError(err) |
| 180 | } |
| 181 | |
| 182 | func appendAudit(tx *gorm.DB, actor Actor, kind, recordID string) error { |
| 183 | return storeError(tx.Create(&AuditEvent{Actor: normalizeAddress(actor.Address), Kind: kind, RecordID: recordID, At: time.Now().UTC()}).Error) |
| 184 | } |
| 185 | |
| 186 | func observerFailed() { slog.Warn(copyObserverFailure) } |
| 187 | |