op_set_status.go

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