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