internal/control/label.go
121 lines · 3696 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 f, err := parseFlags(args, flagSpec{Values: []string{"--color"}, MaxPos: -1, Usage: usage})
54 if err != nil {
55 return c.fail(protocol.ExitUsage, "%v", err)
56 }
57 rest := f.Pos
58 color, colorSet := strings.ToLower(f.Value("--color")), f.Has("--color")
59 if len(rest) != 2 {
60 return c.fail(protocol.ExitUsage, usage)
61 }
62 if colorSet && color != "" {
63 if !labelColorPat.MatchString(color) {
64 return c.fail(protocol.ExitUsage, "--color takes rrggbb (with or without #), or '' to clear")
65 }
66 color = "#" + strings.TrimPrefix(color, "#")
67 }
68 repo, code := resolveRepo(c, rest[0], policy.CanWrite)
69 if code >= 0 {
70 return code
71 }
72 if code := refuseArchived(c, repo); code >= 0 {
73 return code
74 }
75 name := rest[1]
76 if name == "" || len(name) > 50 {
77 return c.fail(protocol.ExitUsage, "a label is 1 to 50 characters")
78 }
79 if !colorSet {
80 // Keep the colour it has, if any; this is "make sure it exists".
81 if labels, err := c.Store.ListLabels(repo.ID); err == nil {
82 for _, l := range labels {
83 if l.Name == name {
84 color = l.Color
85 }
86 }
87 }
88 }
89 if err := c.Store.SetLabel(repo.ID, name, color); err != nil {
90 return c.fail(protocol.ExitFailure, "%v", err)
91 }
92 return c.emit(store.Label{Name: name, Color: color}, func(w io.Writer) {
93 if color == "" {
94 fmt.Fprintf(w, "label %s, no colour set\n", name)
95 return
96 }
97 fmt.Fprintf(w, "label %s is %s\n", name, color)
98 })
99}
100
101func runLabelRemove(c *Ctx, args []string) int {
102 if len(args) != 2 {
103 return c.fail(protocol.ExitUsage, "usage: label remove <owner/name> <label>")
104 }
105 repo, code := resolveRepo(c, args[0], policy.CanWrite)
106 if code >= 0 {
107 return code
108 }
109 if code := refuseArchived(c, repo); code >= 0 {
110 return code
111 }
112 if err := c.Store.DeleteLabel(repo.ID, args[1]); err != nil {
113 if errors.Is(err, store.ErrNotFound) {
114 return c.fail(protocol.ExitNotFound, "no label %q in %s", args[1], repo.Path())
115 }
116 return c.fail(protocol.ExitFailure, "%v", err)
117 }
118 return c.emit(map[string]string{"removed": args[1]}, func(w io.Writer) {
119 fmt.Fprintf(w, "removed label %s\n", args[1])
120 })
121}