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
56// Sign post method for gqlgen
57func (op *SetStatusOperation) IsAuthored() {}
58
59func NewSetStatusOp(author Person, unixTime int64, status Status) *SetStatusOperation {
60 return &SetStatusOperation{
61 OpBase: newOpBase(SetStatusOp, author, unixTime),
62 Status: status,
63 }
64}
65
66type SetStatusTimelineItem struct {
67 hash git.Hash
68 Author Person
69 UnixTime Timestamp
70 Status Status
71}
72
73func (s SetStatusTimelineItem) Hash() git.Hash {
74 return s.hash
75}
76
77// Convenience function to apply the operation
78func Open(b Interface, author Person, unixTime int64) (*SetStatusOperation, error) {
79 op := NewSetStatusOp(author, unixTime, OpenStatus)
80 if err := op.Validate(); err != nil {
81 return nil, err
82 }
83 b.Append(op)
84 return op, nil
85}
86
87// Convenience function to apply the operation
88func Close(b Interface, author Person, unixTime int64) (*SetStatusOperation, error) {
89 op := NewSetStatusOp(author, unixTime, ClosedStatus)
90 if err := op.Validate(); err != nil {
91 return nil, err
92 }
93 b.Append(op)
94 return op, nil
95}