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