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 "net/http"
5 "net/url"
6 "sort"
7 "strconv"
8 "strings"
9
10 "amadan.net/oficina/equipo/internal/equipo/identity"
11 "amadan.net/oficina/equipo/internal/equipo/kernel"
12 "amadan.net/oficina/equipo/internal/taskplan"
13 "github.com/go-chi/chi/v5"
14)
15
16type Handler struct {
17 App *Application
18 Invalid, Forbidden error
19 Failure func(http.ResponseWriter, *http.Request, error)
20 Render func(http.ResponseWriter, *http.Request, Page)
21}
22
23func (h *Handler) Routes(r chi.Router) {
24 r.Get("/tasks/{id}/plan", h.Page)
25 r.Post("/tasks/{id}/plan", h.Edit)
26 r.Post("/tasks/{id}/plan/approve", h.Approve)
27 r.Post("/tasks/{id}/project", h.Correct)
28 r.Post("/tasks/{id}/plan/settings", h.Settings)
29}
30func ReadMutation(w http.ResponseWriter, r *http.Request, invalid error) (kernel.Mutation, error) {
31 r.Body = http.MaxBytesReader(w, r.Body, 64<<10)
32 if r.ParseForm() != nil {
33 return kernel.Mutation{}, invalid
34 }
35 for _, v := range r.PostForm {
36 if len(v) != 1 {
37 return kernel.Mutation{}, invalid
38 }
39 }
40 revision, err := strconv.ParseInt(r.PostForm.Get("revision"), 10, 64)
41 m := kernel.Mutation{RequestID: r.PostForm.Get("request_id"), Revision: revision}
42 if err != nil || !m.Valid() {
43 return m, invalid
44 }
45 return m, nil
46}
47func (h *Handler) actor(w http.ResponseWriter, r *http.Request) (kernel.Actor, bool) {
48 w.Header().Set("Cache-Control", "no-store")
49 id, ok := identity.From(r)
50 if !ok {
51 h.Failure(w, r, h.Forbidden)
52 }
53 return kernel.Actor{Address: id.Address}, ok
54}
55func (h *Handler) Page(w http.ResponseWriter, r *http.Request) {
56 a, ok := h.actor(w, r)
57 if !ok {
58 return
59 }
60 v, err := h.App.Page(r.Context(), a, chi.URLParam(r, "id"))
61 if err != nil {
62 h.Failure(w, r, err)
63 return
64 }
65 h.Render(w, r, v)
66}
67func (h *Handler) finish(w http.ResponseWriter, r *http.Request, id string, err error) {
68 if err != nil {
69 h.Failure(w, r, err)
70 return
71 }
72 http.Redirect(w, r, "/tasks/"+url.PathEscape(id)+"/plan", http.StatusSeeOther)
73}
74func (h *Handler) Edit(w http.ResponseWriter, r *http.Request) {
75 a, ok := h.actor(w, r)
76 if !ok {
77 return
78 }
79 m, err := ReadMutation(w, r, h.Invalid)
80 if err == nil {
81 var p taskplan.Plan
82 p, err = decodePlan(r.PostForm, chi.URLParam(r, "id"), m, h.Invalid)
83 if err == nil {
84 _, err = h.App.EditPlan(r.Context(), a, p, m)
85 }
86 }
87 h.finish(w, r, chi.URLParam(r, "id"), err)
88}
89func decodePlan(v url.Values, id string, m kernel.Mutation, invalid error) (taskplan.Plan, error) {
90 p := taskplan.Plan{TaskID: id, Revision: m.Revision, Kind: taskplan.Kind(v.Get("kind")), PlaybookID: v.Get("playbook_id")}
91 var err error
92 p.ProjectID, err = strconv.ParseInt(v.Get("project_id"), 10, 64)
93 if err != nil {
94 return p, invalid
95 }
96 p.PlaybookRevision, err = strconv.ParseInt(v.Get("playbook_revision"), 10, 64)
97 if err != nil {
98 return p, invalid
99 }
100 count, err := strconv.Atoi(v.Get("step_count"))
101 if err != nil || count < 1 || count > taskplan.MaxSteps {
102 return p, invalid
103 }
104 type positioned struct {
105 step taskplan.Step
106 position int
107 }
108 var ordered []positioned
109 used := map[int]bool{}
110 for i := 0; i <= count; i++ {
111 prefix := "step_" + strconv.Itoa(i) + "_"
112 if i == count && v.Get(prefix+"key") == "" {
113 break
114 }
115 if v.Get(prefix+"remove") == "1" {
116 continue
117 }
118 pos, e := strconv.Atoi(v.Get(prefix + "position"))
119 if e != nil || pos < 1 || pos > taskplan.MaxSteps || used[pos] {
120 return p, invalid
121 }
122 used[pos] = true
123 st := taskplan.Step{Key: v.Get(prefix + "key"), Specialist: v.Get(prefix + "specialist"), Action: v.Get(prefix + "action"), Instructions: v.Get(prefix + "instructions"), ConnectionID: v.Get(prefix + "connection"), Model: v.Get(prefix + "model"), SelectionReason: v.Get(prefix + "reason")}
124 if dep := v.Get(prefix + "depends_on"); dep != "" {
125 st.DependsOn = strings.Split(dep, ",")
126 }
127 ordered = append(ordered, positioned{st, pos})
128 }
129 sort.SliceStable(ordered, func(i, j int) bool { return ordered[i].position < ordered[j].position })
130 for _, st := range ordered {
131 p.Steps = append(p.Steps, st.step)
132 }
133 return p, nil
134}
135func (h *Handler) Approve(w http.ResponseWriter, r *http.Request) {
136 a, ok := h.actor(w, r)
137 if !ok {
138 return
139 }
140 m, err := ReadMutation(w, r, h.Invalid)
141 if err == nil {
142 _, err = h.App.ApprovePlan(r.Context(), a, chi.URLParam(r, "id"), m)
143 }
144 h.finish(w, r, chi.URLParam(r, "id"), err)
145}
146func (h *Handler) Correct(w http.ResponseWriter, r *http.Request) {
147 a, ok := h.actor(w, r)
148 if !ok {
149 return
150 }
151 id := chi.URLParam(r, "id")
152 m, err := ReadMutation(w, r, h.Invalid)
153 if err == nil {
154 project, e := strconv.ParseInt(r.PostForm.Get("project_id"), 10, 64)
155 if e != nil {
156 err = h.Invalid
157 } else {
158 var out Task
159 out, err = h.App.CorrectTaskProject(r.Context(), a, id, project, m)
160 if err == nil && out.ReplacementID != "" {
161 id = out.ReplacementID
162 }
163 }
164 }
165 h.finish(w, r, id, err)
166}
167func (h *Handler) Settings(w http.ResponseWriter, r *http.Request) {
168 a, ok := h.actor(w, r)
169 if !ok {
170 return
171 }
172 id := chi.URLParam(r, "id")
173 m, err := ReadMutation(w, r, h.Invalid)
174 if err == nil {
175 var p taskplan.Plan
176 p, err = h.App.TaskPlan(r.Context(), a, id)
177 if err == nil {
178 mode := ApprovalMode(r.PostForm.Get("mode"))
179 switch r.PostForm.Get("scope") {
180 case "project":
181 err = h.App.SetProjectApproval(r.Context(), a, p.ProjectID, mode, m)
182 case "personal":
183 var value *ApprovalMode
184 if mode != "" {
185 value = &mode
186 }
187 err = h.App.SetApprovalOverride(r.Context(), a, p.ProjectID, value, m)
188 default:
189 err = h.Invalid
190 }
191 }
192 }
193 h.finish(w, r, id, err)
194}
195