| 1 | package equipo |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "crypto/rand" |
| 6 | "encoding/json" |
| 7 | "errors" |
| 8 | "strings" |
| 9 | "unicode/utf8" |
| 10 | |
| 11 | "amadan.net/oficina/equipo/internal/equipo/conversation" |
| 12 | "amadan.net/oficina/equipo/internal/equipo/planning" |
| 13 | "amadan.net/oficina/equipo/internal/equipo/team" |
| 14 | "gorm.io/gorm" |
| 15 | ) |
| 16 | |
| 17 | type JobID = team.JobID |
| 18 | type TargetBinding = team.TargetBinding |
| 19 | |
| 20 | type Task = planning.Task |
| 21 | type taskOutbox = planning.Outbox |
| 22 | |
| 23 | func validBugDescription(s string) bool { return utf8.ValidString(s) && len(s) >= 1 && len(s) <= 16384 } |
| 24 | func bugAuthority(tx *gorm.DB, actor Actor, projectID int64) (Project, error) { |
| 25 | member, err := currentRunnerMember(tx, actor.Address) |
| 26 | if err != nil { |
| 27 | return Project{}, err |
| 28 | } |
| 29 | var p Project |
| 30 | if err := tx.First(&p, projectID).Error; err != nil { |
| 31 | return p, storeError(err) |
| 32 | } |
| 33 | if member.Role != RoleOwner { |
| 34 | var owner DecisionOwner |
| 35 | if err := tx.Where("project_id=? AND decision=?", projectID, "work").First(&owner).Error; err != nil { |
| 36 | return p, storeError(err) |
| 37 | } |
| 38 | if owner.Address != member.Address { |
| 39 | return p, ErrForbidden |
| 40 | } |
| 41 | } |
| 42 | return p, nil |
| 43 | } |
| 44 | func bugBudget(tx *gorm.DB, teamID string, projectID int64) (BudgetConfig, error) { |
| 45 | var row budgetRecord |
| 46 | if err := tx.Where("team_id=?", teamID).First(&row).Error; err != nil { |
| 47 | return BudgetConfig{}, storeError(err) |
| 48 | } |
| 49 | var config BudgetConfig |
| 50 | if json.Unmarshal([]byte(row.Config), &config) != nil || config.Revision != row.Revision || config.Revision <= 0 { |
| 51 | return config, ErrUnavailable |
| 52 | } |
| 53 | if (team.BudgetSet{Revision: config.Revision, Team: config.Team, Projects: config.Projects}).Validate() != nil { |
| 54 | return config, ErrUnavailable |
| 55 | } |
| 56 | return config, nil |
| 57 | } |
| 58 | func (s *Store) CreateBug(ctx context.Context, actor Actor, projectID int64, description, requestID string) (Task, error) { |
| 59 | if projectID <= 0 || !validBugDescription(description) || strings.TrimSpace(requestID) == "" || len(requestID) > 256 || !utf8.ValidString(requestID) { |
| 60 | return Task{}, ErrInvalid |
| 61 | } |
| 62 | var task Task |
| 63 | err := s.write(ctx, func(tx *gorm.DB) error { |
| 64 | project, err := bugAuthority(tx, actor, projectID) |
| 65 | if err != nil { |
| 66 | return err |
| 67 | } |
| 68 | requester := normalizeAddress(actor.Address) |
| 69 | err = tx.Where("requester=? AND request_id=?", requester, requestID).First(&task).Error |
| 70 | if err == nil { |
| 71 | if task.ProjectID != projectID || task.Description != description { |
| 72 | return ErrConflict |
| 73 | } |
| 74 | return nil |
| 75 | } |
| 76 | if !errors.Is(err, gorm.ErrRecordNotFound) { |
| 77 | return storeError(err) |
| 78 | } |
| 79 | var instance Team |
| 80 | if err := tx.First(&instance).Error; err != nil { |
| 81 | return storeError(err) |
| 82 | } |
| 83 | budget, err := bugBudget(tx, instance.ID, projectID) |
| 84 | if err != nil { |
| 85 | return err |
| 86 | } |
| 87 | engineering, review, err := s.bugPair(tx, requester, projectID) |
| 88 | wait := "" |
| 89 | if err != nil { |
| 90 | if !taskRefusal(err) { |
| 91 | return err |
| 92 | } |
| 93 | wait, err = s.pairWait(tx, requester, projectID) |
| 94 | if err != nil { |
| 95 | return err |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | var merge DecisionOwner |
| 100 | if err := tx.Where("project_id=? AND decision=?", projectID, "merge").First(&merge).Error; err != nil { |
| 101 | return storeError(err) |
| 102 | } |
| 103 | if _, err := currentRunnerMember(tx, merge.Address); err != nil { |
| 104 | return err |
| 105 | } |
| 106 | task = Task{ID: rand.Text(), Requester: requester, Description: description, RequestID: requestID, ProjectID: projectID, Revision: 1, State: "preparing", WaitCode: wait, TeamID: instance.ID, MergeOwner: merge.Address} |
| 107 | if wait != "" { |
| 108 | task.State = "waiting" |
| 109 | } |
| 110 | task.ReviewRef = "equipo/" + task.ID |
| 111 | eng, _ := json.Marshal(engineering) |
| 112 | rev, _ := json.Marshal(review) |
| 113 | outbox := taskOutbox{TaskID: task.ID, State: "prepare", ProjectRevision: project.Revision, BudgetRevision: budget.Revision, Engineering: string(eng), Review: string(rev)} |
| 114 | if err := tx.Create(&task).Error; err != nil { |
| 115 | return storeError(err) |
| 116 | } |
| 117 | if _, err := conversation.EnsureThread(tx, task.ID, task.TeamID); err != nil { |
| 118 | return storeError(err) |
| 119 | } |
| 120 | if err := tx.Create(&outbox).Error; err != nil { |
| 121 | return storeError(err) |
| 122 | } |
| 123 | if err := s.planningAt(tx).Seed(task, actor); err != nil { |
| 124 | return err |
| 125 | } |
| 126 | task.Kind = "bug" |
| 127 | task.PlanRevision = 1 |
| 128 | return appendAudit(tx, actor, "task.create", task.ID) |
| 129 | }) |
| 130 | if err != nil { |
| 131 | return Task{}, err |
| 132 | } |
| 133 | return task, nil |
| 134 | } |
| 135 | func (s *Store) Task(ctx context.Context, actor Actor, id string) (Task, error) { |
| 136 | var task Task |
| 137 | err := s.write(ctx, func(tx *gorm.DB) error { |
| 138 | if _, err := currentRunnerMember(tx, actor.Address); err != nil { |
| 139 | return err |
| 140 | } |
| 141 | return storeError(tx.First(&task, "id=?", id).Error) |
| 142 | }) |
| 143 | if err != nil { |
| 144 | return Task{}, err |
| 145 | } |
| 146 | return task, nil |
| 147 | } |
| 148 | |
| 149 | // The app has no saved API price schedule yet. Do not account an API turn as |
| 150 | // subscription usage; the pure plan builder still accepts a priced v2 policy. |
| 151 | func subscriptionSelection(tx *gorm.DB, selected Selection) error { |
| 152 | var c connectionRecord |
| 153 | if err := tx.First(&c, "id=?", selected.ConnectionID).Error; err != nil { |
| 154 | return storeError(err) |
| 155 | } |
| 156 | if err := decodeConnection(&c); err != nil { |
| 157 | return err |
| 158 | } |
| 159 | for _, r := range c.Reports { |
| 160 | if r.Provider == selected.Provider && r.Model == selected.Model && r.Kind == "subscription" { |
| 161 | return nil |
| 162 | } |
| 163 | } |
| 164 | return ErrUnavailable |
| 165 | } |
| 166 | |