1package bug
2
3import (
4 "fmt"
5
6 "github.com/MichaelMure/git-bug/util/git"
7 "github.com/MichaelMure/git-bug/util/text"
8)
9
10// AddCommentOperation will add a new comment in the bug
11
12var _ Operation = AddCommentOperation{}
13
14type AddCommentOperation struct {
15 *OpBase
16 Message string `json:"message"`
17 // TODO: change for a map[string]util.hash to store the filename ?
18 Files []git.Hash `json:"files"`
19}
20
21func (op AddCommentOperation) base() *OpBase {
22 return op.OpBase
23}
24
25func (op AddCommentOperation) Hash() (git.Hash, error) {
26 return hashOperation(op)
27}
28
29func (op AddCommentOperation) Apply(snapshot *Snapshot) {
30 comment := Comment{
31 Message: op.Message,
32 Author: op.Author,
33 Files: op.Files,
34 UnixTime: op.UnixTime,
35 }
36
37 snapshot.Comments = append(snapshot.Comments, comment)
38}
39
40func (op AddCommentOperation) GetFiles() []git.Hash {
41 return op.Files
42}
43
44func (op AddCommentOperation) Validate() error {
45 if err := opBaseValidate(op, AddCommentOp); err != nil {
46 return err
47 }
48
49 if text.Empty(op.Message) {
50 return fmt.Errorf("message is empty")
51 }
52
53 if !text.Safe(op.Message) {
54 return fmt.Errorf("message is not fully printable")
55 }
56
57 return nil
58}
59
60func NewAddCommentOp(author Person, unixTime int64, message string, files []git.Hash) AddCommentOperation {
61 return AddCommentOperation{
62 OpBase: newOpBase(AddCommentOp, author, unixTime),
63 Message: message,
64 Files: files,
65 }
66}
67
68// Convenience function to apply the operation
69func AddComment(b Interface, author Person, unixTime int64, message string) error {
70 return AddCommentWithFiles(b, author, unixTime, message, nil)
71}
72
73func AddCommentWithFiles(b Interface, author Person, unixTime int64, message string, files []git.Hash) error {
74 addCommentOp := NewAddCommentOp(author, unixTime, message, files)
75 if err := addCommentOp.Validate(); err != nil {
76 return err
77 }
78 b.Append(addCommentOp)
79 return nil
80}