1package bug
2
3import (
4 "github.com/MichaelMure/git-bug/util/git"
5 "github.com/pkg/errors"
6
7 "fmt"
8 "time"
9)
10
11// OperationType is an operation type identifier
12type OperationType int
13
14const (
15 _ OperationType = iota
16 CreateOp
17 SetTitleOp
18 AddCommentOp
19 SetStatusOp
20 LabelChangeOp
21)
22
23// Operation define the interface to fulfill for an edit operation of a Bug
24type Operation interface {
25 // base return the OpBase of the Operation, for package internal use
26 base() *OpBase
27 // Time return the time when the operation was added
28 Time() time.Time
29 // GetUnixTime return the unix timestamp when the operation was added
30 GetUnixTime() int64
31 // GetFiles return the files needed by this operation
32 GetFiles() []git.Hash
33 // Apply the operation to a Snapshot to create the final state
34 Apply(snapshot Snapshot) Snapshot
35 // Validate check if the operation is valid (ex: a title is a single line)
36 Validate() error
37 // SetMetadata store arbitrary metadata about the operation
38 SetMetadata(key string, value string)
39 // GetMetadata retrieve arbitrary metadata about the operation
40 GetMetadata(key string) (string, bool)
41}
42
43// OpBase implement the common code for all operations
44type OpBase struct {
45 OperationType OperationType `json:"type"`
46 Author Person `json:"author"`
47 UnixTime int64 `json:"timestamp"`
48 Metadata map[string]string `json:"metadata,omitempty"`
49}
50
51// newOpBase is the constructor for an OpBase
52func newOpBase(opType OperationType, author Person, unixTime int64) *OpBase {
53 return &OpBase{
54 OperationType: opType,
55 Author: author,
56 UnixTime: unixTime,
57 }
58}
59
60// Time return the time when the operation was added
61func (op *OpBase) Time() time.Time {
62 return time.Unix(op.UnixTime, 0)
63}
64
65// GetUnixTime return the unix timestamp when the operation was added
66func (op *OpBase) GetUnixTime() int64 {
67 return op.UnixTime
68}
69
70// GetFiles return the files needed by this operation
71func (op *OpBase) GetFiles() []git.Hash {
72 return nil
73}
74
75// Validate check the OpBase for errors
76func opBaseValidate(op Operation, opType OperationType) error {
77 if op.base().OperationType != opType {
78 return fmt.Errorf("incorrect operation type (expected: %v, actual: %v)", opType, op.base().OperationType)
79 }
80
81 if op.GetUnixTime() == 0 {
82 return fmt.Errorf("time not set")
83 }
84
85 if err := op.base().Author.Validate(); err != nil {
86 return errors.Wrap(err, "author")
87 }
88
89 for _, hash := range op.GetFiles() {
90 if !hash.IsValid() {
91 return fmt.Errorf("file with invalid hash %v", hash)
92 }
93 }
94
95 return nil
96}
97
98// SetMetadata store arbitrary metadata about the operation
99func (op *OpBase) SetMetadata(key string, value string) {
100 if op.Metadata == nil {
101 op.Metadata = make(map[string]string)
102 }
103
104 op.Metadata[key] = value
105}
106
107// GetMetadata retrieve arbitrary metadata about the operation
108func (op *OpBase) GetMetadata(key string) (string, bool) {
109 val, ok := op.Metadata[key]
110 return val, ok
111}