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