aboutsummaryrefslogtreecommitdiffstats
path: root/bug/operation.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 /bug/operation.go
parentb478cd1bcb4756b20f7f4b15fcf81f23e1a60a02 (diff)
downloadgit-bug-7bec0b1f134d213e7505fc2ac03ffea26f2193cc.tar.gz
bug: add a data validation process to avoid merging incorrect operations
Diffstat (limited to 'bug/operation.go')
-rw-r--r--bug/operation.go42
1 files changed, 37 insertions, 5 deletions
diff --git a/bug/operation.go b/bug/operation.go
index 6a8aa0cd..b148141c 100644
--- a/bug/operation.go
+++ b/bug/operation.go
@@ -2,6 +2,9 @@ package bug
import (
"github.com/MichaelMure/git-bug/util/git"
+ "github.com/pkg/errors"
+
+ "fmt"
"time"
)
@@ -25,13 +28,14 @@ type Operation interface {
Time() time.Time
// GetUnixTime return the unix timestamp when the operation was added
GetUnixTime() int64
- // Apply the operation to a Snapshot to create the final state
- Apply(snapshot Snapshot) Snapshot
+ // GetAuthor return the author of the operation
+ GetAuthor() Person
// GetFiles return the files needed by this operation
GetFiles() []git.Hash
-
- // TODO: data validation (ex: a title is a single line)
- // Validate() bool
+ // Apply the operation to a Snapshot to create the final state
+ Apply(snapshot Snapshot) Snapshot
+ // Validate check if the operation is valid (ex: a title is a single line)
+ Validate() error
}
// OpBase implement the common code for all operations
@@ -65,7 +69,35 @@ func (op OpBase) GetUnixTime() int64 {
return op.UnixTime
}
+// GetAuthor return the author of the operation
+func (op OpBase) GetAuthor() Person {
+ return op.Author
+}
+
// GetFiles return the files needed by this operation
func (op OpBase) GetFiles() []git.Hash {
return nil
}
+
+// Validate check the OpBase for errors
+func OpBaseValidate(op Operation, opType OperationType) error {
+ if op.OpType() != opType {
+ return fmt.Errorf("incorrect operation type (expected: %v, actual: %v)", opType, op.OpType())
+ }
+
+ if op.GetUnixTime() == 0 {
+ return fmt.Errorf("time not set")
+ }
+
+ if err := op.GetAuthor().Validate(); err != nil {
+ return errors.Wrap(err, "author")
+ }
+
+ for _, hash := range op.GetFiles() {
+ if !hash.IsValid() {
+ return fmt.Errorf("file with invalid hash %v", hash)
+ }
+ }
+
+ return nil
+}