aboutsummaryrefslogtreecommitdiffstats
path: root/bug/operations/label_change.go
diff options
context:
space:
mode:
authorMichael Muré <batolettre@gmail.com>2018-07-25 18:01:32 +0200
committerMichael Muré <batolettre@gmail.com>2018-07-25 18:01:32 +0200
commit6a12373965aff9f80147f8b5bff6a5a104927365 (patch)
treeccc1e34fba059512acab39286a86f6b73c5ad318 /bug/operations/label_change.go
parent49c90eab26875cbf3094d9a546ad29b426e174a1 (diff)
downloadgit-bug-6a12373965aff9f80147f8b5bff6a5a104927365.tar.gz
more refactoring to have reusable bug action across different UI
Diffstat (limited to 'bug/operations/label_change.go')
-rw-r--r--bug/operations/label_change.go64
1 files changed, 64 insertions, 0 deletions
diff --git a/bug/operations/label_change.go b/bug/operations/label_change.go
index ca07f6f5..b9bf86c8 100644
--- a/bug/operations/label_change.go
+++ b/bug/operations/label_change.go
@@ -1,7 +1,9 @@
package operations
import (
+ "fmt"
"github.com/MichaelMure/git-bug/bug"
+ "io"
"sort"
)
@@ -54,3 +56,65 @@ AddLoop:
return snapshot
}
+
+func ChangeLabels(out io.Writer, b *bug.Bug, author bug.Person, add, remove []string) error {
+ var added, removed []bug.Label
+
+ snap := b.Compile()
+
+ for _, str := range add {
+ label := bug.Label(str)
+
+ // check for duplicate
+ if labelExist(added, label) {
+ fmt.Fprintf(out, "label \"%s\" is a duplicate\n", str)
+ continue
+ }
+
+ // check that the label doesn't already exist
+ if labelExist(snap.Labels, label) {
+ fmt.Fprintf(out, "label \"%s\" is already set on this bug\n", str)
+ continue
+ }
+
+ added = append(added, label)
+ }
+
+ for _, str := range remove {
+ label := bug.Label(str)
+
+ // check for duplicate
+ if labelExist(removed, label) {
+ fmt.Fprintf(out, "label \"%s\" is a duplicate\n", str)
+ continue
+ }
+
+ // check that the label actually exist
+ if !labelExist(snap.Labels, label) {
+ fmt.Fprintf(out, "label \"%s\" doesn't exist on this bug\n", str)
+ continue
+ }
+
+ removed = append(removed, label)
+ }
+
+ if len(added) == 0 && len(removed) == 0 {
+ return fmt.Errorf("no label added or removed")
+ }
+
+ labelOp := NewLabelChangeOperation(author, added, removed)
+
+ b.Append(labelOp)
+
+ return nil
+}
+
+func labelExist(labels []bug.Label, label bug.Label) bool {
+ for _, l := range labels {
+ if l == label {
+ return true
+ }
+ }
+
+ return false
+}