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 // GetAuthor return the author identity
53 GetAuthor() identity.Interface
54}
55
56func hashRaw(data []byte) git.Hash {
57 hasher := sha256.New()
58 // Write can't fail
59 _, _ = hasher.Write(data)
60 return git.Hash(fmt.Sprintf("%x", hasher.Sum(nil)))
61}
62
63// hash compute the hash of the serialized operation
64func hashOperation(op Operation) (git.Hash, error) {
65 // TODO: this might not be the best idea: if a single bit change in the output of json.Marshal, this will break.
66 // Idea: hash the segment of serialized data (= immutable) instead of the go object in memory
67
68 base := op.base()
69
70 if base.hash != "" {
71 return base.hash, nil
72 }
73
74 data, err := json.Marshal(op)
75 if err != nil {
76 return "", err
77 }
78
79 base.hash = hashRaw(data)
80
81 return base.hash, nil
82}
83
84// OpBase implement the common code for all operations
85type OpBase struct {
86 OperationType OperationType
87 Author identity.Interface
88 UnixTime int64
89 Metadata map[string]string
90 // Not serialized. Store the op's hash in memory.
91 hash git.Hash
92 // Not serialized. Store the extra metadata in memory,
93 // compiled from SetMetadataOperation.
94 extraMetadata map[string]string
95}
96
97// newOpBase is the constructor for an OpBase
98func newOpBase(opType OperationType, author identity.Interface, unixTime int64) OpBase {
99 return OpBase{
100 OperationType: opType,
101 Author: author,
102 UnixTime: unixTime,
103 }
104}
105
106func (op OpBase) MarshalJSON() ([]byte, error) {
107 return json.Marshal(struct {
108 OperationType OperationType `json:"type"`
109 Author identity.Interface `json:"author"`
110 UnixTime int64 `json:"timestamp"`
111 Metadata map[string]string `json:"metadata,omitempty"`
112 }{
113 OperationType: op.OperationType,
114 Author: op.Author,
115 UnixTime: op.UnixTime,
116 Metadata: op.Metadata,
117 })
118}
119
120func (op *OpBase) UnmarshalJSON(data []byte) error {
121 aux := struct {
122 OperationType OperationType `json:"type"`
123 Author json.RawMessage `json:"author"`
124 UnixTime int64 `json:"timestamp"`
125 Metadata map[string]string `json:"metadata,omitempty"`
126 }{}
127
128 if err := json.Unmarshal(data, &aux); err != nil {
129 return err
130 }
131
132 // delegate the decoding of the identity
133 author, err := identity.UnmarshalJSON(aux.Author)
134 if err != nil {
135 return err
136 }
137
138 op.OperationType = aux.OperationType
139 op.Author = author
140 op.UnixTime = aux.UnixTime
141 op.Metadata = aux.Metadata
142
143 return nil
144}
145
146// Time return the time when the operation was added
147func (op *OpBase) Time() time.Time {
148 return time.Unix(op.UnixTime, 0)
149}
150
151// GetUnixTime return the unix timestamp when the operation was added
152func (op *OpBase) GetUnixTime() int64 {
153 return op.UnixTime
154}
155
156// GetFiles return the files needed by this operation
157func (op *OpBase) GetFiles() []git.Hash {
158 return nil
159}
160
161// Validate check the OpBase for errors
162func opBaseValidate(op Operation, opType OperationType) error {
163 if op.base().OperationType != opType {
164 return fmt.Errorf("incorrect operation type (expected: %v, actual: %v)", opType, op.base().OperationType)
165 }
166
167 if op.GetUnixTime() == 0 {
168 return fmt.Errorf("time not set")
169 }
170
171 if op.base().Author == nil {
172 return fmt.Errorf("author not set")
173 }
174
175 if err := op.base().Author.Validate(); err != nil {
176 return errors.Wrap(err, "author")
177 }
178
179 for _, hash := range op.GetFiles() {
180 if !hash.IsValid() {
181 return fmt.Errorf("file with invalid hash %v", hash)
182 }
183 }
184
185 return nil
186}
187
188// SetMetadata store arbitrary metadata about the operation
189func (op *OpBase) SetMetadata(key string, value string) {
190 if op.Metadata == nil {
191 op.Metadata = make(map[string]string)
192 }
193
194 op.Metadata[key] = value
195 op.hash = ""
196}
197
198// GetMetadata retrieve arbitrary metadata about the operation
199func (op *OpBase) GetMetadata(key string) (string, bool) {
200 val, ok := op.Metadata[key]
201
202 if ok {
203 return val, true
204 }
205
206 // extraMetadata can't replace the original operations value if any
207 val, ok = op.extraMetadata[key]
208
209 return val, ok
210}
211
212// AllMetadata return all metadata for this operation
213func (op *OpBase) AllMetadata() map[string]string {
214 result := make(map[string]string)
215
216 for key, val := range op.extraMetadata {
217 result[key] = val
218 }
219
220 // Original metadata take precedence
221 for key, val := range op.Metadata {
222 result[key] = val
223 }
224
225 return result
226}
227
228// GetAuthor return author identity
229func (op *OpBase) GetAuthor() identity.Interface {
230 return op.Author
231}