| 1 | package equipo |
| 2 | |
| 3 | import ( |
| 4 | "amadan.net/oficina/equipo/internal/equipo/conversation" |
| 5 | "amadan.net/oficina/equipo/internal/equipo/library" |
| 6 | "amadan.net/oficina/equipo/internal/equipo/ownermode" |
| 7 | "amadan.net/oficina/equipo/internal/equipo/planning" |
| 8 | "amadan.net/oficina/equipo/internal/taskplan" |
| 9 | "context" |
| 10 | "errors" |
| 11 | "time" |
| 12 | |
| 13 | "amadan.net/oficina/equipo/internal/equipo/team" |
| 14 | "gorm.io/gorm" |
| 15 | ) |
| 16 | |
| 17 | func (s *Store) runnerOwner(tx *gorm.DB, id string) (string, error) { |
| 18 | instance, err := runnerInstance(tx) |
| 19 | if err != nil { |
| 20 | return "", err |
| 21 | } |
| 22 | r, err := runnerAt(tx, instance, id) |
| 23 | return r.Person, err |
| 24 | } |
| 25 | func (s *Store) eligible(tx *gorm.DB, c connectionRecord, r ConnectionReport, now time.Time) (bool, IsolationAcknowledgement, error) { |
| 26 | ok, err := ownermode.Eligible(tx, c, r, now, s.confined, s.runnerOwner, ErrForbidden) |
| 27 | return ok, IsolationAcknowledgement{}, err |
| 28 | } |
| 29 | |
| 30 | var specialistAllows = ownermode.SpecialistAllows |
| 31 | |
| 32 | var knownFamily = ownermode.KnownFamily |
| 33 | |
| 34 | func specialistAt(tx *gorm.DB, key string) (Specialist, error) { |
| 35 | return library.ExecutionSpecialist(tx, key, ErrUnavailable, storeError) |
| 36 | } |
| 37 | |
| 38 | // SelectConnection is an instance eligibility snapshot. It creates neither a |
| 39 | // job nor a lease; admission must repeat these checks in its write transaction. |
| 40 | func (s *Store) SelectConnection(ctx context.Context, person string, projectID int64, specialist, excludedFamily string) (Selection, error) { |
| 41 | var result Selection |
| 42 | err := s.write(ctx, func(tx *gorm.DB) error { |
| 43 | var err error |
| 44 | result, err = s.selectConnectionAt(tx, person, projectID, specialist, excludedFamily) |
| 45 | return err |
| 46 | }) |
| 47 | if err != nil { |
| 48 | return Selection{}, err |
| 49 | } |
| 50 | return result, nil |
| 51 | } |
| 52 | |
| 53 | // The assignment service can reuse this check in its admission transaction. |
| 54 | func (s *Store) selectConnectionAt(tx *gorm.DB, person string, projectID int64, specialist, excludedFamily string) (Selection, error) { |
| 55 | return s.selectConnectionOnRunnerAt(tx, person, projectID, specialist, excludedFamily, "") |
| 56 | } |
| 57 | func (s *Store) selectConnectionOnRunnerAt(tx *gorm.DB, person string, projectID int64, specialist, excludedFamily, runnerID string) (Selection, error) { |
| 58 | return s.selectStepConnection(tx, person, projectID, specialist, excludedFamily, runnerID, taskplan.Step{}, true) |
| 59 | } |
| 60 | func (s *Store) selectStepConnection(tx *gorm.DB, person string, projectID int64, specialist, excludedFamily, runnerID string, step taskplan.Step, requirePair bool) (Selection, error) { |
| 61 | return s.selectStepConnectionConfirmed(tx, person, projectID, specialist, excludedFamily, runnerID, step, requirePair, false) |
| 62 | } |
| 63 | |
| 64 | func (s *Store) selectStepConnectionConfirmed(tx *gorm.DB, person string, projectID int64, specialist, excludedFamily, runnerID string, step taskplan.Step, requirePair, confirmed bool) (Selection, error) { |
| 65 | var result Selection |
| 66 | err := func() error { |
| 67 | m, err := currentRunnerMember(tx, person) |
| 68 | if err != nil { |
| 69 | return err |
| 70 | } |
| 71 | var project Project |
| 72 | if err := tx.First(&project, projectID).Error; err != nil { |
| 73 | return storeError(err) |
| 74 | } |
| 75 | choice, ids, err := choiceAt(tx, m.Address, projectID) |
| 76 | if err != nil { |
| 77 | return err |
| 78 | } |
| 79 | sp, err := specialistAt(tx, specialist) |
| 80 | if err != nil { |
| 81 | return err |
| 82 | } |
| 83 | if !library.ProjectAllowed(sp, projectID) { |
| 84 | return ErrForbidden |
| 85 | } |
| 86 | var instance Team |
| 87 | if err := tx.First(&instance).Error; err != nil { |
| 88 | return err |
| 89 | } |
| 90 | if sp.TeamID != instance.ID { |
| 91 | return ErrNotFound |
| 92 | } |
| 93 | // Custom profiles are not certified in this release. Empty tool sets |
| 94 | // are not an implicit grant of the action's tools. |
| 95 | if sp.Role == "" { |
| 96 | return ErrUnavailable |
| 97 | } |
| 98 | if specialist == "review" && !knownFamily(excludedFamily) { |
| 99 | return ErrUnavailable |
| 100 | } |
| 101 | var rows []connectionRecord |
| 102 | query := tx.Where("person = ? AND id IN ?", m.Address, ids) |
| 103 | if runnerID != "" { |
| 104 | query = query.Where("runner_id = ?", runnerID) |
| 105 | } |
| 106 | if err := query.Order("id").Find(&rows).Error; err != nil { |
| 107 | return storeError(err) |
| 108 | } |
| 109 | for i := range rows { |
| 110 | if err := decodeConnection(&rows[i]); err != nil { |
| 111 | return err |
| 112 | } |
| 113 | } |
| 114 | now := s.runnerClock.Now().UTC() |
| 115 | if choice.Mode == "ask" && !confirmed { |
| 116 | return errPlanSelectionAsk |
| 117 | } |
| 118 | pin := choice.PinnedID |
| 119 | // A project pin applies within its family. Review still needs another |
| 120 | // family; an explicit step pick never receives this exception. |
| 121 | if excludedFamily != "" { |
| 122 | for _, c := range rows { |
| 123 | if c.ID == pin && len(c.Reports) > 0 && c.Reports[0].Family == excludedFamily { |
| 124 | pin = "" |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | if step.ConnectionID != "" { |
| 129 | if pin != "" && pin != step.ConnectionID { |
| 130 | return ErrConflict |
| 131 | } |
| 132 | pin = step.ConnectionID |
| 133 | } |
| 134 | var binding conversation.BindingRecord |
| 135 | bindingErr := tx.Where("person=?", m.Address).Take(&binding).Error |
| 136 | if bindingErr != nil && !errors.Is(bindingErr, gorm.ErrRecordNotFound) { |
| 137 | return bindingErr |
| 138 | } |
| 139 | var candidates []Selection |
| 140 | |
| 141 | for _, c := range rows { |
| 142 | if pin != "" && c.ID != pin { |
| 143 | continue |
| 144 | } |
| 145 | for _, r := range c.Reports { |
| 146 | if !knownFamily(r.Family) || r.Family == excludedFamily || !specialistAllows(sp, r) || (step.Model != "" && step.Model != r.Model) { |
| 147 | continue |
| 148 | } |
| 149 | ok, _, err := s.eligible(tx, c, r, now) |
| 150 | if err != nil { |
| 151 | return err |
| 152 | } |
| 153 | if !ok { |
| 154 | continue |
| 155 | } |
| 156 | if requirePair && specialist == "engineering" { |
| 157 | review, err := specialistAt(tx, "review") |
| 158 | if err != nil { |
| 159 | return err |
| 160 | } |
| 161 | found := false |
| 162 | for _, other := range rows { |
| 163 | if other.RunnerID != c.RunnerID { |
| 164 | continue |
| 165 | } |
| 166 | for _, rr := range other.Reports { |
| 167 | if !knownFamily(rr.Family) || rr.Family == r.Family || !specialistAllows(review, rr) { |
| 168 | continue |
| 169 | } |
| 170 | ok, _, err := s.eligible(tx, other, rr, now) |
| 171 | if err != nil { |
| 172 | return err |
| 173 | } |
| 174 | if ok { |
| 175 | found = true |
| 176 | } |
| 177 | } |
| 178 | } |
| 179 | if !found { |
| 180 | continue |
| 181 | } |
| 182 | } |
| 183 | snapshot, err := s.isolationSnapshot(tx, c, r) |
| 184 | if err != nil { |
| 185 | return err |
| 186 | } |
| 187 | reason := "first_allowed" |
| 188 | if pin != "" { |
| 189 | reason = "pinned" |
| 190 | } |
| 191 | selected := Selection{ConnectionID: c.ID, RunnerID: c.RunnerID, Person: m.Address, Provider: r.Provider, Family: r.Family, Model: r.Model, AccountLabel: r.AccountLabel, AccountID: r.Routing.AccountID, Revision: c.Revision, ChoiceRevision: choice.Revision, SpecialistRevision: sp.Revision, Reason: reason, MaxConcurrent: sp.MaxConcurrent, AllowanceKnown: r.AllowanceKnown, AllowanceRemaining: r.AllowanceRemaining, Isolation: snapshot} |
| 192 | if !selected.AllowanceKnown { |
| 193 | selected.AllowanceRemaining = 0 |
| 194 | } |
| 195 | candidates = append(candidates, selected) |
| 196 | } |
| 197 | } |
| 198 | if len(candidates) == 0 { |
| 199 | return ErrUnavailable |
| 200 | } |
| 201 | result = planning.RankSelections(candidates, binding.ConnectionID, step.Key != "", pin, choice.PinnedID) |
| 202 | return nil |
| 203 | }() |
| 204 | if err != nil { |
| 205 | return Selection{}, err |
| 206 | } |
| 207 | return result, nil |
| 208 | } |
| 209 | func (s *Store) isolationSnapshot(tx *gorm.DB, c connectionRecord, r ConnectionReport) (team.IsolationSnapshot, error) { |
| 210 | return ownermode.Snapshot(tx, c, r, s.confined, s.runnerOwner, ErrForbidden) |
| 211 | } |
| 212 | |