internal/httpd/issueactions.go
80 lines · 2560 bytes
1package httpd
2
3import (
4 "fmt"
5 "net/http"
6 "net/url"
7 "strings"
8
9 "gitbay.org/gitbay/internal/store"
10)
11
12// Issue actions. Like the merge request controls, each one runs the
13// command the CLI runs, so label rules, access checks, and the system
14// comments they leave behind have a single implementation.
15
16func (s *Server) issueRedirect(w http.ResponseWriter, r *http.Request, msg string) {
17 dest := fmt.Sprintf("/%s/%s/issues/%s",
18 r.PathValue("owner"), r.PathValue("repo"), r.PathValue("n"))
19 if msg != "" {
20 if len(msg) > 300 {
21 msg = msg[:300]
22 }
23 dest += "?e=" + url.QueryEscape(msg)
24 }
25 http.Redirect(w, r, dest, http.StatusSeeOther)
26}
27
28func issueArgs(r *http.Request, verb string, extra ...string) []string {
29 repo := r.PathValue("owner") + "/" + r.PathValue("repo")
30 return append([]string{"issue", verb, repo, r.PathValue("n")}, extra...)
31}
32
33// issueStateSubmit closes or reopens, following the button pressed.
34func (s *Server) issueStateSubmit(w http.ResponseWriter, r *http.Request, u store.User) {
35 verb := "close"
36 if r.FormValue("action") == "reopen" {
37 verb = "reopen"
38 }
39 _, msg, code := s.runControlCode(u, issueArgs(r, verb))
40 s.done(w, r, code, msg, s.issueRedirect)
41}
42
43// fieldArgs turns a space-separated form value into repeated flags, the
44// shape the label and assign commands take.
45func fieldArgs(flag, values string) []string {
46 var out []string
47 for _, v := range strings.Fields(values) {
48 out = append(out, flag, v)
49 }
50 return out
51}
52
53func (s *Server) issueLabelSubmit(w http.ResponseWriter, r *http.Request, u store.User) {
54 args := append(fieldArgs("--add", r.FormValue("add")), fieldArgs("--remove", r.FormValue("remove"))...)
55 if len(args) == 0 {
56 s.issueRedirect(w, r, "name at least one label")
57 return
58 }
59 _, msg, code := s.runControlCode(u, issueArgs(r, "label", args...))
60 s.done(w, r, code, msg, s.issueRedirect)
61}
62
63func (s *Server) issueAssignSubmit(w http.ResponseWriter, r *http.Request, u store.User) {
64 args := append(fieldArgs("--add", r.FormValue("add")), fieldArgs("--remove", r.FormValue("remove"))...)
65 if len(args) == 0 {
66 s.issueRedirect(w, r, "name at least one person")
67 return
68 }
69 _, msg, code := s.runControlCode(u, issueArgs(r, "assign", args...))
70 s.done(w, r, code, msg, s.issueRedirect)
71}
72
73func (s *Server) issueMilestoneSubmit(w http.ResponseWriter, r *http.Request, u store.User) {
74 title := strings.TrimSpace(r.FormValue("milestone"))
75 if title == "" {
76 title = "none"
77 }
78 _, msg, code := s.runControlCode(u, issueArgs(r, "milestone", title))
79 s.done(w, r, code, msg, s.issueRedirect)
80}