internal/httpd/mractions.go
203 lines · 6700 bytes
1package httpd
2
3import (
4 "fmt"
5 "net/http"
6 "net/url"
7 "strconv"
8 "strings"
9
10 "gitbay.org/gitbay/internal/control"
11 "gitbay.org/gitbay/internal/gitutil"
12 "gitbay.org/gitbay/internal/store"
13)
14
15// Merge request actions. Each one runs the control command the CLI runs,
16// so review rules, merge gates, and audit entries have a single
17// implementation; the browser only chooses arguments and shows the
18// result.
19
20// mrRedirect returns to the merge request, carrying a failure message the
21// page renders as a banner.
22func (s *Server) mrRedirect(w http.ResponseWriter, r *http.Request, msg string) {
23 dest := fmt.Sprintf("/%s/%s/mrs/%s",
24 r.PathValue("owner"), r.PathValue("repo"), r.PathValue("n"))
25 s.setFlash(w, msg)
26 http.Redirect(w, r, dest, http.StatusSeeOther)
27}
28
29// mrDiffRedirect returns to the diff view, where the thread controls are.
30func (s *Server) mrDiffRedirect(w http.ResponseWriter, r *http.Request, msg string) {
31 dest := fmt.Sprintf("/%s/%s/mrs/%s?view=diff",
32 r.PathValue("owner"), r.PathValue("repo"), r.PathValue("n"))
33 s.setFlash(w, msg)
34 http.Redirect(w, r, dest, http.StatusSeeOther)
35}
36
37// mrArgs builds "<verb> owner/name <n>" for the mr command family.
38func mrArgs(r *http.Request, verb string, extra ...string) []string {
39 repo := r.PathValue("owner") + "/" + r.PathValue("repo")
40 return append([]string{"mr", verb, repo, r.PathValue("n")}, extra...)
41}
42
43func (s *Server) mrReviewSubmit(w http.ResponseWriter, r *http.Request, u store.User) {
44 flag := map[string]string{
45 "approve": "--approve",
46 "request-changes": "--request-changes",
47 "comment": "--comment",
48 }[r.FormValue("verdict")]
49 if flag == "" {
50 s.mrRedirect(w, r, "pick approve, request changes, or comment")
51 return
52 }
53 _, msg, code := s.runControlCode(u, mrArgs(r, "review", flag))
54 s.done(w, r, code, msg, s.mrRedirect)
55}
56
57func (s *Server) mrMergeSubmit(w http.ResponseWriter, r *http.Request, u store.User) {
58 args := []string{}
59 if st := strings.TrimSpace(r.FormValue("strategy")); st != "" && st != "auto" {
60 args = append(args, "--strategy", st)
61 }
62 _, msg, code := s.runControlCode(u, mrArgs(r, "merge", args...))
63 s.done(w, r, code, msg, s.mrRedirect)
64}
65
66func (s *Server) mrCloseSubmit(w http.ResponseWriter, r *http.Request, u store.User) {
67 _, msg, code := s.runControlCode(u, mrArgs(r, "close"))
68 s.done(w, r, code, msg, s.mrRedirect)
69}
70
71// mrDraftSubmit toggles the draft mark. The form says which way it is
72// going, so a stale page cannot flip the wrong one.
73func (s *Server) mrDraftSubmit(w http.ResponseWriter, r *http.Request, u store.User) {
74 verb := "ready"
75 if r.FormValue("draft") == "on" {
76 verb = "draft"
77 }
78 _, msg, code := s.runControlCode(u, mrArgs(r, verb))
79 s.done(w, r, code, msg, s.mrRedirect)
80}
81
82// mrDiffCommentSubmit opens a review thread on a diff line, or replies to
83// one. The body goes in on stdin: it is user prose, and argv is visible in
84// /proc.
85func (s *Server) mrDiffCommentSubmit(w http.ResponseWriter, r *http.Request, u store.User) {
86 body := strings.TrimSpace(r.FormValue("body"))
87 if body == "" {
88 s.mrDiffRedirect(w, r, "empty comment")
89 return
90 }
91 var extra []string
92 if reply := strings.TrimSpace(r.FormValue("reply")); reply != "" {
93 if _, err := strconv.ParseInt(reply, 10, 64); err != nil {
94 s.mrDiffRedirect(w, r, "bad thread id")
95 return
96 }
97 extra = []string{"--reply", reply}
98 } else {
99 path := strings.TrimSpace(r.FormValue("path"))
100 line := strings.TrimSpace(r.FormValue("line"))
101 if n, err := strconv.ParseInt(line, 10, 64); path == "" || err != nil || n < 1 {
102 s.mrDiffRedirect(w, r, "pick a line to comment on")
103 return
104 }
105 extra = []string{"--path", path, "--line", line}
106 if r.FormValue("side") == "old" {
107 extra = append(extra, "--old")
108 }
109 }
110 msg, code := s.runControlStdinCode(u, mrArgs(r, "diff-comment", append(extra, "--file", "-")...), body)
111 s.done(w, r, code, msg, s.mrDiffRedirect)
112}
113
114// mrRetargetSubmit moves the merge request onto another branch.
115func (s *Server) mrRetargetSubmit(w http.ResponseWriter, r *http.Request, u store.User) {
116 target := strings.TrimSpace(r.FormValue("target"))
117 if target == "" {
118 s.mrRedirect(w, r, "pick a branch to retarget onto")
119 return
120 }
121 _, msg, code := s.runControlCode(u, mrArgs(r, "retarget", target))
122 s.done(w, r, code, msg, s.mrRedirect)
123}
124
125// mrThreadSubmit resolves or reopens one review thread.
126func (s *Server) mrThreadSubmit(w http.ResponseWriter, r *http.Request, u store.User) {
127 verb := "resolve"
128 if r.FormValue("action") == "unresolve" {
129 verb = "unresolve"
130 }
131 id := strings.TrimSpace(r.FormValue("thread"))
132 if _, err := strconv.ParseInt(id, 10, 64); err != nil {
133 s.mrRedirect(w, r, "bad thread id")
134 return
135 }
136 _, msg, code := s.runControlCode(u, mrArgs(r, verb, id))
137 s.done(w, r, code, msg, s.mrRedirect)
138}
139
140// mrNewPage is the create form: branches to choose from, plus whatever
141// the last attempt had in it so a refusal does not lose the draft.
142type mrNewPage struct {
143 repoPage
144 Branches []gitutil.Ref
145 Source string
146 Target string
147 Title string
148 Body string
149 Notice string
150}
151
152func (s *Server) mrCreateForm(w http.ResponseWriter, r *http.Request, u store.User) {
153 p, ok := s.repoFor(w, r, "")
154 if !ok {
155 return
156 }
157 p.Tab = "merge requests"
158 branches, _ := gitutil.Refs(p.Dir, "heads")
159 q := r.URL.Query()
160 target := q.Get("target")
161 if target == "" {
162 target = p.Repo.DefaultBranch
163 }
164 s.render(w, "mrnew.html", mrNewPage{
165 repoPage: p, Branches: branches,
166 Source: q.Get("source"), Target: target,
167 Title: q.Get("title"), Body: q.Get("body"), Notice: s.takeFlash(w, r),
168 })
169}
170
171func (s *Server) mrCreateSubmit(w http.ResponseWriter, r *http.Request, u store.User) {
172 p, ok := s.repoFor(w, r, "")
173 if !ok {
174 return
175 }
176 source := strings.TrimSpace(r.FormValue("source"))
177 target := strings.TrimSpace(r.FormValue("target"))
178 title := strings.TrimSpace(r.FormValue("title"))
179 body := strings.TrimSpace(r.FormValue("body"))
180
181 back := func(msg string) {
182 q := url.Values{"source": {source}, "target": {target}, "title": {title}, "body": {body}}
183 s.setFlash(w, msg)
184 http.Redirect(w, r, fmt.Sprintf("/%s/mrs/new?%s", p.Repo.Path(), q.Encode()), http.StatusSeeOther)
185 }
186 if source == "" || title == "" {
187 back("pick a source branch and give the merge request a title")
188 return
189 }
190 argv := []string{"mr", "create", p.Repo.Path(), "--source", source, "--title", title}
191 if target != "" {
192 argv = append(argv, "--target", target)
193 }
194 if body != "" {
195 argv = append(argv, "--body", body)
196 }
197 var created control.MRCreated
198 if msg, ok := s.runControlInto(u, argv, &created); !ok {
199 back(msg)
200 return
201 }
202 http.Redirect(w, r, fmt.Sprintf("/%s/mrs/%d", p.Repo.Path(), created.Number), http.StatusSeeOther)
203}