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