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