aboutsummaryrefslogtreecommitdiffstats
path: root/operations/create.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/create.go
parentb478cd1bcb4756b20f7f4b15fcf81f23e1a60a02 (diff)
downloadgit-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.go33
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