internal/httpd/mractions.go
277 lines · 9283 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/policy"
13 "gitbay.org/gitbay/internal/store"
14)
15
16// Merge request actions. Each one runs the control command the CLI runs,
17// so review rules, merge gates, and audit entries have a single
18// implementation; the browser only chooses arguments and shows the
19// result.
20
21// mrRedirect returns to the merge request, carrying a failure message the
22// page renders as a banner.
23func (s *Server) mrRedirect(w http.ResponseWriter, r *http.Request, msg string) {
24 dest := fmt.Sprintf("/%s/%s/mrs/%s",
25 r.PathValue("owner"), r.PathValue("repo"), r.PathValue("n"))
26 s.setFlash(w, msg)
27 http.Redirect(w, r, dest, http.StatusSeeOther)
28}
29
30// mrDiffRedirect returns to the diff view, where the thread controls are.
31func (s *Server) mrDiffRedirect(w http.ResponseWriter, r *http.Request, msg string) {
32 dest := fmt.Sprintf("/%s/%s/mrs/%s?view=diff",
33 r.PathValue("owner"), r.PathValue("repo"), r.PathValue("n"))
34 s.setFlash(w, msg)
35 http.Redirect(w, r, dest, http.StatusSeeOther)
36}
37
38// mrArgs builds "<verb> owner/name <n>" for the mr command family.
39func mrArgs(r *http.Request, verb string, extra ...string) []string {
40 repo := r.PathValue("owner") + "/" + r.PathValue("repo")
41 return append([]string{"mr", verb, repo, r.PathValue("n")}, extra...)
42}
43
44func (s *Server) mrReviewSubmit(w http.ResponseWriter, r *http.Request, u store.User) {
45 flag := map[string]string{
46 "approve": "--approve",
47 "request-changes": "--request-changes",
48 "comment": "--comment",
49 }[r.FormValue("verdict")]
50 if flag == "" {
51 s.mrRedirect(w, r, "pick approve, request changes, or comment")
52 return
53 }
54 _, msg, code := s.runControlCode(u, mrArgs(r, "review", flag))
55 s.done(w, r, code, msg, s.mrRedirect)
56}
57
58func (s *Server) mrReviewRequestSubmit(w http.ResponseWriter, r *http.Request, u store.User) {
59 args := append(fieldArgs("--add", r.FormValue("add")), fieldArgs("--remove", r.FormValue("remove"))...)
60 if len(args) == 0 {
61 s.mrRedirect(w, r, "name at least one person")
62 return
63 }
64 repo := r.PathValue("owner") + "/" + r.PathValue("repo")
65 _, msg, code := s.runControlCode(u, append([]string{"mr", "review", "request", repo, r.PathValue("n")}, args...))
66 s.done(w, r, code, msg, s.mrRedirect)
67}
68
69func (s *Server) mrLabelSubmit(w http.ResponseWriter, r *http.Request, u store.User) {
70 args := append(fieldArgs("--add", r.FormValue("add")), fieldArgs("--remove", r.FormValue("remove"))...)
71 if len(args) == 0 {
72 s.mrRedirect(w, r, "name at least one label")
73 return
74 }
75 _, msg, code := s.runControlCode(u, mrArgs(r, "label", args...))
76 s.done(w, r, code, msg, s.mrRedirect)
77}
78
79func (s *Server) mrMergeSubmit(w http.ResponseWriter, r *http.Request, u store.User) {
80 args := []string{}
81 if st := strings.TrimSpace(r.FormValue("strategy")); st != "" && st != "auto" {
82 args = append(args, "--strategy", st)
83 }
84 _, msg, code := s.runControlCode(u, mrArgs(r, "merge", args...))
85 s.done(w, r, code, msg, s.mrRedirect)
86}
87
88func (s *Server) mrCloseSubmit(w http.ResponseWriter, r *http.Request, u store.User) {
89 args := []string{}
90 if by := strings.TrimSpace(r.FormValue("by")); by != "" {
91 if _, err := strconv.ParseInt(by, 10, 64); err != nil {
92 s.mrRedirect(w, r, "the superseding request is a number")
93 return
94 }
95 args = append(args, "--by", by)
96 }
97 _, msg, code := s.runControlCode(u, mrArgs(r, "close", args...))
98 s.done(w, r, code, msg, s.mrRedirect)
99}
100
101// mrDraftSubmit toggles the draft mark. The form says which way it is
102// going, so a stale page cannot flip the wrong one.
103func (s *Server) mrDraftSubmit(w http.ResponseWriter, r *http.Request, u store.User) {
104 verb := "ready"
105 if r.FormValue("draft") == "on" {
106 verb = "draft"
107 }
108 _, msg, code := s.runControlCode(u, mrArgs(r, verb))
109 s.done(w, r, code, msg, s.mrRedirect)
110}
111
112// mrDiffCommentSubmit opens a review thread on a diff line, or replies to
113// one. The body goes in on stdin: it is user prose, and argv is visible in
114// /proc.
115func (s *Server) mrDiffCommentSubmit(w http.ResponseWriter, r *http.Request, u store.User) {
116 body := strings.TrimSpace(r.FormValue("body"))
117 if body == "" {
118 s.mrDiffRedirect(w, r, "empty comment")
119 return
120 }
121 var extra []string
122 if reply := strings.TrimSpace(r.FormValue("reply")); reply != "" {
123 if _, err := strconv.ParseInt(reply, 10, 64); err != nil {
124 s.mrDiffRedirect(w, r, "bad thread id")
125 return
126 }
127 extra = []string{"--reply", reply}
128 } else {
129 path := strings.TrimSpace(r.FormValue("path"))
130 line := strings.TrimSpace(r.FormValue("line"))
131 if n, err := strconv.ParseInt(line, 10, 64); path == "" || err != nil || n < 1 {
132 s.mrDiffRedirect(w, r, "pick a line to comment on")
133 return
134 }
135 extra = []string{"--path", path, "--line", line}
136 if r.FormValue("side") == "old" {
137 extra = append(extra, "--old")
138 }
139 }
140 // "Add to review" holds the comment back until the verdict; "Comment"
141 // posts it now, which is what the form did before there was a choice.
142 if r.FormValue("pending") == "on" {
143 extra = append(extra, "--pending")
144 }
145 msg, code := s.runControlStdinCode(u, mrArgs(r, "diff-comment", append(extra, "--file", "-")...), body)
146 s.done(w, r, code, msg, s.mrDiffRedirect)
147}
148
149// mrRetargetSubmit moves the merge request onto another branch.
150func (s *Server) mrRetargetSubmit(w http.ResponseWriter, r *http.Request, u store.User) {
151 target := strings.TrimSpace(r.FormValue("target"))
152 if target == "" {
153 s.mrRedirect(w, r, "pick a branch to retarget onto")
154 return
155 }
156 _, msg, code := s.runControlCode(u, mrArgs(r, "retarget", target))
157 s.done(w, r, code, msg, s.mrRedirect)
158}
159
160// mrThreadSubmit resolves or reopens one review thread.
161func (s *Server) mrThreadSubmit(w http.ResponseWriter, r *http.Request, u store.User) {
162 verb := "resolve"
163 if r.FormValue("action") == "unresolve" {
164 verb = "unresolve"
165 }
166 id := strings.TrimSpace(r.FormValue("thread"))
167 if _, err := strconv.ParseInt(id, 10, 64); err != nil {
168 s.mrRedirect(w, r, "bad thread id")
169 return
170 }
171 _, msg, code := s.runControlCode(u, mrArgs(r, verb, id))
172 s.done(w, r, code, msg, s.mrRedirect)
173}
174
175// mrNewPage is the create form: branches to choose from, plus whatever
176// the last attempt had in it so a refusal does not lose the draft.
177type mrNewPage struct {
178 repoPage
179 Branches []gitutil.Ref
180 Sources []string
181 Source string
182 Target string
183 Title string
184 Body string
185 Format string
186 Notice string
187}
188
189// mrSources lists the branches a merge request may be opened from, in the
190// form the command takes: this repository's branches by name, and those of
191// any fork of it the viewer can push to as "owner/name:branch".
192// Write is the filter because a contributor proposes from a fork they
193// own; the command still checks the source for itself (#168).
194func (s *Server) mrSources(u store.User, p repoPage) []string {
195 var out []string
196 branches, _ := gitutil.Refs(p.Dir, "heads")
197 for _, b := range branches {
198 out = append(out, b.Name)
199 }
200 if u.ID == 0 {
201 return out
202 }
203 forks, _ := s.st.ListForks(p.Repo.ID)
204 for _, f := range forks {
205 grant, _ := s.st.AccessRole(f.ID, u.ID)
206 if !policy.CanWrite(u, f, grant) {
207 continue
208 }
209 dir := control.RepoDir(s.cfg.Server.Root, f.OwnerName, f.Name)
210 refs, _ := gitutil.Refs(dir, "heads")
211 for _, b := range refs {
212 out = append(out, f.Path()+":"+b.Name)
213 }
214 }
215 return out
216}
217
218func (s *Server) mrCreateForm(w http.ResponseWriter, r *http.Request, u store.User) {
219 p, ok := s.repoFor(w, r, "")
220 if !ok {
221 return
222 }
223 p.Tab = "merge requests"
224 branches, _ := gitutil.Refs(p.Dir, "heads")
225 q := r.URL.Query()
226 target := q.Get("target")
227 if target == "" {
228 target = p.Repo.DefaultBranch
229 }
230 format := q.Get("format")
231 if format != "org" {
232 format = "md"
233 }
234 s.render(w, "mrnew.html", mrNewPage{
235 repoPage: p, Branches: branches, Sources: s.mrSources(u, p),
236 Source: q.Get("source"), Target: target,
237 Title: q.Get("title"), Body: q.Get("body"), Format: format, Notice: s.takeFlash(w, r),
238 })
239}
240
241func (s *Server) mrCreateSubmit(w http.ResponseWriter, r *http.Request, u store.User) {
242 p, ok := s.repoFor(w, r, "")
243 if !ok {
244 return
245 }
246 source := strings.TrimSpace(r.FormValue("source"))
247 target := strings.TrimSpace(r.FormValue("target"))
248 title := strings.TrimSpace(r.FormValue("title"))
249 body := strings.TrimSpace(r.FormValue("body"))
250 format := r.FormValue("format")
251 if format != "org" {
252 format = "md"
253 }
254
255 back := func(msg string) {
256 q := url.Values{"source": {source}, "target": {target}, "title": {title}, "body": {body}, "format": {format}}
257 s.setFlash(w, msg)
258 http.Redirect(w, r, fmt.Sprintf("/%s/mrs/new?%s", p.Repo.Path(), q.Encode()), http.StatusSeeOther)
259 }
260 if source == "" || title == "" {
261 back("pick a source branch and give the merge request a title")
262 return
263 }
264 argv := []string{"mr", "create", p.Repo.Path(), "--source", source, "--title", title, "--format", format}
265 if target != "" {
266 argv = append(argv, "--target", target)
267 }
268 if body != "" {
269 argv = append(argv, "--body", body)
270 }
271 var created control.MRCreated
272 if msg, ok := s.runControlInto(u, argv, &created); !ok {
273 back(msg)
274 return
275 }
276 http.Redirect(w, r, fmt.Sprintf("/%s/mrs/%d", p.Repo.Path(), created.Number), http.StatusSeeOther)
277}