internal/httpd/labels.go

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

68 lines · 1949 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	anyColor := false
32	for _, l := range labels {
33		if l.Color != "" {
34			anyColor = true
35			break
36		}
37	}
38	s.render(w, "labels.html", struct {
39		repoPage
40		Labels      []store.Label
41		LabelColors map[string]template.CSS
42		CanWrite    bool
43		AnyColor    bool
44		Notice      string
45	}{p, labels, s.labelColors(p.Repo), s.canWriteRepo(r, p.Repo), anyColor, s.takeFlash(w, r)})
46}
47
48// labelSubmit creates a label, sets its colour, or removes it, through
49// the label commands the CLI runs.
50func (s *Server) labelSubmit(w http.ResponseWriter, r *http.Request, u store.User) {
51	repo := r.PathValue("owner") + "/" + r.PathValue("repo")
52	name := strings.TrimSpace(r.FormValue("name"))
53	back := func(w http.ResponseWriter, r *http.Request, msg string) { s.backTo(w, r, "labels", msg) }
54	if name == "" {
55		back(w, r, "name the label")
56		return
57	}
58	argv := []string{"label", "set", repo, name, "--color", strings.TrimSpace(r.FormValue("color"))}
59	if r.FormValue("action") == "remove" {
60		if ok, msg := confirmed(r, name); !ok {
61			s.backTo(w, r, "labels", msg)
62			return
63		}
64		argv = []string{"label", "remove", repo, name}
65	}
66	_, msg, code := s.runControlCode(u, argv)
67	s.done(w, r, code, msg, back)
68}