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