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