aboutsummaryrefslogtreecommitdiffstats
path: root/operations/label_change.go
diff options
context:
space:
mode:
authorMichael Muré <batolettre@gmail.com>2018-09-28 20:39:39 +0200
committerMichael Muré <batolettre@gmail.com>2018-09-29 00:51:54 +0200
commit1bf268cebc84a9de1e538cbb54bcc0f434022192 (patch)
treedaeb92cd6b15d56a7a7102f95b73756e5b9597d0 /operations/label_change.go
parent8af6f7d98f2fd98f85d6a17bcda49983c272cf48 (diff)
downloadgit-bug-1bf268cebc84a9de1e538cbb54bcc0f434022192.tar.gz
merge package operations into bug, they are tightly coupled anyway
Diffstat (limited to 'operations/label_change.go')
-rw-r--r--operations/label_change.go186
1 files changed, 0 insertions, 186 deletions
diff --git a/operations/label_change.go b/operations/label_change.go
deleted file mode 100644
index 83e3e692..00000000
--- a/operations/label_change.go
+++ /dev/null
@@ -1,186 +0,0 @@
-package operations
-
-import (
- "fmt"
- "sort"
-
- "github.com/MichaelMure/git-bug/bug"
- "github.com/pkg/errors"
-)
-
-var _ bug.Operation = LabelChangeOperation{}
-
-// LabelChangeOperation define a Bug operation to add or remove labels
-type LabelChangeOperation struct {
- *bug.OpBase
- Added []bug.Label `json:"added"`
- Removed []bug.Label `json:"removed"`
-}
-
-// Apply apply the operation
-func (op LabelChangeOperation) Apply(snapshot bug.Snapshot) bug.Snapshot {
- // Add in the set
-AddLoop:
- for _, added := range op.Added {
- for _, label := range snapshot.Labels {
- if label == added {
- // Already exist
- continue AddLoop
- }
- }
-
- snapshot.Labels = append(snapshot.Labels, added)
- }
-
- // Remove in the set
- for _, removed := range op.Removed {
- for i, label := range snapshot.Labels {
- if label == removed {
- snapshot.Labels[i] = snapshot.Labels[len(snapshot.Labels)-1]
- snapshot.Labels = snapshot.Labels[:len(snapshot.Labels)-1]
- }
- }
- }
-
- // Sort
- sort.Slice(snapshot.Labels, func(i, j int) bool {
- return string(snapshot.Labels[i]) < string(snapshot.Labels[j])
- })
-
- return snapshot
-}
-
-func (op LabelChangeOperation) Validate() error {
- if err := bug.OpBaseValidate(op, bug.LabelChangeOp); err != nil {
- return err
- }
-
- for _, l := range op.Added {
- if err := l.Validate(); err != nil {
- return errors.Wrap(err, "added label")
- }
- }
-
- for _, l := range op.Removed {
- if err := l.Validate(); err != nil {
- return errors.Wrap(err, "removed label")
- }
- }
-
- if len(op.Added)+len(op.Removed) <= 0 {
- return fmt.Errorf("no label change")
- }
-
- return nil
-}
-
-func NewLabelChangeOperation(author bug.Person, unixTime int64, added, removed []bug.Label) LabelChangeOperation {
- return LabelChangeOperation{
- OpBase: bug.NewOpBase(bug.LabelChangeOp, author, unixTime),
- Added: added,
- Removed: removed,
- }
-}
-
-// ChangeLabels is a convenience function to apply the operation
-func ChangeLabels(b bug.Interface, author bug.Person, unixTime int64, add, remove []string) ([]LabelChangeResult, error) {
- var added, removed []bug.Label
- var results []LabelChangeResult
-
- snap := b.Compile()
-
- for _, str := range add {
- label := bug.Label(str)
-
- // check for duplicate
- if labelExist(added, label) {
- results = append(results, LabelChangeResult{Label: label, Status: LabelChangeDuplicateInOp})
- continue
- }
-
- // check that the label doesn't already exist
- if labelExist(snap.Labels, label) {
- results = append(results, LabelChangeResult{Label: label, Status: LabelChangeAlreadySet})
- continue
- }
-
- added = append(added, label)
- results = append(results, LabelChangeResult{Label: label, Status: LabelChangeAdded})
- }
-
- for _, str := range remove {
- label := bug.Label(str)
-
- // check for duplicate
- if labelExist(removed, label) {
- results = append(results, LabelChangeResult{Label: label, Status: LabelChangeDuplicateInOp})
- continue
- }
-
- // check that the label actually exist
- if !labelExist(snap.Labels, label) {
- results = append(results, LabelChangeResult{Label: label, Status: LabelChangeDoesntExist})
- continue
- }
-
- removed = append(removed, label)
- results = append(results, LabelChangeResult{Label: label, Status: LabelChangeRemoved})
- }
-
- if len(added) == 0 && len(removed) == 0 {
- return results, fmt.Errorf("no label added or removed")
- }
-
- labelOp := NewLabelChangeOperation(author, unixTime, added, removed)
-
- if err := labelOp.Validate(); err != nil {
- return nil, err
- }
-
- b.Append(labelOp)
-
- return results, nil
-}
-
-func labelExist(labels []bug.Label, label bug.Label) bool {
- for _, l := range labels {
- if l == label {
- return true
- }
- }
-
- return false
-}
-
-type LabelChangeStatus int
-
-const (
- _ LabelChangeStatus = iota
- LabelChangeAdded
- LabelChangeRemoved
- LabelChangeDuplicateInOp
- LabelChangeAlreadySet
- LabelChangeDoesntExist
-)
-
-type LabelChangeResult struct {
- Label bug.Label
- Status LabelChangeStatus
-}
-
-func (l LabelChangeResult) String() string {
- switch l.Status {
- case LabelChangeAdded:
- return fmt.Sprintf("label %s added", l.Label)
- case LabelChangeRemoved:
- return fmt.Sprintf("label %s removed", l.Label)
- case LabelChangeDuplicateInOp:
- return fmt.Sprintf("label %s is a duplicate", l.Label)
- case LabelChangeAlreadySet:
- return fmt.Sprintf("label %s was already set", l.Label)
- case LabelChangeDoesntExist:
- return fmt.Sprintf("label %s doesn't exist on this bug", l.Label)
- default:
- panic(fmt.Sprintf("unknown label change status %v", l.Status))
- }
-}