internal/httpd/mractions.go
330 lines · 11219 bytes
18 symbols in this file
Server.mrRedirectServer.mrDiffRedirectmrArgsServer.mrReviewSubmitServer.mrReviewRequestSubmitServer.mrLabelSubmitServer.mrMergeSubmitServer.mrCloseSubmitServer.mrDraftSubmitServer.mrDiffCommentSubmitServer.mrRetargetSubmitServer.mrThreadSubmitServer.mrSuggestionSubmitmrNewPageServer.writableForksServer.mrSourcesServer.mrCreateFormServer.mrCreateSubmit
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 verdict := r.FormValue("verdict")
46 flag := map[string]string{
47 "approve": "--approve",
48 "request-changes": "--request-changes",
49 "comment": "--comment",
50 "discard": "--discard",
51 }[verdict]
52 if flag == "" {
53 s.mrRedirect(w, r, "pick approve, request changes, comment, or discard")
54 return
55 }
56 if body := strings.TrimSpace(r.FormValue("body")); body != "" && verdict != "discard" {
57 if msg, code := s.runControlStdinCode(u, mrArgs(r, "comment", "--file", "-"), body); code != 0 {
58 s.done(w, r, code, msg, s.mrRedirect)
59 return
60 }
61 }
62 _, msg, code := s.runControlCode(u, mrArgs(r, "review", flag))
63 s.done(w, r, code, msg, s.mrRedirect)
64}
65
66func (s *Server) mrReviewRequestSubmit(w http.ResponseWriter, r *http.Request, u store.User) {
67 args := append(fieldArgs("--add", r.FormValue("add")), fieldArgs("--remove", r.FormValue("remove"))...)
68 if len(args) == 0 {
69 s.mrRedirect(w, r, "name at least one person")
70 return
71 }
72 repo := r.PathValue("owner") + "/" + r.PathValue("repo")
73 _, msg, code := s.runControlCode(u, append([]string{"mr", "review", "request", repo, r.PathValue("n")}, args...))
74 s.done(w, r, code, msg, s.mrRedirect)
75}
76
77func (s *Server) mrLabelSubmit(w http.ResponseWriter, r *http.Request, u store.User) {
78 args := append(fieldArgs("--add", r.FormValue("add")), fieldArgs("--remove", r.FormValue("remove"))...)
79 if len(args) == 0 {
80 s.mrRedirect(w, r, "name at least one label")
81 return
82 }
83 _, msg, code := s.runControlCode(u, mrArgs(r, "label", args...))
84 s.done(w, r, code, msg, s.mrRedirect)
85}
86
87func (s *Server) mrMergeSubmit(w http.ResponseWriter, r *http.Request, u store.User) {
88 args := []string{}
89 if r.FormValue("cancel") == "on" {
90 args = append(args, "--cancel")
91 } else {
92 if st := strings.TrimSpace(r.FormValue("strategy")); st != "" && st != "auto" {
93 args = append(args, "--strategy", st)
94 }
95 if r.FormValue("when_ready") == "on" {
96 args = append(args, "--when-ready")
97 }
98 }
99 _, msg, code := s.runControlCode(u, mrArgs(r, "merge", args...))
100 s.done(w, r, code, msg, s.mrRedirect)
101}
102
103func (s *Server) mrCloseSubmit(w http.ResponseWriter, r *http.Request, u store.User) {
104 args := []string{}
105 if by := strings.TrimPrefix(strings.TrimSpace(r.FormValue("by")), "!"); by != "" {
106 if _, err := strconv.ParseInt(by, 10, 64); err != nil {
107 s.mrRedirect(w, r, "the superseding request is a number")
108 return
109 }
110 args = append(args, "--by", by)
111 }
112 _, msg, code := s.runControlCode(u, mrArgs(r, "close", args...))
113 s.done(w, r, code, msg, s.mrRedirect)
114}
115
116// mrDraftSubmit toggles the draft mark. The form says which way it is
117// going, so a stale page cannot flip the wrong one.
118func (s *Server) mrDraftSubmit(w http.ResponseWriter, r *http.Request, u store.User) {
119 verb := "ready"
120 if r.FormValue("draft") == "on" {
121 verb = "draft"
122 }
123 _, msg, code := s.runControlCode(u, mrArgs(r, verb))
124 s.done(w, r, code, msg, s.mrRedirect)
125}
126
127// mrDiffCommentSubmit opens a review thread on a diff line, or replies to
128// one. The body goes in on stdin: it is user prose, and argv is visible in
129// /proc.
130func (s *Server) mrDiffCommentSubmit(w http.ResponseWriter, r *http.Request, u store.User) {
131 body := strings.TrimSpace(r.FormValue("body"))
132 if body == "" {
133 s.mrDiffRedirect(w, r, "empty comment")
134 return
135 }
136 var extra []string
137 if reply := strings.TrimSpace(r.FormValue("reply")); reply != "" {
138 if _, err := strconv.ParseInt(reply, 10, 64); err != nil {
139 s.mrDiffRedirect(w, r, "bad thread id")
140 return
141 }
142 extra = []string{"--reply", reply}
143 } else {
144 path := strings.TrimSpace(r.FormValue("path"))
145 line := strings.TrimSpace(r.FormValue("line"))
146 if n, err := strconv.ParseInt(line, 10, 64); path == "" || err != nil || n < 1 {
147 s.mrDiffRedirect(w, r, "pick a line to comment on")
148 return
149 }
150 extra = []string{"--path", path, "--line", line}
151 if start := strings.TrimSpace(r.FormValue("start_line")); start != "" {
152 if n, err := strconv.ParseInt(start, 10, 64); err != nil || n < 1 {
153 s.mrDiffRedirect(w, r, "the first line is a line number")
154 return
155 }
156 extra = append(extra, "--start-line", start)
157 }
158 if r.FormValue("side") == "old" {
159 extra = append(extra, "--old")
160 }
161 }
162 // "Add to review" holds the comment back until the verdict; "Comment"
163 // posts it now, which is what the form did before there was a choice.
164 if r.FormValue("pending") == "on" {
165 extra = append(extra, "--pending")
166 }
167 msg, code := s.runControlStdinCode(u, mrArgs(r, "diff-comment", append(extra, "--file", "-")...), body)
168 s.done(w, r, code, msg, s.mrDiffRedirect)
169}
170
171// mrRetargetSubmit moves the merge request onto another branch.
172func (s *Server) mrRetargetSubmit(w http.ResponseWriter, r *http.Request, u store.User) {
173 target := strings.TrimSpace(r.FormValue("target"))
174 if target == "" {
175 s.mrRedirect(w, r, "pick a branch to retarget onto")
176 return
177 }
178 _, msg, code := s.runControlCode(u, mrArgs(r, "retarget", target))
179 s.done(w, r, code, msg, s.mrRedirect)
180}
181
182// mrThreadSubmit resolves or reopens one review thread.
183func (s *Server) mrThreadSubmit(w http.ResponseWriter, r *http.Request, u store.User) {
184 verb := "resolve"
185 if r.FormValue("action") == "unresolve" {
186 verb = "unresolve"
187 }
188 id := strings.TrimSpace(r.FormValue("thread"))
189 if _, err := strconv.ParseInt(id, 10, 64); err != nil {
190 s.mrRedirect(w, r, "bad thread id")
191 return
192 }
193 _, msg, code := s.runControlCode(u, mrArgs(r, verb, id))
194 s.done(w, r, code, msg, s.mrRedirect)
195}
196
197// mrSuggestionSubmit commits a thread's suggestion to the source branch.
198func (s *Server) mrSuggestionSubmit(w http.ResponseWriter, r *http.Request, u store.User) {
199 id := strings.TrimSpace(r.FormValue("thread"))
200 if _, err := strconv.ParseInt(id, 10, 64); err != nil {
201 s.mrDiffRedirect(w, r, "bad thread id")
202 return
203 }
204 _, msg, code := s.runControlCode(u, mrArgs(r, "apply-suggestion", id))
205 s.done(w, r, code, msg, s.mrDiffRedirect)
206}
207
208// mrNewPage is the create form: branches to choose from, plus whatever
209// the last attempt had in it so a refusal does not lose the draft.
210type mrNewPage struct {
211 repoPage
212 Branches []gitutil.Ref
213 Sources []string
214 Source string
215 Target string
216 Title string
217 Body string
218 Format string
219 Notice string
220 Draft *draft
221}
222
223// writableForks lists the forks of repo that u can push to — the source
224// half of what a merge request may be opened from, alongside the
225// repository's own branches. Write is the filter because a contributor
226// proposes from a fork they own; the command still checks the source for
227// itself (#168).
228func (s *Server) writableForks(u store.User, repo store.Repo) []store.Repo {
229 if u.ID == 0 {
230 return nil
231 }
232 forks, _ := s.st.ListForks(repo.ID)
233 var out []store.Repo
234 for _, f := range forks {
235 grant, _ := s.st.AccessRole(f.ID, u.ID)
236 if policy.CanWrite(u, f, grant) {
237 out = append(out, f)
238 }
239 }
240 return out
241}
242
243// mrSources lists the branches a merge request may be opened from, in the
244// form the command takes: this repository's branches by name, and those of
245// any writable fork as "owner/name:branch".
246func (s *Server) mrSources(u store.User, p repoPage) []string {
247 var out []string
248 branches, _ := gitutil.Refs(p.Dir, "heads")
249 for _, b := range branches {
250 out = append(out, b.Name)
251 }
252 for _, f := range s.writableForks(u, p.Repo) {
253 dir := control.RepoDir(s.cfg.Server.Root, f.OwnerName, f.Name)
254 refs, _ := gitutil.Refs(dir, "heads")
255 for _, b := range refs {
256 out = append(out, f.Path()+":"+b.Name)
257 }
258 }
259 return out
260}
261
262func (s *Server) mrCreateForm(w http.ResponseWriter, r *http.Request, u store.User) {
263 p, ok := s.repoFor(w, r, "")
264 if !ok {
265 return
266 }
267 p.Tab = "merge requests"
268 branches, _ := gitutil.Refs(p.Dir, "heads")
269 q := r.URL.Query()
270 target := q.Get("target")
271 if target == "" {
272 target = p.Repo.DefaultBranch
273 }
274 format := q.Get("format")
275 if format != "org" {
276 format = "md"
277 }
278 s.render(w, "mrnew.html", mrNewPage{
279 repoPage: p, Branches: branches, Sources: s.mrSources(u, p),
280 Source: q.Get("source"), Target: target,
281 Title: q.Get("title"), Body: q.Get("body"), Format: format, Notice: s.takeFlash(w, r),
282 })
283}
284
285func (s *Server) mrCreateSubmit(w http.ResponseWriter, r *http.Request, u store.User) {
286 p, ok := s.repoFor(w, r, "")
287 if !ok {
288 return
289 }
290 source := strings.TrimSpace(r.FormValue("source"))
291 target := strings.TrimSpace(r.FormValue("target"))
292 title := strings.TrimSpace(r.FormValue("title"))
293 body := strings.TrimSpace(r.FormValue("body"))
294 format := bodyFormat(r)
295
296 // Preview: the same page back with the draft intact, nothing opened.
297 if wantsPreview(r) {
298 p.Tab = "merge requests"
299 branches, _ := gitutil.Refs(p.Dir, "heads")
300 s.render(w, "mrnew.html", mrNewPage{
301 repoPage: p, Branches: branches, Sources: s.mrSources(u, p),
302 Source: source, Target: target, Title: title, Body: body, Format: format,
303 Draft: s.draftFor(r, p.Repo, "body", "body", format),
304 })
305 return
306 }
307
308 back := func(msg string) {
309 q := url.Values{"source": {source}, "target": {target}, "title": {title}, "body": {body}, "format": {format}}
310 s.setFlash(w, msg)
311 http.Redirect(w, r, fmt.Sprintf("/%s/mrs/new?%s", p.Repo.Path(), q.Encode()), http.StatusSeeOther)
312 }
313 if source == "" || title == "" {
314 back("pick a source branch and give the merge request a title")
315 return
316 }
317 argv := []string{"mr", "create", p.Repo.Path(), "--source", source, "--title", title, "--format", format}
318 if target != "" {
319 argv = append(argv, "--target", target)
320 }
321 if body != "" {
322 argv = append(argv, "--body", body)
323 }
324 var created control.MRCreated
325 if msg, ok := s.runControlInto(u, argv, &created); !ok {
326 back(msg)
327 return
328 }
329 http.Redirect(w, r, fmt.Sprintf("/%s/mrs/%d", p.Repo.Path(), created.Number), http.StatusSeeOther)
330}