1package bug
2
3import (
4 "crypto/sha256"
5 "encoding/json"
6 "fmt"
7 "time"
8
9 "github.com/MichaelMure/git-bug/identity"
10
11 "github.com/MichaelMure/git-bug/util/git"
12 "github.com/pkg/errors"
13)
14
15// OperationType is an operation type identifier
16type OperationType int
17
18const (
19 _ OperationType = iota
20 CreateOp
21 SetTitleOp
22 AddCommentOp
23 SetStatusOp
24 LabelChangeOp
25 EditCommentOp
26 NoOpOp
27 SetMetadataOp
28)
29
30// Operation define the interface to fulfill for an edit operation of a Bug
31type Operation interface {
32 // base return the OpBase of the Operation, for package internal use
33 base() *OpBase
34 // Hash return the hash of the operation, to be used for back references
35 Hash() (git.Hash, error)
36 // Time return the time when the operation was added
37 Time() time.Time
38 // GetUnixTime return the unix timestamp when the operation was added
39 GetUnixTime() int64
40 // GetFiles return the files needed by this operation
41 GetFiles() []git.Hash
42 // Apply the operation to a Snapshot to create the final state
43 Apply(snapshot *Snapshot)
44 // Validate check if the operation is valid (ex: a title is a single line)
45 Validate() error
46 // SetMetadata store arbitrary metadata about the operation
47 SetMetadata(key string, value string)
48 // GetMetadata retrieve arbitrary metadata about the operation
49 GetMetadata(key string) (string, bool)
50 // AllMetadata return all metadata for this operation
51 AllMetadata() map[string]string
52}
53
54func hashRaw(data []byte) git.Hash {
55 hasher := sha256.New()
56 // Write can't fail
57 _, _ = hasher.Write(data)
58 return git.Hash(fmt.Sprintf("%x", hasher.Sum(nil)))
59}
60
61// hash compute the hash of the serialized operation
62func hashOperation(op Operation) (git.Hash, error) {
63 base := op.base()
64
65 if base.hash != "" {
66 return base.hash, nil
67 }
68
69 data, err := json.Marshal(op)
70 if err != nil {
71 return "", err
72 }
73
74 base.hash = hashRaw(data)
75
76 return base.hash, nil
77}
78
79// OpBase implement the common code for all operations
80type OpBase struct {
81 OperationType OperationType
82 Author identity.Interface
83 UnixTime int64
84 Metadata map[string]string
85 // Not serialized. Store the op's hash in memory.
86 hash git.Hash
87 // Not serialized. Store the extra metadata in memory,
88 // compiled from SetMetadataOperation.
89 extraMetadata map[string]string
90}
91
92// newOpBase is the constructor for an OpBase
93func newOpBase(opType OperationType, author identity.Interface, unixTime int64) OpBase {
94 return OpBase{
95 OperationType: opType,
96 Author: author,
97 UnixTime: unixTime,
98 }
99}
100
101func (op OpBase) MarshalJSON() ([]byte, error) {
102 return json.Marshal(struct {
103 OperationType OperationType `json:"type"`
104 Author identity.Interface `json:"author"`
105 UnixTime int64 `json:"timestamp"`
106 Metadata map[string]string `json:"metadata,omitempty"`
107 }{
108 OperationType: op.OperationType,
109 Author: op.Author,
110 UnixTime: op.UnixTime,
111 Metadata: op.Metadata,
112 })
113}
114
115func (op *OpBase) UnmarshalJSON(data []byte) error {
116 aux := struct {
117 OperationType OperationType `json:"type"`
118 Author json.RawMessage `json:"author"`
119 UnixTime int64 `json:"timestamp"`
120 Metadata map[string]string `json:"metadata,omitempty"`
121 }{}
122
123 if err := json.Unmarshal(data, &aux); err != nil {
124 return err
125 }
126
127 // delegate the decoding of the identity
128 author, err := identity.UnmarshalJSON(aux.Author)
129 if err != nil {
130 return err
131 }
132
133 op.OperationType = aux.OperationType
134 op.Author = author
135 op.UnixTime = aux.UnixTime
136 op.Metadata = aux.Metadata
137
138 return nil
139}
140
141// Time return the time when the operation was added
142func (op *OpBase) Time() time.Time {
143 return time.Unix(op.UnixTime, 0)
144}
145
146// GetUnixTime return the unix timestamp when the operation was added
147func (op *OpBase) GetUnixTime() int64 {
148 return op.UnixTime
149}
150
151// GetFiles return the files needed by this operation
152func (op *OpBase) GetFiles() []git.Hash {
153 return nil
154}
155
156// Validate check the OpBase for errors
157func opBaseValidate(op Operation, opType OperationType) error {
158 if op.base().OperationType != opType {
159 return fmt.Errorf("incorrect operation type (expected: %v, actual: %v)", opType, op.base().OperationType)
160 }
161
162 if op.GetUnixTime() == 0 {
163 return fmt.Errorf("time not set")
164 }
165
166 if op.base().Author == nil {
167 return fmt.Errorf("author not set")
168 }
169
170 if err := op.base().Author.Validate(); err != nil {
171 return errors.Wrap(err, "author")
172 }
173
174 for _, hash := range op.GetFiles() {
175 if !hash.IsValid() {
176 return fmt.Errorf("file with invalid hash %v", hash)
177 }
178 }
179
180 return nil
181}
182
183// SetMetadata store arbitrary metadata about the operation
184func (op *OpBase) SetMetadata(key string, value string) {
185 if op.Metadata == nil {
186 op.Metadata = make(map[string]string)
187 }
188
189 op.Metadata[key] = value
190 op.hash = ""
191}
192
193// GetMetadata retrieve arbitrary metadata about the operation
194func (op *OpBase) GetMetadata(key string) (string, bool) {
195 val, ok := op.Metadata[key]
196
197 if ok {
198 return val, true
199 }
200
201 // extraMetadata can't replace the original operations value if any
202 val, ok = op.extraMetadata[key]
203
204 return val, ok
205}
206
207// AllMetadata return all metadata for this operation
208func (op *OpBase) AllMetadata() map[string]string {
209 result := make(map[string]string)
210
211 for key, val := range op.extraMetadata {
212 result[key] = val
213 }
214
215 // Original metadata take precedence
216 for key, val := range op.Metadata {
217 result[key] = val
218 }
219
220 return result
221}