aboutsummaryrefslogtreecommitdiffstats
path: root/bug/op_label_change.go
diff options
context:
space:
mode:
Diffstat (limited to 'bug/op_label_change.go')
-rw-r--r--bug/op_label_change.go81
1 files changed, 27 insertions, 54 deletions
diff --git a/bug/op_label_change.go b/bug/op_label_change.go
index 8b0e5ec8..8bcc7853 100644
--- a/bug/op_label_change.go
+++ b/bug/op_label_change.go
@@ -1,13 +1,13 @@
package bug
import (
- "encoding/json"
"fmt"
"sort"
"github.com/pkg/errors"
"github.com/MichaelMure/git-bug/entity"
+ "github.com/MichaelMure/git-bug/entity/dag"
"github.com/MichaelMure/git-bug/identity"
"github.com/MichaelMure/git-bug/util/timestamp"
)
@@ -16,18 +16,18 @@ var _ Operation = &LabelChangeOperation{}
// LabelChangeOperation define a Bug operation to add or remove labels
type LabelChangeOperation struct {
- OpBase
+ dag.OpBase
Added []Label `json:"added"`
Removed []Label `json:"removed"`
}
func (op *LabelChangeOperation) Id() entity.Id {
- return idOperation(op, &op.OpBase)
+ return dag.IdOperation(op, &op.OpBase)
}
-// Apply apply the operation
+// Apply applies the operation
func (op *LabelChangeOperation) Apply(snapshot *Snapshot) {
- snapshot.addActor(op.Author_)
+ snapshot.addActor(op.Author())
// Add in the set
AddLoop:
@@ -59,7 +59,7 @@ AddLoop:
item := &LabelChangeTimelineItem{
id: op.Id(),
- Author: op.Author_,
+ Author: op.Author(),
UnixTime: timestamp.Timestamp(op.UnixTime),
Added: op.Added,
Removed: op.Removed,
@@ -92,41 +92,9 @@ func (op *LabelChangeOperation) Validate() error {
return nil
}
-// UnmarshalJSON is a two step JSON unmarshalling
-// This workaround is necessary to avoid the inner OpBase.MarshalJSON
-// overriding the outer op's MarshalJSON
-func (op *LabelChangeOperation) UnmarshalJSON(data []byte) error {
- // Unmarshal OpBase and the op separately
-
- base := OpBase{}
- err := json.Unmarshal(data, &base)
- if err != nil {
- return err
- }
-
- aux := struct {
- Added []Label `json:"added"`
- Removed []Label `json:"removed"`
- }{}
-
- err = json.Unmarshal(data, &aux)
- if err != nil {
- return err
- }
-
- op.OpBase = base
- op.Added = aux.Added
- op.Removed = aux.Removed
-
- return nil
-}
-
-// Sign post method for gqlgen
-func (op *LabelChangeOperation) IsAuthored() {}
-
func NewLabelChangeOperation(author identity.Interface, unixTime int64, added, removed []Label) *LabelChangeOperation {
return &LabelChangeOperation{
- OpBase: newOpBase(LabelChangeOp, author, unixTime),
+ OpBase: dag.NewOpBase(LabelChangeOp, author, unixTime),
Added: added,
Removed: removed,
}
@@ -144,11 +112,11 @@ func (l LabelChangeTimelineItem) Id() entity.Id {
return l.id
}
-// Sign post method for gqlgen
-func (l *LabelChangeTimelineItem) IsAuthored() {}
+// IsAuthored is a sign post method for gqlgen
+func (l LabelChangeTimelineItem) IsAuthored() {}
-// ChangeLabels is a convenience function to apply the operation
-func ChangeLabels(b Interface, author identity.Interface, unixTime int64, add, remove []string) ([]LabelChangeResult, *LabelChangeOperation, error) {
+// ChangeLabels is a convenience function to change labels on a bug
+func ChangeLabels(b Interface, author identity.Interface, unixTime int64, add, remove []string, metadata map[string]string) ([]LabelChangeResult, *LabelChangeOperation, error) {
var added, removed []Label
var results []LabelChangeResult
@@ -196,23 +164,25 @@ func ChangeLabels(b Interface, author identity.Interface, unixTime int64, add, r
return results, nil, fmt.Errorf("no label added or removed")
}
- labelOp := NewLabelChangeOperation(author, unixTime, added, removed)
-
- if err := labelOp.Validate(); err != nil {
+ op := NewLabelChangeOperation(author, unixTime, added, removed)
+ for key, val := range metadata {
+ op.SetMetadata(key, val)
+ }
+ if err := op.Validate(); err != nil {
return nil, nil, err
}
- b.Append(labelOp)
+ b.Append(op)
- return results, labelOp, nil
+ return results, op, nil
}
// ForceChangeLabels is a convenience function to apply the operation
// The difference with ChangeLabels is that no checks of deduplications are done. You are entirely
-// responsible of what you are doing. In the general case, you want to use ChangeLabels instead.
+// responsible for what you are doing. In the general case, you want to use ChangeLabels instead.
// The intended use of this function is to allow importers to create legal but unexpected label changes,
// like removing a label with no information of when it was added before.
-func ForceChangeLabels(b Interface, author identity.Interface, unixTime int64, add, remove []string) (*LabelChangeOperation, error) {
+func ForceChangeLabels(b Interface, author identity.Interface, unixTime int64, add, remove []string, metadata map[string]string) (*LabelChangeOperation, error) {
added := make([]Label, len(add))
for i, str := range add {
added[i] = Label(str)
@@ -223,15 +193,18 @@ func ForceChangeLabels(b Interface, author identity.Interface, unixTime int64, a
removed[i] = Label(str)
}
- labelOp := NewLabelChangeOperation(author, unixTime, added, removed)
+ op := NewLabelChangeOperation(author, unixTime, added, removed)
- if err := labelOp.Validate(); err != nil {
+ for key, val := range metadata {
+ op.SetMetadata(key, val)
+ }
+ if err := op.Validate(); err != nil {
return nil, err
}
- b.Append(labelOp)
+ b.Append(op)
- return labelOp, nil
+ return op, nil
}
func labelExist(labels []Label, label Label) bool {