internal/httpd/mractions.go

v1.26.0
gitbay/internal/httpd/mractions.go history · blame · raw

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