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/set_title.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/set_title.go')
-rw-r--r-- | operations/set_title.go | 40 |
1 files changed, 39 insertions, 1 deletions
diff --git a/operations/set_title.go b/operations/set_title.go index 7aa76268..46addce6 100644 --- a/operations/set_title.go +++ b/operations/set_title.go @@ -1,7 +1,11 @@ package operations import ( + "fmt" + "strings" + "github.com/MichaelMure/git-bug/bug" + "github.com/MichaelMure/git-bug/util/text" ) // SetTitleOperation will change the title of a bug @@ -20,6 +24,34 @@ func (op SetTitleOperation) Apply(snapshot bug.Snapshot) bug.Snapshot { return snapshot } +func (op SetTitleOperation) Validate() error { + if err := bug.OpBaseValidate(op, bug.SetTitleOp); err != nil { + return err + } + + if text.Empty(op.Title) { + return fmt.Errorf("title is empty") + } + + if strings.Contains(op.Title, "\n") { + return fmt.Errorf("title should be a single line") + } + + if !text.Safe(op.Title) { + return fmt.Errorf("title should be fully printable") + } + + if strings.Contains(op.Was, "\n") { + return fmt.Errorf("previous title should be a single line") + } + + if !text.Safe(op.Was) { + return fmt.Errorf("previous title should be fully printable") + } + + return nil +} + func NewSetTitleOp(author bug.Person, title string, was string) SetTitleOperation { return SetTitleOperation{ OpBase: bug.NewOpBase(bug.SetTitleOp, author), @@ -29,7 +61,7 @@ func NewSetTitleOp(author bug.Person, title string, was string) SetTitleOperatio } // Convenience function to apply the operation -func SetTitle(b bug.Interface, author bug.Person, title string) { +func SetTitle(b bug.Interface, author bug.Person, title string) error { it := bug.NewOperationIterator(b) var lastTitleOp bug.Operation @@ -48,5 +80,11 @@ func SetTitle(b bug.Interface, author bug.Person, title string) { } setTitleOp := NewSetTitleOp(author, title, was) + + if err := setTitleOp.Validate(); err != nil { + return err + } + b.Append(setTitleOp) + return nil } |