op_set_status.go

 1package bug
 2
 3import (
 4	"github.com/MichaelMure/git-bug/identity"
 5	"github.com/MichaelMure/git-bug/util/git"
 6	"github.com/pkg/errors"
 7)
 8
 9var _ Operation = &SetStatusOperation{}
10
11// SetStatusOperation will change the status of a bug
12type SetStatusOperation struct {
13	OpBase
14	Status Status `json:"status"`
15}
16
17func (op *SetStatusOperation) base() *OpBase {
18	return &op.OpBase
19}
20
21func (op *SetStatusOperation) Hash() (git.Hash, error) {
22	return hashOperation(op)
23}
24
25func (op *SetStatusOperation) Apply(snapshot *Snapshot) {
26	snapshot.Status = op.Status
27
28	hash, err := op.Hash()
29	if err != nil {
30		// Should never error unless a programming error happened
31		// (covered in OpBase.Validate())
32		panic(err)
33	}
34
35	item := &SetStatusTimelineItem{
36		hash:     hash,
37		Author:   op.Author,
38		UnixTime: Timestamp(op.UnixTime),
39		Status:   op.Status,
40	}
41
42	snapshot.Timeline = append(snapshot.Timeline, item)
43}
44
45func (op *SetStatusOperation) Validate() error {
46	if err := opBaseValidate(op, SetStatusOp); err != nil {
47		return err
48	}
49
50	if err := op.Status.Validate(); err != nil {
51		return errors.Wrap(err, "status")
52	}
53
54	return nil
55}
56
57// Sign post method for gqlgen
58func (op *SetStatusOperation) IsAuthored() {}
59
60func NewSetStatusOp(author identity.Interface, unixTime int64, status Status) *SetStatusOperation {
61	return &SetStatusOperation{
62		OpBase: newOpBase(SetStatusOp, author, unixTime),
63		Status: status,
64	}
65}
66
67type SetStatusTimelineItem struct {
68	hash     git.Hash
69	Author   identity.Interface
70	UnixTime Timestamp
71	Status   Status
72}
73
74func (s SetStatusTimelineItem) Hash() git.Hash {
75	return s.hash
76}
77
78// Convenience function to apply the operation
79func Open(b Interface, author identity.Interface, unixTime int64) (*SetStatusOperation, error) {
80	op := NewSetStatusOp(author, unixTime, OpenStatus)
81	if err := op.Validate(); err != nil {
82		return nil, err
83	}
84	b.Append(op)
85	return op, nil
86}
87
88// Convenience function to apply the operation
89func Close(b Interface, author identity.Interface, unixTime int64) (*SetStatusOperation, error) {
90	op := NewSetStatusOp(author, unixTime, ClosedStatus)
91	if err := op.Validate(); err != nil {
92		return nil, err
93	}
94	b.Append(op)
95	return op, nil
96}