oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1package planning
2
3import (
4 "crypto/rand"
5 "encoding/json"
6 "errors"
7 "strings"
8
9 "amadan.net/oficina/equipo/internal/equipo/kernel"
10 "amadan.net/oficina/equipo/internal/taskplan"
11 "gorm.io/gorm"
12)
13
14type ProjectOption struct {
15 ID int64
16 Name string
17}
18type StepView struct {
19 Pick *SelectionView
20 ConfirmID string
21 taskplan.Step
22 Index, Position int
23 State, Account, Actual, Depends string
24 Locked bool
25}
26type Page struct {
27 CanSelect bool
28 PlaybookName string
29 PlaybookChanges []string
30 Connections []ConnectionOption
31 Task Task
32 Plan taskplan.Plan
33 Project string
34 Projects []ProjectOption
35 Steps []StepView
36 ProjectSetting, PersonalSetting ApprovalSetting
37 Effective ApprovalMode
38 CanEdit, Owner bool
39 RequestID, ApproveID, CorrectID, ProjectSettingID, PersonalSettingID string
40}
41
42func (s *Store) Page(a kernel.Actor, id string) (out Page, err error) {
43 out.Task, err = s.visibleTask(a, id)
44 if err != nil {
45 return
46 }
47 out.Plan, err = s.Plan(out.Task)
48 if err != nil {
49 return
50 }
51 if s.deps.PlaybookDetails != nil {
52 out.PlaybookName, out.PlaybookChanges, err = s.deps.PlaybookDetails(s.tx, out.Task.TeamID, out.Plan)
53 if err != nil {
54 return
55 }
56 }
57 _, accessErr := s.deps.Access(s.tx, a, out.Task.ProjectID)
58 out.CanEdit = accessErr == nil && a.Address == out.Task.Requester
59 out.CanSelect = out.CanEdit && a.Address == out.Task.Requester
60 out.Owner = s.deps.Member(s.tx, a, true) == nil
61 out.RequestID, out.ApproveID, out.CorrectID = rand.Text(), rand.Text(), rand.Text()
62 out.ProjectSettingID, out.PersonalSettingID = rand.Text(), rand.Text()
63 if err = s.tx.Table("projects").Select("id,name").Order("name,id").Find(&out.Projects).Error; err != nil {
64 return
65 }
66 for _, p := range out.Projects {
67 if p.ID == out.Plan.ProjectID {
68 out.Project = p.Name
69 }
70 }
71 for _, setting := range []struct {
72 person string
73 out *ApprovalSetting
74 }{{"", &out.ProjectSetting}, {out.Task.Requester, &out.PersonalSetting}} {
75 err = s.tx.Where("project_id=? AND person=?", out.Plan.ProjectID, setting.person).Take(setting.out).Error
76 if errors.Is(err, gorm.ErrRecordNotFound) {
77 err = nil
78 }
79 if err != nil {
80 return
81 }
82 }
83 if out.CanSelect {
84 out.Connections, err = s.connectionOptions(a, out.Plan.ProjectID)
85 }
86 if err != nil {
87 return
88 }
89 out.Effective, err = s.Approval(out.Task.Requester, out.Plan.ProjectID)
90 if err != nil {
91 return
92 }
93 var steps []struct {
94 Key, State string
95 ID int64
96 }
97 if out.Task.JobID != 0 {
98 err = s.tx.Table("steps").Select("id,key,state").Where("job_id=?", out.Task.JobID).Find(&steps).Error
99 if err != nil {
100 return
101 }
102 }
103 for i, st := range out.Plan.Steps {
104 v := StepView{Step: st, Index: i, Position: i + 1, State: "pending", Depends: strings.Join(st.DependsOn, ",")}
105 for _, actual := range steps {
106 if actual.Key == st.Key {
107 v.State = actual.State
108 var n int64
109 if err = s.tx.Table("attempts").Where("step_id=?", actual.ID).Count(&n).Error; err != nil {
110 return
111 }
112 v.Locked = n > 0 || actual.State != "pending"
113 var a struct{ Provider, Model string }
114 e := s.tx.Table("attempts").Select("provider,model").Where("step_id=?", actual.ID).Order("id DESC").Take(&a).Error
115 if e != nil && !errors.Is(e, gorm.ErrRecordNotFound) {
116 err = e
117 return
118 }
119 if a.Provider != "" {
120 v.Actual = a.Provider + " / " + a.Model
121 }
122 }
123 }
124 var n int64
125 if err = s.tx.Table("runner_assignments").Where("task_id=? AND job_id=? AND step_key=? AND claim_request<>''", id, out.Task.JobID, st.Key).Count(&n).Error; err != nil {
126 return
127 }
128 v.Locked = v.Locked || n > 0
129 // Account labels are shown only to the account's person. IDs are opaque.
130 if st.ConnectionID != "" {
131 var row struct{ ReportsJSON string }
132 e := s.tx.Table("connections").Select("reports_json").Where("id=? AND person=?", st.ConnectionID, a.Address).Take(&row).Error
133 if e != nil && !errors.Is(e, gorm.ErrRecordNotFound) {
134 err = e
135 return
136 }
137 v.Account = ""
138 if v.Account == "" && row.ReportsJSON != "" {
139 var reports []struct{ AccountLabel string }
140 if err = json.Unmarshal([]byte(row.ReportsJSON), &reports); err != nil {
141 return
142 }
143 if len(reports) > 0 {
144 v.Account = reports[0].AccountLabel
145 }
146 }
147 }
148 v.Pick, err = s.selectionView(a, out.Task, st.Key)
149 if err != nil {
150 return
151 }
152 v.ConfirmID = rand.Text()
153 out.Steps = append(out.Steps, v)
154 }
155 return
156}
157
158func (s *Store) visibleTask(a kernel.Actor, id string) (Task, error) {
159 if err := s.deps.Member(s.tx, a, false); err != nil {
160 return Task{}, err
161 }
162 var teamID string
163 if err := s.tx.Table("teams").Select("id").Row().Scan(&teamID); err != nil {
164 return Task{}, err
165 }
166 var t Task
167 err := s.tx.Where("id=? AND team_id=?", id, teamID).First(&t).Error
168 return t, err
169}
170
171type ConnectionOption struct{ ID, Label string }
172
173func (s *Store) connectionOptions(a kernel.Actor, project int64) ([]ConnectionOption, error) {
174 var choice struct{ ConnectionIDsJSON string }
175 err := s.tx.Table("connection_choices").Where("person=? AND project_id=?", a.Address, project).Take(&choice).Error
176 if errors.Is(err, gorm.ErrRecordNotFound) {
177 return nil, nil
178 }
179 if err != nil {
180 return nil, err
181 }
182 var ids []string
183 if err = json.Unmarshal([]byte(choice.ConnectionIDsJSON), &ids); err != nil {
184 return nil, err
185 }
186 var rows []struct{ ID, ReportsJSON string }
187 if err = s.tx.Table("connections").Select("id,reports_json").Where("person=? AND id IN ?", a.Address, ids).Order("id").Find(&rows).Error; err != nil {
188 return nil, err
189 }
190 var out []ConnectionOption
191 for _, row := range rows {
192 var reports []struct{ AccountLabel, Provider, Model string }
193 if err = json.Unmarshal([]byte(row.ReportsJSON), &reports); err != nil {
194 return nil, err
195 }
196 if len(reports) == 0 {
197 continue
198 }
199 label := reports[0].AccountLabel
200 if label == "" {
201 label = reports[0].Provider + " / " + reports[0].Model
202 }
203 out = append(out, ConnectionOption{row.ID, label})
204 }
205 return out, nil
206}
207