git-bug/bug/operation.go

226 lines
5.6 KiB
Go
Raw Normal View History

2018-07-13 17:13:40 +03:00
package bug
2018-07-12 22:31:41 +03:00
import (
"crypto/sha256"
"encoding/json"
"fmt"
"time"
"github.com/pkg/errors"
2018-11-21 20:56:12 +03:00
"github.com/MichaelMure/git-bug/entity"
"github.com/MichaelMure/git-bug/identity"
"github.com/MichaelMure/git-bug/util/git"
)
// OperationType is an operation type identifier
2018-07-12 22:31:41 +03:00
type OperationType int
const (
2018-07-17 20:28:37 +03:00
_ OperationType = iota
CreateOp
SetTitleOp
AddCommentOp
SetStatusOp
2018-07-18 17:41:09 +03:00
LabelChangeOp
EditCommentOp
NoOpOp
SetMetadataOp
2018-07-12 22:31:41 +03:00
)
2018-08-13 16:28:16 +03:00
// Operation define the interface to fulfill for an edit operation of a Bug
2018-07-12 22:31:41 +03:00
type Operation interface {
// base return the OpBase of the Operation, for package internal use
base() *OpBase
// Id return the identifier of the operation, to be used for back references
Id() entity.Id
2018-08-13 16:28:16 +03:00
// Time return the time when the operation was added
Time() time.Time
// GetUnixTime return the unix timestamp when the operation was added
GetUnixTime() int64
// GetFiles return the files needed by this operation
GetFiles() []git.Hash
// Apply the operation to a Snapshot to create the final state
Apply(snapshot *Snapshot)
// Validate check if the operation is valid (ex: a title is a single line)
Validate() error
// SetMetadata store arbitrary metadata about the operation
SetMetadata(key string, value string)
// GetMetadata retrieve arbitrary metadata about the operation
GetMetadata(key string) (string, bool)
// AllMetadata return all metadata for this operation
AllMetadata() map[string]string
// GetAuthor return the author identity
GetAuthor() identity.Interface
// sign-post method for gqlgen
IsOperation()
2018-07-12 22:31:41 +03:00
}
func deriveId(data []byte) entity.Id {
sum := sha256.Sum256(data)
return entity.Id(fmt.Sprintf("%x", sum))
}
func idOperation(op Operation) entity.Id {
base := op.base()
if base.id == "" {
// something went really wrong
panic("op's id not set")
}
if base.id == entity.UnsetId {
// This means we are trying to get the op's Id *before* it has been stored, for instance when
// adding multiple ops in one go in an OperationPack.
// As the Id is computed based on the actual bytes written on the disk, we are going to predict
// those and then get the Id. This is safe as it will be the exact same code writing on disk later.
data, err := json.Marshal(op)
if err != nil {
panic(err)
}
base.id = deriveId(data)
}
return base.id
}
2018-08-13 16:28:16 +03:00
// OpBase implement the common code for all operations
type OpBase struct {
OperationType OperationType `json:"type"`
Author identity.Interface `json:"author"`
UnixTime int64 `json:"timestamp"`
Metadata map[string]string `json:"metadata,omitempty"`
// Not serialized. Store the op's id in memory.
id entity.Id
2018-11-21 20:56:12 +03:00
// Not serialized. Store the extra metadata in memory,
// compiled from SetMetadataOperation.
extraMetadata map[string]string
}
// newOpBase is the constructor for an OpBase
2018-11-21 20:56:12 +03:00
func newOpBase(opType OperationType, author identity.Interface, unixTime int64) OpBase {
2018-10-01 12:37:17 +03:00
return OpBase{
OperationType: opType,
Author: author,
UnixTime: unixTime,
id: entity.UnsetId,
}
}
2018-11-21 20:56:12 +03:00
func (op *OpBase) UnmarshalJSON(data []byte) error {
// Compute the Id when loading the op from disk.
op.id = deriveId(data)
2019-01-19 21:23:31 +03:00
aux := struct {
OperationType OperationType `json:"type"`
Author json.RawMessage `json:"author"`
UnixTime int64 `json:"timestamp"`
Metadata map[string]string `json:"metadata,omitempty"`
}{}
2018-11-21 20:56:12 +03:00
if err := json.Unmarshal(data, &aux); err != nil {
return err
}
2019-01-19 21:23:31 +03:00
// delegate the decoding of the identity
author, err := identity.UnmarshalJSON(aux.Author)
if err != nil {
return err
}
2018-11-21 20:56:12 +03:00
op.OperationType = aux.OperationType
2019-01-19 21:23:31 +03:00
op.Author = author
2018-11-21 20:56:12 +03:00
op.UnixTime = aux.UnixTime
op.Metadata = aux.Metadata
return nil
}
2018-08-13 16:28:16 +03:00
// Time return the time when the operation was added
func (op *OpBase) Time() time.Time {
return time.Unix(op.UnixTime, 0)
}
// GetUnixTime return the unix timestamp when the operation was added
func (op *OpBase) GetUnixTime() int64 {
return op.UnixTime
}
// GetFiles return the files needed by this operation
func (op *OpBase) GetFiles() []git.Hash {
return nil
}
// Validate check the OpBase for errors
func opBaseValidate(op Operation, opType OperationType) error {
if op.base().OperationType != opType {
return fmt.Errorf("incorrect operation type (expected: %v, actual: %v)", opType, op.base().OperationType)
}
if op.GetUnixTime() == 0 {
return fmt.Errorf("time not set")
}
2018-11-21 20:56:12 +03:00
if op.base().Author == nil {
return fmt.Errorf("author not set")
}
if err := op.base().Author.Validate(); err != nil {
return errors.Wrap(err, "author")
}
for _, hash := range op.GetFiles() {
if !hash.IsValid() {
return fmt.Errorf("file with invalid hash %v", hash)
}
}
return nil
}
// SetMetadata store arbitrary metadata about the operation
func (op *OpBase) SetMetadata(key string, value string) {
if op.Metadata == nil {
op.Metadata = make(map[string]string)
}
op.Metadata[key] = value
op.id = entity.UnsetId
}
// GetMetadata retrieve arbitrary metadata about the operation
func (op *OpBase) GetMetadata(key string) (string, bool) {
val, ok := op.Metadata[key]
if ok {
return val, true
}
// extraMetadata can't replace the original operations value if any
val, ok = op.extraMetadata[key]
return val, ok
}
// AllMetadata return all metadata for this operation
func (op *OpBase) AllMetadata() map[string]string {
result := make(map[string]string)
for key, val := range op.extraMetadata {
result[key] = val
}
// Original metadata take precedence
for key, val := range op.Metadata {
result[key] = val
}
return result
}
// GetAuthor return author identity
func (op *OpBase) GetAuthor() identity.Interface {
return op.Author
}