aboutsummaryrefslogtreecommitdiffstats
path: root/operations/label_change.go
diff options
context:
space:
mode:
authorMichael Muré <batolettre@gmail.com>2018-09-15 13:15:00 +0200
committerMichael Muré <batolettre@gmail.com>2018-09-15 13:15:00 +0200
commit7bec0b1f134d213e7505fc2ac03ffea26f2193cc (patch)
treee263cccd84406843eacbc6bd184acdacb25a49d1 /operations/label_change.go
parentb478cd1bcb4756b20f7f4b15fcf81f23e1a60a02 (diff)
downloadgit-bug-7bec0b1f134d213e7505fc2ac03ffea26f2193cc.tar.gz
bug: add a data validation process to avoid merging incorrect operations
Diffstat (limited to 'operations/label_change.go')
-rw-r--r--operations/label_change.go29
1 files changed, 29 insertions, 0 deletions
diff --git a/operations/label_change.go b/operations/label_change.go
index a3cc9898..507651df 100644
--- a/operations/label_change.go
+++ b/operations/label_change.go
@@ -5,6 +5,7 @@ import (
"sort"
"github.com/MichaelMure/git-bug/bug"
+ "github.com/pkg/errors"
)
var _ bug.Operation = LabelChangeOperation{}
@@ -49,6 +50,30 @@ AddLoop:
return snapshot
}
+func (op LabelChangeOperation) Validate() error {
+ if err := bug.OpBaseValidate(op, bug.LabelChangeOp); err != nil {
+ return err
+ }
+
+ for _, l := range op.Added {
+ if err := l.Validate(); err != nil {
+ return errors.Wrap(err, "added label")
+ }
+ }
+
+ for _, l := range op.Removed {
+ if err := l.Validate(); err != nil {
+ return errors.Wrap(err, "removed label")
+ }
+ }
+
+ if len(op.Added)+len(op.Removed) <= 0 {
+ return fmt.Errorf("no label change")
+ }
+
+ return nil
+}
+
func NewLabelChangeOperation(author bug.Person, added, removed []bug.Label) LabelChangeOperation {
return LabelChangeOperation{
OpBase: bug.NewOpBase(bug.LabelChangeOp, author),
@@ -108,6 +133,10 @@ func ChangeLabels(b bug.Interface, author bug.Person, add, remove []string) ([]L
labelOp := NewLabelChangeOperation(author, added, removed)
+ if err := labelOp.Validate(); err != nil {
+ return nil, err
+ }
+
b.Append(labelOp)
return results, nil