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) Apply(snapshot Snapshot) Snapshot {
26 comment := Comment{
27 Message: op.Message,
28 Author: op.Author,
29 Files: op.Files,
30 UnixTime: op.UnixTime,
31 }
32
33 snapshot.Comments = append(snapshot.Comments, comment)
34
35 return snapshot
36}
37
38func (op AddCommentOperation) GetFiles() []git.Hash {
39 return op.Files
40}
41
42func (op AddCommentOperation) Validate() error {
43 if err := opBaseValidate(op, AddCommentOp); err != nil {
44 return err
45 }
46
47 if text.Empty(op.Message) {
48 return fmt.Errorf("message is empty")
49 }
50
51 if !text.Safe(op.Message) {
52 return fmt.Errorf("message is not fully printable")
53 }
54
55 return nil
56}
57
58func NewAddCommentOp(author Person, unixTime int64, message string, files []git.Hash) AddCommentOperation {
59 return AddCommentOperation{
60 OpBase: newOpBase(AddCommentOp, author, unixTime),
61 Message: message,
62 Files: files,
63 }
64}
65
66// Convenience function to apply the operation
67func AddComment(b Interface, author Person, unixTime int64, message string) error {
68 return AddCommentWithFiles(b, author, unixTime, message, nil)
69}
70
71func AddCommentWithFiles(b Interface, author Person, unixTime int64, message string, files []git.Hash) error {
72 addCommentOp := NewAddCommentOp(author, unixTime, message, files)
73 if err := addCommentOp.Validate(); err != nil {
74 return err
75 }
76 b.Append(addCommentOp)
77 return nil
78}