1package bug
2
3import (
4 "crypto/sha256"
5 "encoding/json"
6 "fmt"
7 "time"
8
9 "github.com/MichaelMure/git-bug/util/git"
10 "github.com/pkg/errors"
11)
12
13// OperationType is an operation type identifier
14type OperationType int
15
16const (
17 _ OperationType = iota
18 CreateOp
19 SetTitleOp
20 AddCommentOp
21 SetStatusOp
22 LabelChangeOp
23 EditCommentOp
24 NoOpOp
25)
26
27// Operation define the interface to fulfill for an edit operation of a Bug
28type Operation interface {
29 // base return the OpBase of the Operation, for package internal use
30 base() *OpBase
31 // Hash return the hash of the operation, to be used for back references
32 Hash() (git.Hash, error)
33 // Time return the time when the operation was added
34 Time() time.Time
35 // GetUnixTime return the unix timestamp when the operation was added
36 GetUnixTime() int64
37 // GetFiles return the files needed by this operation
38 GetFiles() []git.Hash
39 // Apply the operation to a Snapshot to create the final state
40 Apply(snapshot *Snapshot)
41 // Validate check if the operation is valid (ex: a title is a single line)
42 Validate() error
43 // SetMetadata store arbitrary metadata about the operation
44 SetMetadata(key string, value string)
45 // GetMetadata retrieve arbitrary metadata about the operation
46 GetMetadata(key string) (string, bool)
47 // AllMetadata return all metadata for this operation
48 AllMetadata() map[string]string
49}
50
51func hashRaw(data []byte) git.Hash {
52 hasher := sha256.New()
53 // Write can't fail
54 _, _ = hasher.Write(data)
55 return git.Hash(fmt.Sprintf("%x", hasher.Sum(nil)))
56}
57
58// hash compute the hash of the serialized operation
59func hashOperation(op Operation) (git.Hash, error) {
60 base := op.base()
61
62 if base.hash != "" {
63 return base.hash, nil
64 }
65
66 data, err := json.Marshal(op)
67 if err != nil {
68 return "", err
69 }
70
71 base.hash = hashRaw(data)
72
73 return base.hash, nil
74}
75
76// OpBase implement the common code for all operations
77type OpBase struct {
78 OperationType OperationType `json:"type"`
79 Author Person `json:"author"`
80 UnixTime int64 `json:"timestamp"`
81 hash git.Hash
82 Metadata map[string]string `json:"metadata,omitempty"`
83}
84
85// newOpBase is the constructor for an OpBase
86func newOpBase(opType OperationType, author Person, unixTime int64) OpBase {
87 return OpBase{
88 OperationType: opType,
89 Author: author,
90 UnixTime: unixTime,
91 }
92}
93
94// Time return the time when the operation was added
95func (op *OpBase) Time() time.Time {
96 return time.Unix(op.UnixTime, 0)
97}
98
99// GetUnixTime return the unix timestamp when the operation was added
100func (op *OpBase) GetUnixTime() int64 {
101 return op.UnixTime
102}
103
104// GetFiles return the files needed by this operation
105func (op *OpBase) GetFiles() []git.Hash {
106 return nil
107}
108
109// Validate check the OpBase for errors
110func opBaseValidate(op Operation, opType OperationType) error {
111 if op.base().OperationType != opType {
112 return fmt.Errorf("incorrect operation type (expected: %v, actual: %v)", opType, op.base().OperationType)
113 }
114
115 if _, err := op.Hash(); err != nil {
116 return errors.Wrap(err, "op is not serializable")
117 }
118
119 if op.GetUnixTime() == 0 {
120 return fmt.Errorf("time not set")
121 }
122
123 if err := op.base().Author.Validate(); err != nil {
124 return errors.Wrap(err, "author")
125 }
126
127 for _, hash := range op.GetFiles() {
128 if !hash.IsValid() {
129 return fmt.Errorf("file with invalid hash %v", hash)
130 }
131 }
132
133 return nil
134}
135
136// SetMetadata store arbitrary metadata about the operation
137func (op *OpBase) SetMetadata(key string, value string) {
138 if op.Metadata == nil {
139 op.Metadata = make(map[string]string)
140 }
141
142 op.Metadata[key] = value
143 op.hash = ""
144}
145
146// GetMetadata retrieve arbitrary metadata about the operation
147func (op *OpBase) GetMetadata(key string) (string, bool) {
148 val, ok := op.Metadata[key]
149 return val, ok
150}
151
152// AllMetadata return all metadata for this operation
153func (op *OpBase) AllMetadata() map[string]string {
154 return op.Metadata
155}