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)
 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	hasher.Write(data)
 51	return git.Hash(fmt.Sprintf("%x", hasher.Sum(nil)))
 52}
 53
 54// hash compute the hash of the serialized operation
 55func hashOperation(op Operation) (git.Hash, error) {
 56	base := op.base()
 57
 58	if base.hash != "" {
 59		return base.hash, nil
 60	}
 61
 62	data, err := json.Marshal(op)
 63	if err != nil {
 64		return "", err
 65	}
 66
 67	base.hash = hashRaw(data)
 68
 69	return base.hash, nil
 70}
 71
 72// OpBase implement the common code for all operations
 73type OpBase struct {
 74	OperationType OperationType `json:"type"`
 75	Author        Person        `json:"author"`
 76	UnixTime      int64         `json:"timestamp"`
 77	hash          git.Hash
 78	Metadata      map[string]string `json:"metadata,omitempty"`
 79}
 80
 81// newOpBase is the constructor for an OpBase
 82func newOpBase(opType OperationType, author Person, unixTime int64) *OpBase {
 83	return &OpBase{
 84		OperationType: opType,
 85		Author:        author,
 86		UnixTime:      unixTime,
 87	}
 88}
 89
 90// Time return the time when the operation was added
 91func (op *OpBase) Time() time.Time {
 92	return time.Unix(op.UnixTime, 0)
 93}
 94
 95// GetUnixTime return the unix timestamp when the operation was added
 96func (op *OpBase) GetUnixTime() int64 {
 97	return op.UnixTime
 98}
 99
100// GetFiles return the files needed by this operation
101func (op *OpBase) GetFiles() []git.Hash {
102	return nil
103}
104
105// Validate check the OpBase for errors
106func opBaseValidate(op Operation, opType OperationType) error {
107	if op.base().OperationType != opType {
108		return fmt.Errorf("incorrect operation type (expected: %v, actual: %v)", opType, op.base().OperationType)
109	}
110
111	if _, err := op.Hash(); err != nil {
112		return errors.Wrap(err, "op is not serializable")
113	}
114
115	if op.GetUnixTime() == 0 {
116		return fmt.Errorf("time not set")
117	}
118
119	if err := op.base().Author.Validate(); err != nil {
120		return errors.Wrap(err, "author")
121	}
122
123	for _, hash := range op.GetFiles() {
124		if !hash.IsValid() {
125			return fmt.Errorf("file with invalid hash %v", hash)
126		}
127	}
128
129	return nil
130}
131
132// SetMetadata store arbitrary metadata about the operation
133func (op *OpBase) SetMetadata(key string, value string) {
134	if op.Metadata == nil {
135		op.Metadata = make(map[string]string)
136	}
137
138	op.Metadata[key] = value
139}
140
141// GetMetadata retrieve arbitrary metadata about the operation
142func (op *OpBase) GetMetadata(key string) (string, bool) {
143	val, ok := op.Metadata[key]
144	return val, ok
145}