1package operations
 2
 3import (
 4	"github.com/MichaelMure/git-bug/bug"
 5	"github.com/MichaelMure/git-bug/util"
 6)
 7
 8// AddCommentOperation will add a new comment in the bug
 9
10var _ bug.Operation = AddCommentOperation{}
11
12type AddCommentOperation struct {
13	bug.OpBase
14	Message string
15	// TODO: change for a map[string]util.hash to store the filename ?
16	files []util.Hash
17}
18
19func (op AddCommentOperation) Apply(snapshot bug.Snapshot) bug.Snapshot {
20	comment := bug.Comment{
21		Message:  op.Message,
22		Author:   op.Author,
23		Files:    op.files,
24		UnixTime: op.UnixTime(),
25	}
26
27	snapshot.Comments = append(snapshot.Comments, comment)
28
29	return snapshot
30}
31
32func (op AddCommentOperation) Files() []util.Hash {
33	return op.files
34}
35
36func NewAddCommentOp(author bug.Person, message string, files []util.Hash) AddCommentOperation {
37	return AddCommentOperation{
38		OpBase:  bug.NewOpBase(bug.AddCommentOp, author),
39		Message: message,
40		files:   files,
41	}
42}
43
44// Convenience function to apply the operation
45func Comment(b bug.Interface, author bug.Person, message string) {
46	CommentWithFiles(b, author, message, nil)
47}
48
49func CommentWithFiles(b bug.Interface, author bug.Person, message string, files []util.Hash) {
50	addCommentOp := NewAddCommentOp(author, message, files)
51	b.Append(addCommentOp)
52}