diff options
author | Michael Muré <batolettre@gmail.com> | 2018-09-15 13:15:00 +0200 |
---|---|---|
committer | Michael Muré <batolettre@gmail.com> | 2018-09-15 13:15:00 +0200 |
commit | 7bec0b1f134d213e7505fc2ac03ffea26f2193cc (patch) | |
tree | e263cccd84406843eacbc6bd184acdacb25a49d1 /operations/label_change.go | |
parent | b478cd1bcb4756b20f7f4b15fcf81f23e1a60a02 (diff) | |
download | git-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.go | 29 |
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 |