aboutsummaryrefslogtreecommitdiffstats
path: root/bug/operation.go
blob: a408e1670f142d8e2a5c443c97fc74f537d623c9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package bug

import (
	"github.com/MichaelMure/git-bug/util/git"
	"github.com/pkg/errors"

	"fmt"
	"time"
)

// OperationType is an operation type identifier
type OperationType int

const (
	_ OperationType = iota
	CreateOp
	SetTitleOp
	AddCommentOp
	SetStatusOp
	LabelChangeOp
)

// Operation define the interface to fulfill for an edit operation of a Bug
type Operation interface {
	// OpType return the type of operation
	OpType() OperationType
	// Time return the time when the operation was added
	Time() time.Time
	// GetUnixTime return the unix timestamp when the operation was added
	GetUnixTime() int64
	// GetAuthor return the author of the operation
	GetAuthor() Person
	// 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) 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)
}

// OpBase implement the common code for all operations
type OpBase struct {
	OperationType OperationType     `json:"type"`
	Author        Person            `json:"author"`
	UnixTime      int64             `json:"timestamp"`
	Metadata      map[string]string `json:"metadata,omitempty"`
}

// NewOpBase is the constructor for an OpBase
func NewOpBase(opType OperationType, author Person, unixTime int64) *OpBase {
	return &OpBase{
		OperationType: opType,
		Author:        author,
		UnixTime:      unixTime,
	}
}

// OpType return the type of operation
func (op *OpBase) OpType() OperationType {
	return op.OperationType
}

// 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
}

// GetAuthor return the author of the operation
func (op *OpBase) GetAuthor() Person {
	return op.Author
}

// 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.OpType() != opType {
		return fmt.Errorf("incorrect operation type (expected: %v, actual: %v)", opType, op.OpType())
	}

	if op.GetUnixTime() == 0 {
		return fmt.Errorf("time not set")
	}

	if err := op.GetAuthor().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
}

// GetMetadata retrieve arbitrary metadata about the operation
func (op *OpBase) GetMetadata(key string) (string, bool) {
	val, ok := op.Metadata[key]
	return val, ok
}