aboutsummaryrefslogtreecommitdiffstats
path: root/operations/set_title.go
diff options
context:
space:
mode:
Diffstat (limited to 'operations/set_title.go')
-rw-r--r--operations/set_title.go40
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
}