| 1 | package equipo |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "fmt" |
| 6 | "net/http" |
| 7 | "strconv" |
| 8 | "strings" |
| 9 | |
| 10 | "amadan.net/oficina/equipo/internal/equipo/chat" |
| 11 | "amadan.net/oficina/equipo/internal/equipo/mergegate" |
| 12 | "amadan.net/oficina/equipo/internal/wire" |
| 13 | "github.com/go-chi/chi/v5" |
| 14 | ) |
| 15 | |
| 16 | // mergePolicyView is a project page's "How Equipo merges" section: the |
| 17 | // policy in plain sentences, the person's words, the recent decisions, |
| 18 | // and an owner's form. |
| 19 | type mergePolicyView struct { |
| 20 | wire.MergePolicy |
| 21 | Sentences []string |
| 22 | Extra string |
| 23 | Recent []mergeDecisionLine |
| 24 | } |
| 25 | |
| 26 | type mergeDecisionLine struct { |
| 27 | Label, Failing string |
| 28 | } |
| 29 | |
| 30 | func mergePolicyViewOf(p wire.MergePolicy) *mergePolicyView { |
| 31 | v := &mergePolicyView{MergePolicy: p, Extra: strings.Join(p.ExtraSensitive, "\n")} |
| 32 | if p.Mode == mergegate.ModeAsk { |
| 33 | v.Sentences = append(v.Sentences, copyText("work.merge_policy.ask")) |
| 34 | if p.ModeReason != "" { |
| 35 | v.Sentences = append(v.Sentences, fmt.Sprintf(copyText("work.merge_policy.reason"), p.ModeReason)) |
| 36 | } |
| 37 | } else { |
| 38 | v.Sentences = append(v.Sentences, fmt.Sprintf(copyText("work.merge_policy.rules"), p.MaxLines, p.MaxFiles)) |
| 39 | } |
| 40 | v.Sentences = append(v.Sentences, copyText("work.merge_policy.ships_"+p.MainShips)) |
| 41 | if len(p.ExtraSensitive) > 0 { |
| 42 | v.Sentences = append(v.Sentences, fmt.Sprintf(copyText("work.merge_policy.extra"), strings.Join(p.ExtraSensitive, ", "))) |
| 43 | } |
| 44 | for _, d := range p.Decisions { |
| 45 | var failing []string |
| 46 | for _, r := range d.Reasons { |
| 47 | if !r.OK { |
| 48 | failing = append(failing, r.Name) |
| 49 | } |
| 50 | } |
| 51 | v.Recent = append(v.Recent, mergeDecisionLine{Label: copyText("work.merge_policy.decision." + d.Class), Failing: strings.Join(failing, ", ")}) |
| 52 | } |
| 53 | return v |
| 54 | } |
| 55 | |
| 56 | // workMergePolicy is an owner's form on the project page: their own |
| 57 | // action, so it may loosen the policy. The store takes the whole policy, |
| 58 | // so what the post leaves out is the policy at its revision: the form |
| 59 | // has no ship question, and the ship buttons (only=main_ships) send |
| 60 | // nothing else. The revision read must be the one posted, and the store |
| 61 | // refuses the write unless it is still current, so a stale page changes |
| 62 | // nothing. |
| 63 | func (a *application) workMergePolicy(w http.ResponseWriter, r *http.Request) { |
| 64 | id := chi.URLParam(r, "id") |
| 65 | a.chatActionTo(w, r, "/projects/"+id, func(ctx context.Context, address string) error { |
| 66 | atoi := func(name string) int { |
| 67 | n, err := strconv.Atoi(strings.TrimSpace(r.PostFormValue(name))) |
| 68 | if err != nil { |
| 69 | return -1 |
| 70 | } |
| 71 | return n |
| 72 | } |
| 73 | rev, err := strconv.ParseInt(r.PostFormValue("revision"), 10, 64) |
| 74 | if err != nil { |
| 75 | return ErrInvalid |
| 76 | } |
| 77 | cur, err := a.store.Chat().MergePolicy(ctx, address, id) |
| 78 | if err != nil { |
| 79 | return err |
| 80 | } |
| 81 | if cur.Revision != rev { |
| 82 | return chat.ErrRevision |
| 83 | } |
| 84 | in := wire.SetMergePolicy{Mode: cur.Mode, MaxLines: cur.MaxLines, MaxFiles: cur.MaxFiles, MainShips: cur.MainShips, ExtraSensitive: cur.ExtraSensitive, Words: cur.Words, Revision: rev} |
| 85 | if r.PostFormValue("only") == "main_ships" { |
| 86 | if !r.PostForm.Has("main_ships") { |
| 87 | return ErrInvalid |
| 88 | } |
| 89 | } else { |
| 90 | var extra []string |
| 91 | for _, line := range strings.Split(r.PostFormValue("extra_sensitive"), "\n") { |
| 92 | if g := strings.TrimSpace(line); g != "" { |
| 93 | extra = append(extra, g) |
| 94 | } |
| 95 | } |
| 96 | in.Mode, in.MaxLines, in.MaxFiles, in.ExtraSensitive, in.Words = r.PostFormValue("mode"), atoi("max_lines"), atoi("max_files"), extra, r.PostFormValue("words") |
| 97 | } |
| 98 | if r.PostForm.Has("main_ships") { |
| 99 | in.MainShips = r.PostFormValue("main_ships") |
| 100 | } |
| 101 | _, err = a.store.Chat().SetMergePolicy(ctx, address, id, in) |
| 102 | return err |
| 103 | }) |
| 104 | } |
| 105 | |