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