1package bug
2
3import (
4 "fmt"
5
6 "github.com/MichaelMure/git-bug/identity"
7
8 "github.com/MichaelMure/git-bug/util/git"
9 "github.com/MichaelMure/git-bug/util/text"
10)
11
12var _ Operation = &AddCommentOperation{}
13
14// AddCommentOperation will add a new comment in the bug
15type AddCommentOperation struct {
16 OpBase
17 Message string `json:"message"`
18 // TODO: change for a map[string]util.hash to store the filename ?
19 Files []git.Hash `json:"files"`
20}
21
22func (op *AddCommentOperation) base() *OpBase {
23 return &op.OpBase
24}
25
26func (op *AddCommentOperation) Hash() (git.Hash, error) {
27 return hashOperation(op)
28}
29
30func (op *AddCommentOperation) Apply(snapshot *Snapshot) {
31 comment := Comment{
32 Message: op.Message,
33 Author: op.Author,
34 Files: op.Files,
35 UnixTime: Timestamp(op.UnixTime),
36 }
37
38 snapshot.Comments = append(snapshot.Comments, comment)
39
40 hash, err := op.Hash()
41 if err != nil {
42 // Should never error unless a programming error happened
43 // (covered in OpBase.Validate())
44 panic(err)
45 }
46
47 item := &AddCommentTimelineItem{
48 CommentTimelineItem: NewCommentTimelineItem(hash, comment),
49 }
50
51 snapshot.Timeline = append(snapshot.Timeline, item)
52}
53
54func (op *AddCommentOperation) GetFiles() []git.Hash {
55 return op.Files
56}
57
58func (op *AddCommentOperation) Validate() error {
59 if err := opBaseValidate(op, AddCommentOp); err != nil {
60 return err
61 }
62
63 if !text.Safe(op.Message) {
64 return fmt.Errorf("message is not fully printable")
65 }
66
67 return nil
68}
69
70// Sign post method for gqlgen
71func (op *AddCommentOperation) IsAuthored() {}
72
73func NewAddCommentOp(author identity.Interface, unixTime int64, message string, files []git.Hash) *AddCommentOperation {
74 return &AddCommentOperation{
75 OpBase: newOpBase(AddCommentOp, author, unixTime),
76 Message: message,
77 Files: files,
78 }
79}
80
81// CreateTimelineItem replace a AddComment operation in the Timeline and hold its edition history
82type AddCommentTimelineItem struct {
83 CommentTimelineItem
84}
85
86// Convenience function to apply the operation
87func AddComment(b Interface, author identity.Interface, unixTime int64, message string) (*AddCommentOperation, error) {
88 return AddCommentWithFiles(b, author, unixTime, message, nil)
89}
90
91func AddCommentWithFiles(b Interface, author identity.Interface, unixTime int64, message string, files []git.Hash) (*AddCommentOperation, error) {
92 addCommentOp := NewAddCommentOp(author, unixTime, message, files)
93 if err := addCommentOp.Validate(); err != nil {
94 return nil, err
95 }
96 b.Append(addCommentOp)
97 return addCommentOp, nil
98}