internal/control/label.go
128 lines · 3780 bytes
1package control
2
3import (
4 "errors"
5 "fmt"
6 "io"
7 "regexp"
8 "strings"
9
10 "gitbay.org/gitbay/internal/policy"
11 "gitbay.org/gitbay/internal/protocol"
12 "gitbay.org/gitbay/internal/store"
13)
14
15func init() {
16 register(Command{Path: []string{"label", "list"},
17 Summary: "list a repository's issue labels with colour and use",
18 Usage: "label list <owner/name>", ReadOnly: true, Run: runLabelList})
19 register(Command{Path: []string{"label", "set"},
20 Summary: "create a label or set its colour",
21 Usage: "label set <owner/name> <label> [--color rrggbb|'']", Run: runLabelSet})
22 register(Command{Path: []string{"label", "remove"},
23 Summary: "remove a label from the repository and from every issue",
24 Usage: "label remove <owner/name> <label>", Run: runLabelRemove})
25}
26
27// A colour is six hex digits, with or without the hash: over bare ssh a
28// bare # starts a comment for the remote tokenizer, so the form without
29// it is the one that types cleanly there.
30var labelColorPat = regexp.MustCompile(`^#?[0-9a-fA-F]{6}$`)
31
32func runLabelList(c *Ctx, args []string) int {
33 if len(args) != 1 {
34 return c.fail(protocol.ExitUsage, "usage: label list <owner/name>")
35 }
36 repo, code := resolveRepo(c, args[0], policy.CanRead)
37 if code >= 0 {
38 return code
39 }
40 labels, err := c.Store.ListLabels(repo.ID)
41 if err != nil {
42 return c.fail(protocol.ExitFailure, "%v", err)
43 }
44 return c.emit(labels, func(w io.Writer) {
45 for _, l := range labels {
46 fmt.Fprintf(w, "%s\t%s\t%d\n", l.Name, l.Color, l.Issues)
47 }
48 })
49}
50
51func runLabelSet(c *Ctx, args []string) int {
52 const usage = "usage: label set <owner/name> <label> [--color #rrggbb|'']"
53 var rest []string
54 color, colorSet := "", false
55 for i := 0; i < len(args); i++ {
56 if args[i] == "--color" {
57 if i+1 >= len(args) {
58 return c.fail(protocol.ExitUsage, "--color requires rrggbb, or '' to clear")
59 }
60 color, colorSet = strings.ToLower(args[i+1]), true
61 i++
62 continue
63 }
64 rest = append(rest, args[i])
65 }
66 if len(rest) != 2 {
67 return c.fail(protocol.ExitUsage, usage)
68 }
69 if colorSet && color != "" {
70 if !labelColorPat.MatchString(color) {
71 return c.fail(protocol.ExitUsage, "--color takes rrggbb (with or without #), or '' to clear")
72 }
73 color = "#" + strings.TrimPrefix(color, "#")
74 }
75 repo, code := resolveRepo(c, rest[0], policy.CanWrite)
76 if code >= 0 {
77 return code
78 }
79 if code := refuseArchived(c, repo); code >= 0 {
80 return code
81 }
82 name := rest[1]
83 if name == "" || len(name) > 50 {
84 return c.fail(protocol.ExitUsage, "a label is 1 to 50 characters")
85 }
86 if !colorSet {
87 // Keep the colour it has, if any; this is "make sure it exists".
88 if labels, err := c.Store.ListLabels(repo.ID); err == nil {
89 for _, l := range labels {
90 if l.Name == name {
91 color = l.Color
92 }
93 }
94 }
95 }
96 if err := c.Store.SetLabel(repo.ID, name, color); err != nil {
97 return c.fail(protocol.ExitFailure, "%v", err)
98 }
99 return c.emit(store.Label{Name: name, Color: color}, func(w io.Writer) {
100 if color == "" {
101 fmt.Fprintf(w, "label %s, no colour set\n", name)
102 return
103 }
104 fmt.Fprintf(w, "label %s is %s\n", name, color)
105 })
106}
107
108func runLabelRemove(c *Ctx, args []string) int {
109 if len(args) != 2 {
110 return c.fail(protocol.ExitUsage, "usage: label remove <owner/name> <label>")
111 }
112 repo, code := resolveRepo(c, args[0], policy.CanWrite)
113 if code >= 0 {
114 return code
115 }
116 if code := refuseArchived(c, repo); code >= 0 {
117 return code
118 }
119 if err := c.Store.DeleteLabel(repo.ID, args[1]); err != nil {
120 if errors.Is(err, store.ErrNotFound) {
121 return c.fail(protocol.ExitNotFound, "no label %q in %s", args[1], repo.Path())
122 }
123 return c.fail(protocol.ExitFailure, "%v", err)
124 }
125 return c.emit(map[string]string{"removed": args[1]}, func(w io.Writer) {
126 fmt.Fprintf(w, "removed label %s\n", args[1])
127 })
128}