1package bug
2
3import (
4 "github.com/pkg/errors"
5
6 "github.com/MichaelMure/git-bug/entities/identity"
7 "github.com/MichaelMure/git-bug/entity"
8 "github.com/MichaelMure/git-bug/entity/dag"
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 dag.OpBase
17 Status Status `json:"status"`
18}
19
20func (op *SetStatusOperation) Id() entity.Id {
21 return dag.IdOperation(op, &op.OpBase)
22}
23
24func (op *SetStatusOperation) Apply(snapshot *Snapshot) {
25 snapshot.Status = op.Status
26 snapshot.addActor(op.Author())
27
28 item := &SetStatusTimelineItem{
29 id: op.Id(),
30 Author: op.Author(),
31 UnixTime: timestamp.Timestamp(op.UnixTime),
32 Status: op.Status,
33 }
34
35 snapshot.Timeline = append(snapshot.Timeline, item)
36}
37
38func (op *SetStatusOperation) Validate() error {
39 if err := op.OpBase.Validate(op, SetStatusOp); err != nil {
40 return err
41 }
42
43 if err := op.Status.Validate(); err != nil {
44 return errors.Wrap(err, "status")
45 }
46
47 return nil
48}
49
50func NewSetStatusOp(author identity.Interface, unixTime int64, status Status) *SetStatusOperation {
51 return &SetStatusOperation{
52 OpBase: dag.NewOpBase(SetStatusOp, author, unixTime),
53 Status: status,
54 }
55}
56
57type SetStatusTimelineItem struct {
58 id entity.Id
59 Author identity.Interface
60 UnixTime timestamp.Timestamp
61 Status Status
62}
63
64func (s SetStatusTimelineItem) Id() entity.Id {
65 return s.id
66}
67
68// IsAuthored is a sign post method for gqlgen
69func (s SetStatusTimelineItem) IsAuthored() {}
70
71// Open is a convenience function to change a bugs state to Open
72func Open(b Interface, author identity.Interface, unixTime int64, metadata map[string]string) (*SetStatusOperation, error) {
73 op := NewSetStatusOp(author, unixTime, OpenStatus)
74 for key, value := range metadata {
75 op.SetMetadata(key, value)
76 }
77 if err := op.Validate(); err != nil {
78 return nil, err
79 }
80 b.Append(op)
81 return op, nil
82}
83
84// Close is a convenience function to change a bugs state to Close
85func Close(b Interface, author identity.Interface, unixTime int64, metadata map[string]string) (*SetStatusOperation, error) {
86 op := NewSetStatusOp(author, unixTime, ClosedStatus)
87 for key, value := range metadata {
88 op.SetMetadata(key, value)
89 }
90 if err := op.Validate(); err != nil {
91 return nil, err
92 }
93 b.Append(op)
94 return op, nil
95}