op_set_status.go

 1package bug
 2
 3import (
 4	"github.com/MichaelMure/git-bug/util/git"
 5	"github.com/pkg/errors"
 6)
 7
 8// SetStatusOperation will change the status of a bug
 9
10var _ Operation = SetStatusOperation{}
11
12type SetStatusOperation struct {
13	*OpBase
14	Status Status `json:"status"`
15}
16
17func (op SetStatusOperation) base() *OpBase {
18	return op.OpBase
19}
20
21func (op SetStatusOperation) Hash() (git.Hash, error) {
22	return hashOperation(op)
23}
24
25func (op SetStatusOperation) Apply(snapshot Snapshot) Snapshot {
26	snapshot.Status = op.Status
27
28	return snapshot
29}
30
31func (op SetStatusOperation) Validate() error {
32	if err := opBaseValidate(op, SetStatusOp); err != nil {
33		return err
34	}
35
36	if err := op.Status.Validate(); err != nil {
37		return errors.Wrap(err, "status")
38	}
39
40	return nil
41}
42
43func NewSetStatusOp(author Person, unixTime int64, status Status) SetStatusOperation {
44	return SetStatusOperation{
45		OpBase: newOpBase(SetStatusOp, author, unixTime),
46		Status: status,
47	}
48}
49
50// Convenience function to apply the operation
51func Open(b Interface, author Person, unixTime int64) error {
52	op := NewSetStatusOp(author, unixTime, OpenStatus)
53	if err := op.Validate(); err != nil {
54		return err
55	}
56	b.Append(op)
57	return nil
58}
59
60// Convenience function to apply the operation
61func Close(b Interface, author Person, unixTime int64) error {
62	op := NewSetStatusOp(author, unixTime, ClosedStatus)
63	if err := op.Validate(); err != nil {
64		return err
65	}
66	b.Append(op)
67	return nil
68}