2018-09-28 21:39:39 +03:00
|
|
|
package bug
|
|
|
|
|
|
|
|
import (
|
2019-01-19 21:23:31 +03:00
|
|
|
"encoding/json"
|
|
|
|
|
2019-08-07 16:31:38 +03:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
|
2019-08-11 15:08:03 +03:00
|
|
|
"github.com/MichaelMure/git-bug/entity"
|
2018-11-21 20:56:12 +03:00
|
|
|
"github.com/MichaelMure/git-bug/identity"
|
2019-02-25 01:05:03 +03:00
|
|
|
"github.com/MichaelMure/git-bug/util/timestamp"
|
2018-09-28 21:39:39 +03:00
|
|
|
)
|
|
|
|
|
2018-09-29 21:41:19 +03:00
|
|
|
var _ Operation = &SetStatusOperation{}
|
2018-09-28 21:39:39 +03:00
|
|
|
|
2018-09-29 21:41:19 +03:00
|
|
|
// SetStatusOperation will change the status of a bug
|
2018-09-28 21:39:39 +03:00
|
|
|
type SetStatusOperation struct {
|
2018-10-01 12:37:17 +03:00
|
|
|
OpBase
|
2019-08-11 15:08:03 +03:00
|
|
|
Status Status `json:"status"`
|
2018-09-28 21:39:39 +03:00
|
|
|
}
|
|
|
|
|
2020-02-03 23:03:48 +03:00
|
|
|
// Sign-post method for gqlgen
|
|
|
|
func (op *SetStatusOperation) IsOperation() {}
|
|
|
|
|
2018-09-29 21:41:19 +03:00
|
|
|
func (op *SetStatusOperation) base() *OpBase {
|
2018-10-01 12:37:17 +03:00
|
|
|
return &op.OpBase
|
2018-09-28 21:39:39 +03:00
|
|
|
}
|
2018-09-29 00:51:47 +03:00
|
|
|
|
2019-08-11 15:08:03 +03:00
|
|
|
func (op *SetStatusOperation) Id() entity.Id {
|
2019-08-07 16:31:38 +03:00
|
|
|
return idOperation(op)
|
2018-09-29 00:51:47 +03:00
|
|
|
}
|
2018-09-28 21:39:39 +03:00
|
|
|
|
2018-09-29 21:41:19 +03:00
|
|
|
func (op *SetStatusOperation) Apply(snapshot *Snapshot) {
|
2018-09-28 21:39:39 +03:00
|
|
|
snapshot.Status = op.Status
|
2019-03-31 23:32:35 +03:00
|
|
|
snapshot.addActor(op.Author)
|
2018-09-30 18:15:54 +03:00
|
|
|
|
|
|
|
item := &SetStatusTimelineItem{
|
2019-08-11 15:08:03 +03:00
|
|
|
id: op.Id(),
|
2018-09-30 18:15:54 +03:00
|
|
|
Author: op.Author,
|
2019-02-25 01:05:03 +03:00
|
|
|
UnixTime: timestamp.Timestamp(op.UnixTime),
|
2018-09-30 18:15:54 +03:00
|
|
|
Status: op.Status,
|
|
|
|
}
|
|
|
|
|
|
|
|
snapshot.Timeline = append(snapshot.Timeline, item)
|
2018-09-28 21:39:39 +03:00
|
|
|
}
|
|
|
|
|
2018-09-29 21:41:19 +03:00
|
|
|
func (op *SetStatusOperation) Validate() error {
|
2018-09-28 21:39:39 +03:00
|
|
|
if err := opBaseValidate(op, SetStatusOp); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := op.Status.Validate(); err != nil {
|
|
|
|
return errors.Wrap(err, "status")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-08-11 15:08:03 +03:00
|
|
|
// UnmarshalJSON is a two step JSON unmarshaling
|
|
|
|
// This workaround is necessary to avoid the inner OpBase.MarshalJSON
|
|
|
|
// overriding the outer op's MarshalJSON
|
2019-01-19 21:23:31 +03:00
|
|
|
func (op *SetStatusOperation) UnmarshalJSON(data []byte) error {
|
|
|
|
// Unmarshal OpBase and the op separately
|
|
|
|
|
|
|
|
base := OpBase{}
|
|
|
|
err := json.Unmarshal(data, &base)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
aux := struct {
|
|
|
|
Status Status `json:"status"`
|
|
|
|
}{}
|
|
|
|
|
|
|
|
err = json.Unmarshal(data, &aux)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
op.OpBase = base
|
|
|
|
op.Status = aux.Status
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-12-23 19:11:37 +03:00
|
|
|
// Sign post method for gqlgen
|
|
|
|
func (op *SetStatusOperation) IsAuthored() {}
|
|
|
|
|
2018-11-21 20:56:12 +03:00
|
|
|
func NewSetStatusOp(author identity.Interface, unixTime int64, status Status) *SetStatusOperation {
|
2018-09-29 21:41:19 +03:00
|
|
|
return &SetStatusOperation{
|
2018-09-28 21:39:39 +03:00
|
|
|
OpBase: newOpBase(SetStatusOp, author, unixTime),
|
|
|
|
Status: status,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-30 18:15:54 +03:00
|
|
|
type SetStatusTimelineItem struct {
|
2019-08-11 15:08:03 +03:00
|
|
|
id entity.Id
|
2018-11-21 20:56:12 +03:00
|
|
|
Author identity.Interface
|
2019-02-25 01:05:03 +03:00
|
|
|
UnixTime timestamp.Timestamp
|
2018-09-30 18:15:54 +03:00
|
|
|
Status Status
|
|
|
|
}
|
|
|
|
|
2019-08-11 15:08:03 +03:00
|
|
|
func (s SetStatusTimelineItem) Id() entity.Id {
|
2019-08-07 16:31:38 +03:00
|
|
|
return s.id
|
2018-09-30 18:15:54 +03:00
|
|
|
}
|
|
|
|
|
2019-06-23 19:32:22 +03:00
|
|
|
// Sign post method for gqlgen
|
|
|
|
func (s *SetStatusTimelineItem) IsAuthored() {}
|
|
|
|
|
2018-09-28 21:39:39 +03:00
|
|
|
// Convenience function to apply the operation
|
2018-11-21 20:56:12 +03:00
|
|
|
func Open(b Interface, author identity.Interface, unixTime int64) (*SetStatusOperation, error) {
|
2018-09-28 21:39:39 +03:00
|
|
|
op := NewSetStatusOp(author, unixTime, OpenStatus)
|
|
|
|
if err := op.Validate(); err != nil {
|
2018-10-02 00:31:16 +03:00
|
|
|
return nil, err
|
2018-09-28 21:39:39 +03:00
|
|
|
}
|
|
|
|
b.Append(op)
|
2018-10-02 00:31:16 +03:00
|
|
|
return op, nil
|
2018-09-28 21:39:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Convenience function to apply the operation
|
2018-11-21 20:56:12 +03:00
|
|
|
func Close(b Interface, author identity.Interface, unixTime int64) (*SetStatusOperation, error) {
|
2018-09-28 21:39:39 +03:00
|
|
|
op := NewSetStatusOp(author, unixTime, ClosedStatus)
|
|
|
|
if err := op.Validate(); err != nil {
|
2018-10-02 00:31:16 +03:00
|
|
|
return nil, err
|
2018-09-28 21:39:39 +03:00
|
|
|
}
|
|
|
|
b.Append(op)
|
2018-10-02 00:31:16 +03:00
|
|
|
return op, nil
|
2018-09-28 21:39:39 +03:00
|
|
|
}
|