operation.go

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