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
27 hash, err := op.Hash()
28 if err != nil {
29 // Should never error unless a programming error happened
30 // (covered in OpBase.Validate())
31 panic(err)
32 }
33
34 item := &SetStatusTimelineItem{
35 hash: hash,
36 Author: op.Author,
37 UnixTime: Timestamp(op.UnixTime),
38 Status: op.Status,
39 }
40
41 snapshot.Timeline = append(snapshot.Timeline, item)
42}
43
44func (op *SetStatusOperation) Validate() error {
45 if err := opBaseValidate(op, SetStatusOp); err != nil {
46 return err
47 }
48
49 if err := op.Status.Validate(); err != nil {
50 return errors.Wrap(err, "status")
51 }
52
53 return nil
54}
55
56func NewSetStatusOp(author Person, unixTime int64, status Status) *SetStatusOperation {
57 return &SetStatusOperation{
58 OpBase: newOpBase(SetStatusOp, author, unixTime),
59 Status: status,
60 }
61}
62
63type SetStatusTimelineItem struct {
64 hash git.Hash
65 Author Person
66 UnixTime Timestamp
67 Status Status
68}
69
70func (s SetStatusTimelineItem) Hash() git.Hash {
71 return s.hash
72}
73
74// Convenience function to apply the operation
75func Open(b Interface, author Person, unixTime int64) error {
76 op := NewSetStatusOp(author, unixTime, OpenStatus)
77 if err := op.Validate(); err != nil {
78 return err
79 }
80 b.Append(op)
81 return nil
82}
83
84// Convenience function to apply the operation
85func Close(b Interface, author Person, unixTime int64) error {
86 op := NewSetStatusOp(author, unixTime, ClosedStatus)
87 if err := op.Validate(); err != nil {
88 return err
89 }
90 b.Append(op)
91 return nil
92}