aboutsummaryrefslogtreecommitdiffstats
path: root/bug/op_set_status.go
blob: cdfa25e740d206904e7281486cb9a512ecf87d04 (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
package bug

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

var _ Operation = &SetStatusOperation{}

// SetStatusOperation will change the status of a bug
type SetStatusOperation struct {
	*OpBase
	Status Status `json:"status"`
}

func (op *SetStatusOperation) base() *OpBase {
	return op.OpBase
}

func (op *SetStatusOperation) Hash() (git.Hash, error) {
	return hashOperation(op)
}

func (op *SetStatusOperation) Apply(snapshot *Snapshot) {
	snapshot.Status = op.Status
	snapshot.Timeline = append(snapshot.Timeline, op)
}

func (op *SetStatusOperation) Validate() error {
	if err := opBaseValidate(op, SetStatusOp); err != nil {
		return err
	}

	if err := op.Status.Validate(); err != nil {
		return errors.Wrap(err, "status")
	}

	return nil
}

func NewSetStatusOp(author Person, unixTime int64, status Status) *SetStatusOperation {
	return &SetStatusOperation{
		OpBase: newOpBase(SetStatusOp, author, unixTime),
		Status: status,
	}
}

// Convenience function to apply the operation
func Open(b Interface, author Person, unixTime int64) error {
	op := NewSetStatusOp(author, unixTime, OpenStatus)
	if err := op.Validate(); err != nil {
		return err
	}
	b.Append(op)
	return nil
}

// Convenience function to apply the operation
func Close(b Interface, author Person, unixTime int64) error {
	op := NewSetStatusOp(author, unixTime, ClosedStatus)
	if err := op.Validate(); err != nil {
		return err
	}
	b.Append(op)
	return nil
}