internal/httpd/mractions.go

4a511e42574ead9c92dc73fd320f296cc4dbea07
gitbay/internal/httpd/mractions.go

336 lines · 11484 bytes

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

Global

⌘K
Command palette & jump
/
Focus search
?
Show this help
g d
Dashboard
g e
Explore repositories
g n
Notifications

Repository & editor

t
Find file in repository
y
Copy immutable SHA permalink
g c
Code tab
g i
Issues tab
g m
Merge requests tab
g b
Builds tab
⌘↵
Submit active form