From 6a12373965aff9f80147f8b5bff6a5a104927365 Mon Sep 17 00:00:00 2001 From: Michael Muré Date: Wed, 25 Jul 2018 18:01:32 +0200 Subject: more refactoring to have reusable bug action across different UI --- bug/operations/label_change.go | 64 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) (limited to 'bug/operations/label_change.go') 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 +} -- cgit