2018-07-13 17:13:40 +03:00
|
|
|
package bug
|
|
|
|
|
2018-07-13 22:21:24 +03:00
|
|
|
import (
|
2018-09-12 17:57:04 +03:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2018-08-13 19:32:11 +03:00
|
|
|
|
2018-09-15 14:15:00 +03:00
|
|
|
"github.com/pkg/errors"
|
2020-07-01 20:39:02 +03:00
|
|
|
|
2020-12-10 16:12:45 +03:00
|
|
|
"github.com/MichaelMure/git-bug/entity"
|
2020-07-01 20:39:02 +03:00
|
|
|
"github.com/MichaelMure/git-bug/repository"
|
2018-07-13 22:21:24 +03:00
|
|
|
)
|
|
|
|
|
2020-07-01 21:00:53 +03:00
|
|
|
// 1: original format
|
|
|
|
// 2: no more legacy identities
|
|
|
|
const formatVersion = 2
|
2018-09-12 17:57:04 +03:00
|
|
|
|
2018-07-13 17:13:40 +03:00
|
|
|
// OperationPack represent an ordered set of operation to apply
|
|
|
|
// to a Bug. These operations are stored in a single Git commit.
|
|
|
|
//
|
|
|
|
// These commits will be linked together in a linear chain of commits
|
|
|
|
// inside Git to form the complete ordered chain of operation to
|
|
|
|
// apply to get the final state of the Bug
|
|
|
|
type OperationPack struct {
|
2018-07-14 23:17:37 +03:00
|
|
|
Operations []Operation
|
2018-07-17 02:52:56 +03:00
|
|
|
|
2018-11-21 20:56:12 +03:00
|
|
|
// Private field so not serialized
|
2020-07-01 20:39:02 +03:00
|
|
|
commitHash repository.Hash
|
2018-07-13 22:21:24 +03:00
|
|
|
}
|
|
|
|
|
2018-09-12 17:57:04 +03:00
|
|
|
func (opp *OperationPack) MarshalJSON() ([]byte, error) {
|
|
|
|
return json.Marshal(struct {
|
|
|
|
Version uint `json:"version"`
|
|
|
|
Operations []Operation `json:"ops"`
|
|
|
|
}{
|
|
|
|
Version: formatVersion,
|
|
|
|
Operations: opp.Operations,
|
|
|
|
})
|
|
|
|
}
|
2018-07-14 23:17:37 +03:00
|
|
|
|
2018-09-12 17:57:04 +03:00
|
|
|
func (opp *OperationPack) UnmarshalJSON(data []byte) error {
|
|
|
|
aux := struct {
|
|
|
|
Version uint `json:"version"`
|
|
|
|
Operations []json.RawMessage `json:"ops"`
|
|
|
|
}{}
|
|
|
|
|
|
|
|
if err := json.Unmarshal(data, &aux); err != nil {
|
|
|
|
return err
|
2018-07-14 23:17:37 +03:00
|
|
|
}
|
|
|
|
|
2020-07-01 21:00:53 +03:00
|
|
|
if aux.Version < formatVersion {
|
2020-12-10 16:12:45 +03:00
|
|
|
return entity.NewErrOldFormatVersion(aux.Version)
|
2020-07-01 21:00:53 +03:00
|
|
|
}
|
|
|
|
if aux.Version > formatVersion {
|
2020-12-10 16:12:45 +03:00
|
|
|
return entity.NewErrNewFormatVersion(aux.Version)
|
2018-09-12 17:57:04 +03:00
|
|
|
}
|
2018-07-13 22:21:24 +03:00
|
|
|
|
2018-09-12 17:57:04 +03:00
|
|
|
for _, raw := range aux.Operations {
|
|
|
|
var t struct {
|
|
|
|
OperationType OperationType `json:"type"`
|
|
|
|
}
|
2018-07-14 23:17:37 +03:00
|
|
|
|
2018-09-12 17:57:04 +03:00
|
|
|
if err := json.Unmarshal(raw, &t); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-07-14 23:17:37 +03:00
|
|
|
|
2018-11-21 20:56:12 +03:00
|
|
|
// delegate to specialized unmarshal function
|
2018-09-28 21:39:39 +03:00
|
|
|
op, err := opp.unmarshalOp(raw, t.OperationType)
|
|
|
|
if err != nil {
|
2018-09-12 17:57:04 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-09-28 21:39:39 +03:00
|
|
|
opp.Operations = append(opp.Operations, op)
|
2018-07-13 22:21:24 +03:00
|
|
|
}
|
|
|
|
|
2018-09-12 17:57:04 +03:00
|
|
|
return nil
|
2018-07-13 17:13:40 +03:00
|
|
|
}
|
|
|
|
|
2018-09-28 21:39:39 +03:00
|
|
|
func (opp *OperationPack) unmarshalOp(raw []byte, _type OperationType) (Operation, error) {
|
|
|
|
switch _type {
|
2018-11-21 20:56:12 +03:00
|
|
|
case AddCommentOp:
|
|
|
|
op := &AddCommentOperation{}
|
|
|
|
err := json.Unmarshal(raw, &op)
|
|
|
|
return op, err
|
2018-09-28 21:39:39 +03:00
|
|
|
case CreateOp:
|
2018-09-29 21:41:19 +03:00
|
|
|
op := &CreateOperation{}
|
2018-09-28 21:39:39 +03:00
|
|
|
err := json.Unmarshal(raw, &op)
|
|
|
|
return op, err
|
2018-11-21 20:56:12 +03:00
|
|
|
case EditCommentOp:
|
|
|
|
op := &EditCommentOperation{}
|
2018-09-28 21:39:39 +03:00
|
|
|
err := json.Unmarshal(raw, &op)
|
|
|
|
return op, err
|
2018-11-21 20:56:12 +03:00
|
|
|
case LabelChangeOp:
|
|
|
|
op := &LabelChangeOperation{}
|
2018-09-28 21:39:39 +03:00
|
|
|
err := json.Unmarshal(raw, &op)
|
|
|
|
return op, err
|
2018-11-21 20:56:12 +03:00
|
|
|
case NoOpOp:
|
|
|
|
op := &NoOpOperation{}
|
2018-09-28 21:39:39 +03:00
|
|
|
err := json.Unmarshal(raw, &op)
|
|
|
|
return op, err
|
2018-11-21 20:56:12 +03:00
|
|
|
case SetMetadataOp:
|
|
|
|
op := &SetMetadataOperation{}
|
2018-09-29 21:41:19 +03:00
|
|
|
err := json.Unmarshal(raw, &op)
|
|
|
|
return op, err
|
2018-11-21 20:56:12 +03:00
|
|
|
case SetStatusOp:
|
|
|
|
op := &SetStatusOperation{}
|
|
|
|
err := json.Unmarshal(raw, &op)
|
|
|
|
return op, err
|
|
|
|
case SetTitleOp:
|
|
|
|
op := &SetTitleOperation{}
|
2018-09-28 21:39:39 +03:00
|
|
|
err := json.Unmarshal(raw, &op)
|
|
|
|
return op, err
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("unknown operation type %v", _type)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-13 17:13:40 +03:00
|
|
|
// Append a new operation to the pack
|
|
|
|
func (opp *OperationPack) Append(op Operation) {
|
|
|
|
opp.Operations = append(opp.Operations, op)
|
|
|
|
}
|
|
|
|
|
2018-08-13 16:28:16 +03:00
|
|
|
// IsEmpty tell if the OperationPack is empty
|
2018-07-13 17:48:55 +03:00
|
|
|
func (opp *OperationPack) IsEmpty() bool {
|
|
|
|
return len(opp.Operations) == 0
|
|
|
|
}
|
|
|
|
|
2018-08-13 16:28:16 +03:00
|
|
|
// IsValid tell if the OperationPack is considered valid
|
2018-09-15 14:15:00 +03:00
|
|
|
func (opp *OperationPack) Validate() error {
|
|
|
|
if opp.IsEmpty() {
|
|
|
|
return fmt.Errorf("empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, op := range opp.Operations {
|
|
|
|
if err := op.Validate(); err != nil {
|
|
|
|
return errors.Wrap(err, "op")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2018-07-13 17:13:40 +03:00
|
|
|
}
|
2018-07-13 22:21:24 +03:00
|
|
|
|
2018-08-13 16:28:16 +03:00
|
|
|
// Write will serialize and store the OperationPack as a git blob and return
|
|
|
|
// its hash
|
2020-07-01 20:39:02 +03:00
|
|
|
func (opp *OperationPack) Write(repo repository.ClockedRepo) (repository.Hash, error) {
|
2019-01-20 17:41:27 +03:00
|
|
|
// make sure we don't write invalid data
|
|
|
|
err := opp.Validate()
|
|
|
|
if err != nil {
|
|
|
|
return "", errors.Wrap(err, "validation error")
|
|
|
|
}
|
|
|
|
|
2019-01-19 21:23:31 +03:00
|
|
|
// First, make sure that all the identities are properly Commit as well
|
2020-09-16 17:22:02 +03:00
|
|
|
// TODO: this might be downgraded to "make sure it exist in git" but then, what make
|
|
|
|
// sure no data is lost on identities ?
|
2019-01-19 21:23:31 +03:00
|
|
|
for _, op := range opp.Operations {
|
2020-09-16 17:22:02 +03:00
|
|
|
if op.base().Author.NeedCommit() {
|
|
|
|
return "", fmt.Errorf("identity need commmit")
|
2019-01-19 21:23:31 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-12 17:57:04 +03:00
|
|
|
data, err := json.Marshal(opp)
|
2018-07-13 22:21:24 +03:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
hash, err := repo.StoreData(data)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return hash, nil
|
|
|
|
}
|
2018-07-17 02:52:56 +03:00
|
|
|
|
|
|
|
// Make a deep copy
|
|
|
|
func (opp *OperationPack) Clone() OperationPack {
|
|
|
|
|
|
|
|
clone := OperationPack{
|
|
|
|
Operations: make([]Operation, len(opp.Operations)),
|
|
|
|
commitHash: opp.commitHash,
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, op := range opp.Operations {
|
|
|
|
clone.Operations[i] = op
|
|
|
|
}
|
|
|
|
|
|
|
|
return clone
|
|
|
|
}
|