2018-07-13 23:53:53 +03:00
|
|
|
package operations
|
|
|
|
|
2018-07-14 23:18:40 +03:00
|
|
|
import (
|
2018-09-15 14:15:00 +03:00
|
|
|
"fmt"
|
|
|
|
|
2018-07-14 23:18:40 +03:00
|
|
|
"github.com/MichaelMure/git-bug/bug"
|
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-14 23:18:40 +03:00
|
|
|
)
|
2018-07-13 23:53:53 +03:00
|
|
|
|
2018-07-18 01:16:06 +03:00
|
|
|
// AddCommentOperation will add a new comment in the bug
|
|
|
|
|
2018-07-13 23:53:53 +03:00
|
|
|
var _ bug.Operation = AddCommentOperation{}
|
|
|
|
|
|
|
|
type AddCommentOperation struct {
|
2018-09-24 21:19:16 +03:00
|
|
|
*bug.OpBase
|
2018-09-12 17:57:04 +03:00
|
|
|
Message string `json:"message"`
|
2018-08-06 21:31:20 +03:00
|
|
|
// TODO: change for a map[string]util.hash to store the filename ?
|
2018-09-12 17:57:04 +03:00
|
|
|
Files []git.Hash `json:"files"`
|
2018-07-13 23:53:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (op AddCommentOperation) Apply(snapshot bug.Snapshot) bug.Snapshot {
|
|
|
|
comment := bug.Comment{
|
2018-07-18 01:16:06 +03:00
|
|
|
Message: op.Message,
|
|
|
|
Author: op.Author,
|
2018-09-12 17:57:04 +03:00
|
|
|
Files: op.Files,
|
2018-09-10 20:03:17 +03:00
|
|
|
UnixTime: op.UnixTime,
|
2018-07-13 23:53:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
snapshot.Comments = append(snapshot.Comments, comment)
|
|
|
|
|
|
|
|
return snapshot
|
|
|
|
}
|
2018-07-25 19:01:32 +03:00
|
|
|
|
2018-09-12 17:57:04 +03:00
|
|
|
func (op AddCommentOperation) GetFiles() []git.Hash {
|
|
|
|
return op.Files
|
2018-08-03 00:37:49 +03:00
|
|
|
}
|
|
|
|
|
2018-09-15 14:15:00 +03:00
|
|
|
func (op AddCommentOperation) Validate() error {
|
|
|
|
if err := bug.OpBaseValidate(op, bug.AddCommentOp); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if text.Empty(op.Message) {
|
|
|
|
return fmt.Errorf("message is empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !text.Safe(op.Message) {
|
|
|
|
return fmt.Errorf("message is not fully printable")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-09-11 23:04:16 +03:00
|
|
|
func NewAddCommentOp(author bug.Person, message string, files []git.Hash) AddCommentOperation {
|
2018-07-25 22:25:26 +03:00
|
|
|
return AddCommentOperation{
|
|
|
|
OpBase: bug.NewOpBase(bug.AddCommentOp, author),
|
|
|
|
Message: message,
|
2018-09-12 17:57:04 +03:00
|
|
|
Files: files,
|
2018-07-25 22:25:26 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convenience function to apply the operation
|
2018-09-15 14:15:00 +03:00
|
|
|
func Comment(b bug.Interface, author bug.Person, message string) error {
|
|
|
|
return CommentWithFiles(b, author, message, nil)
|
2018-08-03 00:37:49 +03:00
|
|
|
}
|
|
|
|
|
2018-09-15 14:15:00 +03:00
|
|
|
func CommentWithFiles(b bug.Interface, author bug.Person, message string, files []git.Hash) error {
|
2018-08-03 00:37:49 +03:00
|
|
|
addCommentOp := NewAddCommentOp(author, message, files)
|
2018-09-15 14:15:00 +03:00
|
|
|
if err := addCommentOp.Validate(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-07-25 19:01:32 +03:00
|
|
|
b.Append(addCommentOp)
|
2018-09-15 14:15:00 +03:00
|
|
|
return nil
|
2018-07-25 19:01:32 +03:00
|
|
|
}
|