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