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/create.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/create.go')
-rw-r--r-- | operations/create.go | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/operations/create.go b/operations/create.go index a671171e..efd4492f 100644 --- a/operations/create.go +++ b/operations/create.go @@ -1,8 +1,12 @@ package operations import ( + "fmt" + "strings" + "github.com/MichaelMure/git-bug/bug" "github.com/MichaelMure/git-bug/util/git" + "github.com/MichaelMure/git-bug/util/text" ) // CreateOperation define the initial creation of a bug @@ -34,6 +38,30 @@ func (op CreateOperation) GetFiles() []git.Hash { return op.Files } +func (op CreateOperation) Validate() error { + if err := bug.OpBaseValidate(op, bug.CreateOp); 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 is not fully printable") + } + + if !text.Safe(op.Message) { + return fmt.Errorf("message is not fully printable") + } + + return nil +} + func NewCreateOp(author bug.Person, title, message string, files []git.Hash) CreateOperation { return CreateOperation{ OpBase: bug.NewOpBase(bug.CreateOp, author), @@ -51,6 +79,11 @@ func Create(author bug.Person, title, message string) (*bug.Bug, error) { func CreateWithFiles(author bug.Person, title, message string, files []git.Hash) (*bug.Bug, error) { newBug := bug.NewBug() createOp := NewCreateOp(author, title, message, files) + + if err := createOp.Validate(); err != nil { + return nil, err + } + newBug.Append(createOp) return newBug, nil |