2018-09-28 21:39:39 +03:00
|
|
|
package bug
|
2018-07-12 22:31:41 +03:00
|
|
|
|
|
|
|
import (
|
2018-09-15 14:15:00 +03:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2018-09-11 23:04:16 +03:00
|
|
|
"github.com/MichaelMure/git-bug/util/git"
|
2018-09-15 14:15:00 +03:00
|
|
|
"github.com/MichaelMure/git-bug/util/text"
|
2018-07-12 22:31:41 +03:00
|
|
|
)
|
|
|
|
|
2018-09-29 21:41:19 +03:00
|
|
|
var _ Operation = &CreateOperation{}
|
2018-07-12 22:31:41 +03:00
|
|
|
|
2018-09-29 21:41:19 +03:00
|
|
|
// CreateOperation define the initial creation of a bug
|
2018-07-12 22:31:41 +03:00
|
|
|
type CreateOperation struct {
|
2018-09-28 21:39:39 +03:00
|
|
|
*OpBase
|
2018-09-12 17:57:04 +03:00
|
|
|
Title string `json:"title"`
|
|
|
|
Message string `json:"message"`
|
|
|
|
Files []git.Hash `json:"files"`
|
2018-07-12 22:31:41 +03:00
|
|
|
}
|
|
|
|
|
2018-09-29 21:41:19 +03:00
|
|
|
func (op *CreateOperation) base() *OpBase {
|
2018-09-28 21:39:39 +03:00
|
|
|
return op.OpBase
|
|
|
|
}
|
|
|
|
|
2018-09-29 21:41:19 +03:00
|
|
|
func (op *CreateOperation) Hash() (git.Hash, error) {
|
2018-09-29 00:51:47 +03:00
|
|
|
return hashOperation(op)
|
|
|
|
}
|
|
|
|
|
2018-09-29 21:41:19 +03:00
|
|
|
func (op *CreateOperation) Apply(snapshot *Snapshot) {
|
2018-07-12 22:31:41 +03:00
|
|
|
snapshot.Title = op.Title
|
2018-09-29 21:41:19 +03:00
|
|
|
|
|
|
|
comment := Comment{
|
|
|
|
Message: op.Message,
|
|
|
|
Author: op.Author,
|
2018-09-30 12:00:39 +03:00
|
|
|
UnixTime: Timestamp(op.UnixTime),
|
2018-07-12 22:31:41 +03:00
|
|
|
}
|
2018-09-29 21:41:19 +03:00
|
|
|
|
|
|
|
snapshot.Comments = []Comment{comment}
|
2018-08-01 03:15:40 +03:00
|
|
|
snapshot.Author = op.Author
|
|
|
|
snapshot.CreatedAt = op.Time()
|
2018-09-29 21:41:19 +03:00
|
|
|
|
|
|
|
hash, err := op.Hash()
|
|
|
|
if err != nil {
|
|
|
|
// Should never error unless a programming error happened
|
|
|
|
// (covered in OpBase.Validate())
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
snapshot.Timeline = []TimelineItem{
|
2018-09-30 18:15:54 +03:00
|
|
|
&CreateTimelineItem{
|
|
|
|
CommentTimelineItem: NewCommentTimelineItem(hash, comment),
|
|
|
|
},
|
2018-09-29 21:41:19 +03:00
|
|
|
}
|
2018-07-12 22:31:41 +03:00
|
|
|
}
|
2018-07-25 19:01:32 +03:00
|
|
|
|
2018-09-29 21:41:19 +03:00
|
|
|
func (op *CreateOperation) GetFiles() []git.Hash {
|
2018-09-12 17:57:04 +03:00
|
|
|
return op.Files
|
2018-08-03 00:37:49 +03:00
|
|
|
}
|
|
|
|
|
2018-09-29 21:41:19 +03:00
|
|
|
func (op *CreateOperation) Validate() error {
|
2018-09-28 21:39:39 +03:00
|
|
|
if err := opBaseValidate(op, CreateOp); err != nil {
|
2018-09-15 14:15:00 +03:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2018-09-29 21:41:19 +03:00
|
|
|
func NewCreateOp(author Person, unixTime int64, title, message string, files []git.Hash) *CreateOperation {
|
|
|
|
return &CreateOperation{
|
2018-09-28 21:39:39 +03:00
|
|
|
OpBase: newOpBase(CreateOp, author, unixTime),
|
2018-07-25 22:25:26 +03:00
|
|
|
Title: title,
|
|
|
|
Message: message,
|
2018-09-12 17:57:04 +03:00
|
|
|
Files: files,
|
2018-07-25 22:25:26 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-30 18:15:54 +03:00
|
|
|
// CreateTimelineItem replace a Create operation in the Timeline and hold its edition history
|
|
|
|
type CreateTimelineItem struct {
|
|
|
|
CommentTimelineItem
|
|
|
|
}
|
|
|
|
|
2018-07-25 22:25:26 +03:00
|
|
|
// Convenience function to apply the operation
|
2018-09-28 21:39:39 +03:00
|
|
|
func Create(author Person, unixTime int64, title, message string) (*Bug, error) {
|
2018-09-25 18:56:58 +03:00
|
|
|
return CreateWithFiles(author, unixTime, title, message, nil)
|
2018-08-03 00:37:49 +03:00
|
|
|
}
|
|
|
|
|
2018-09-28 21:39:39 +03:00
|
|
|
func CreateWithFiles(author Person, unixTime int64, title, message string, files []git.Hash) (*Bug, error) {
|
|
|
|
newBug := NewBug()
|
2018-09-25 18:56:58 +03:00
|
|
|
createOp := NewCreateOp(author, unixTime, title, message, files)
|
2018-09-15 14:15:00 +03:00
|
|
|
|
|
|
|
if err := createOp.Validate(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-07-25 19:01:32 +03:00
|
|
|
newBug.Append(createOp)
|
|
|
|
|
|
|
|
return newBug, nil
|
|
|
|
}
|