internal/httpd/labels.go

e2dec5d9ff2cd5dd54f68adec4190d8bafeaf302
gitbay/internal/httpd/labels.go history · blame · raw

60 lines · 1831 bytes

 1package httpd
 2
 3import (
 4	"html/template"
 5	"net/http"
 6	"strings"
 7
 8	"gitbay.org/gitbay/internal/control"
 9	"gitbay.org/gitbay/internal/store"
10)
11
12// labels lists a repository's label set with its colour and how many
13// issues carry it. Applying a label is on the issue page; this is where
14// the set itself is kept (#163).
15func (s *Server) labels(w http.ResponseWriter, r *http.Request) {
16	p, ok := s.repoFor(w, r, "")
17	if !ok {
18		return
19	}
20	p.Tab = "issues"
21	readable, err := control.ReadableScope(s.st, s.viewer(r), p.Repo)
22	if err != nil {
23		http.Error(w, "internal error", http.StatusInternalServerError)
24		return
25	}
26	labels, err := s.st.ListLabels(p.Repo, readable)
27	if err != nil {
28		http.Error(w, "internal error", http.StatusInternalServerError)
29		return
30	}
31	s.render(w, "labels.html", struct {
32		repoPage
33		Labels      []store.Label
34		LabelColors map[string]template.CSS
35		CanWrite    bool
36		Notice      string
37	}{p, labels, s.labelColors(p.Repo), s.canWriteRepo(r, p.Repo), s.takeFlash(w, r)})
38}
39
40// labelSubmit creates a label, sets its colour, or removes it, through
41// the label commands the CLI runs.
42func (s *Server) labelSubmit(w http.ResponseWriter, r *http.Request, u store.User) {
43	repo := r.PathValue("owner") + "/" + r.PathValue("repo")
44	name := strings.TrimSpace(r.FormValue("name"))
45	back := func(w http.ResponseWriter, r *http.Request, msg string) { s.backTo(w, r, "labels", msg) }
46	if name == "" {
47		s.backTo(w, r, "labels", "name the label")
48		return
49	}
50	argv := []string{"label", "set", repo, name, "--color", strings.TrimSpace(r.FormValue("color"))}
51	if r.FormValue("action") == "remove" {
52		if ok, msg := confirmed(r, name); !ok {
53			s.backTo(w, r, "labels", msg)
54			return
55		}
56		argv = []string{"label", "remove", repo, name}
57	}
58	_, msg, code := s.runControlCode(u, argv)
59	s.done(w, r, code, msg, back)
60}