| 1 | package equipo |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "errors" |
| 6 | |
| 7 | "amadan.net/oficina/equipo/internal/equipo/team" |
| 8 | "gorm.io/gorm" |
| 9 | ) |
| 10 | |
| 11 | func taskRefusal(err error) bool { |
| 12 | return err == ErrUnavailable || err == errPlanSelectionAsk || err == errPlanSecondFamily || errors.Is(err, ErrConflict) || errors.Is(err, ErrForbidden) || errors.Is(err, ErrNotFound) || errors.Is(err, team.ErrBudget) || errors.Is(err, team.ErrGrantRevoked) || errors.Is(err, team.ErrGrantStale) |
| 13 | } |
| 14 | func taskWait(tx *gorm.DB, id, code string) error { |
| 15 | if code == "" { |
| 16 | if err := tx.Model(&Task{}).Where("id=? AND state='waiting'", id).Select("State").Updates(&Task{State: "preparing"}).Error; err != nil { |
| 17 | return storeError(err) |
| 18 | } |
| 19 | } |
| 20 | return storeError(tx.Model(&Task{}).Where("id=? AND wait_code<>?", id, code).Select("WaitCode").Updates(&Task{WaitCode: code}).Error) |
| 21 | } |
| 22 | |
| 23 | // A missing family and an ineligible connection need different setup actions. |
| 24 | func (s *Store) pairWait(tx *gorm.DB, person string, project int64) (string, error) { |
| 25 | _, ids, err := choiceAt(tx, person, project) |
| 26 | if err != nil { |
| 27 | if taskRefusal(err) { |
| 28 | return "task.wait.setup", nil |
| 29 | } |
| 30 | return "", err |
| 31 | } |
| 32 | var rows []connectionRecord |
| 33 | if err := tx.Where("person=? AND id IN ?", person, ids).Find(&rows).Error; err != nil { |
| 34 | return "", storeError(err) |
| 35 | } |
| 36 | families := map[string]map[string]bool{} |
| 37 | for _, c := range rows { |
| 38 | if err := decodeConnection(&c); err != nil { |
| 39 | return "", err |
| 40 | } |
| 41 | if families[c.RunnerID] == nil { |
| 42 | families[c.RunnerID] = map[string]bool{} |
| 43 | } |
| 44 | for _, r := range c.Reports { |
| 45 | if knownFamily(r.Family) { |
| 46 | families[c.RunnerID][r.Family] = true |
| 47 | } |
| 48 | } |
| 49 | } |
| 50 | for _, f := range families { |
| 51 | if len(f) >= 2 { |
| 52 | return "task.wait.setup", nil |
| 53 | } |
| 54 | } |
| 55 | return "task.wait.family", nil |
| 56 | } |
| 57 | |
| 58 | func (s *Store) bugPair(tx *gorm.DB, person string, project int64) (Selection, Selection, error) { |
| 59 | eng, err := s.selectConnectionAt(tx, person, project, "engineering", "") |
| 60 | if err != nil { |
| 61 | return eng, Selection{}, err |
| 62 | } |
| 63 | rev, err := s.selectConnectionOnRunnerAt(tx, person, project, "review", eng.Family, eng.RunnerID) |
| 64 | if err != nil { |
| 65 | return eng, rev, err |
| 66 | } |
| 67 | for _, sel := range []Selection{eng, rev} { |
| 68 | if err := subscriptionSelection(tx, sel); err != nil { |
| 69 | return eng, rev, err |
| 70 | } |
| 71 | } |
| 72 | return eng, rev, nil |
| 73 | } |
| 74 | |
| 75 | func (s *Store) refreshPendingPair(tx *gorm.DB, task Task, out *taskOutbox) error { |
| 76 | if task.JobID != 0 { |
| 77 | return nil |
| 78 | } |
| 79 | project, err := bugAuthority(tx, Actor{Address: task.Requester}, task.ProjectID) |
| 80 | if err != nil { |
| 81 | return err |
| 82 | } |
| 83 | if task.State == "waiting" { |
| 84 | budget, err := bugBudget(tx, task.TeamID, task.ProjectID) |
| 85 | if err != nil { |
| 86 | return err |
| 87 | } |
| 88 | out.ProjectRevision, out.BudgetRevision = project.Revision, budget.Revision |
| 89 | } |
| 90 | plan, err := s.planningAt(tx).Plan(task) |
| 91 | if err != nil { |
| 92 | return err |
| 93 | } |
| 94 | eng, rev, err := s.selectionPolicy().Pair(tx, task, plan, false) |
| 95 | if err != nil { |
| 96 | return err |
| 97 | } |
| 98 | a, _ := json.Marshal(eng) |
| 99 | b, _ := json.Marshal(rev) |
| 100 | out.Engineering, out.Review = string(a), string(b) |
| 101 | return storeError(tx.Model(out).Where("task_id=?", task.ID).Select("Engineering", "Review", "ProjectRevision", "BudgetRevision").Updates(out).Error) |
| 102 | } |
| 103 | |