1package bug
2
3import (
4 "encoding/json"
5
6 "github.com/pkg/errors"
7
8 "github.com/MichaelMure/git-bug/identity"
9 "github.com/MichaelMure/git-bug/util/timestamp"
10)
11
12var _ Operation = &SetStatusOperation{}
13
14// SetStatusOperation will change the status of a bug
15type SetStatusOperation struct {
16 OpBase
17 Status Status
18}
19
20func (op *SetStatusOperation) base() *OpBase {
21 return &op.OpBase
22}
23
24func (op *SetStatusOperation) ID() string {
25 return idOperation(op)
26}
27
28func (op *SetStatusOperation) Apply(snapshot *Snapshot) {
29 snapshot.Status = op.Status
30 snapshot.addActor(op.Author)
31
32 item := &SetStatusTimelineItem{
33 id: op.ID(),
34 Author: op.Author,
35 UnixTime: timestamp.Timestamp(op.UnixTime),
36 Status: op.Status,
37 }
38
39 snapshot.Timeline = append(snapshot.Timeline, item)
40}
41
42func (op *SetStatusOperation) Validate() error {
43 if err := opBaseValidate(op, SetStatusOp); err != nil {
44 return err
45 }
46
47 if err := op.Status.Validate(); err != nil {
48 return errors.Wrap(err, "status")
49 }
50
51 return nil
52}
53
54// Workaround to avoid the inner OpBase.MarshalJSON overriding the outer op
55// MarshalJSON
56func (op *SetStatusOperation) MarshalJSON() ([]byte, error) {
57 base, err := json.Marshal(op.OpBase)
58 if err != nil {
59 return nil, err
60 }
61
62 // revert back to a flat map to be able to add our own fields
63 var data map[string]interface{}
64 if err := json.Unmarshal(base, &data); err != nil {
65 return nil, err
66 }
67
68 data["status"] = op.Status
69
70 return json.Marshal(data)
71}
72
73// Workaround to avoid the inner OpBase.MarshalJSON overriding the outer op
74// MarshalJSON
75func (op *SetStatusOperation) UnmarshalJSON(data []byte) error {
76 // Unmarshal OpBase and the op separately
77
78 base := OpBase{}
79 err := json.Unmarshal(data, &base)
80 if err != nil {
81 return err
82 }
83
84 aux := struct {
85 Status Status `json:"status"`
86 }{}
87
88 err = json.Unmarshal(data, &aux)
89 if err != nil {
90 return err
91 }
92
93 op.OpBase = base
94 op.Status = aux.Status
95
96 return nil
97}
98
99// Sign post method for gqlgen
100func (op *SetStatusOperation) IsAuthored() {}
101
102func NewSetStatusOp(author identity.Interface, unixTime int64, status Status) *SetStatusOperation {
103 return &SetStatusOperation{
104 OpBase: newOpBase(SetStatusOp, author, unixTime),
105 Status: status,
106 }
107}
108
109type SetStatusTimelineItem struct {
110 id string
111 Author identity.Interface
112 UnixTime timestamp.Timestamp
113 Status Status
114}
115
116func (s SetStatusTimelineItem) ID() string {
117 return s.id
118}
119
120// Sign post method for gqlgen
121func (s *SetStatusTimelineItem) IsAuthored() {}
122
123// Convenience function to apply the operation
124func Open(b Interface, author identity.Interface, unixTime int64) (*SetStatusOperation, error) {
125 op := NewSetStatusOp(author, unixTime, OpenStatus)
126 if err := op.Validate(); err != nil {
127 return nil, err
128 }
129 b.Append(op)
130 return op, nil
131}
132
133// Convenience function to apply the operation
134func Close(b Interface, author identity.Interface, unixTime int64) (*SetStatusOperation, error) {
135 op := NewSetStatusOp(author, unixTime, ClosedStatus)
136 if err := op.Validate(); err != nil {
137 return nil, err
138 }
139 b.Append(op)
140 return op, nil
141}