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