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/add_comment.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/add_comment.go')
-rw-r--r-- | operations/add_comment.go | 29 |
1 files changed, 26 insertions, 3 deletions
diff --git a/operations/add_comment.go b/operations/add_comment.go index 8c4b8b9c..01572eb7 100644 --- a/operations/add_comment.go +++ b/operations/add_comment.go @@ -1,8 +1,11 @@ package operations import ( + "fmt" + "github.com/MichaelMure/git-bug/bug" "github.com/MichaelMure/git-bug/util/git" + "github.com/MichaelMure/git-bug/util/text" ) // AddCommentOperation will add a new comment in the bug @@ -33,6 +36,22 @@ func (op AddCommentOperation) GetFiles() []git.Hash { return op.Files } +func (op AddCommentOperation) Validate() error { + if err := bug.OpBaseValidate(op, bug.AddCommentOp); err != nil { + return err + } + + if text.Empty(op.Message) { + return fmt.Errorf("message is empty") + } + + if !text.Safe(op.Message) { + return fmt.Errorf("message is not fully printable") + } + + return nil +} + func NewAddCommentOp(author bug.Person, message string, files []git.Hash) AddCommentOperation { return AddCommentOperation{ OpBase: bug.NewOpBase(bug.AddCommentOp, author), @@ -42,11 +61,15 @@ func NewAddCommentOp(author bug.Person, message string, files []git.Hash) AddCom } // Convenience function to apply the operation -func Comment(b bug.Interface, author bug.Person, message string) { - CommentWithFiles(b, author, message, nil) +func Comment(b bug.Interface, author bug.Person, message string) error { + return CommentWithFiles(b, author, message, nil) } -func CommentWithFiles(b bug.Interface, author bug.Person, message string, files []git.Hash) { +func CommentWithFiles(b bug.Interface, author bug.Person, message string, files []git.Hash) error { addCommentOp := NewAddCommentOp(author, message, files) + if err := addCommentOp.Validate(); err != nil { + return err + } b.Append(addCommentOp) + return nil } |