label_change.go

  1package operations
  2
  3import (
  4	"fmt"
  5	"io"
  6	"io/ioutil"
  7	"sort"
  8
  9	"github.com/MichaelMure/git-bug/bug"
 10)
 11
 12var _ bug.Operation = LabelChangeOperation{}
 13
 14// LabelChangeOperation define a Bug operation to add or remove labels
 15type LabelChangeOperation struct {
 16	bug.OpBase
 17	Added   []bug.Label
 18	Removed []bug.Label
 19}
 20
 21// Apply apply the operation
 22func (op LabelChangeOperation) Apply(snapshot bug.Snapshot) bug.Snapshot {
 23	// Add in the set
 24AddLoop:
 25	for _, added := range op.Added {
 26		for _, label := range snapshot.Labels {
 27			if label == added {
 28				// Already exist
 29				continue AddLoop
 30			}
 31		}
 32
 33		snapshot.Labels = append(snapshot.Labels, added)
 34	}
 35
 36	// Remove in the set
 37	for _, removed := range op.Removed {
 38		for i, label := range snapshot.Labels {
 39			if label == removed {
 40				snapshot.Labels[i] = snapshot.Labels[len(snapshot.Labels)-1]
 41				snapshot.Labels = snapshot.Labels[:len(snapshot.Labels)-1]
 42			}
 43		}
 44	}
 45
 46	// Sort
 47	sort.Slice(snapshot.Labels, func(i, j int) bool {
 48		return string(snapshot.Labels[i]) < string(snapshot.Labels[j])
 49	})
 50
 51	return snapshot
 52}
 53
 54func NewLabelChangeOperation(author bug.Person, added, removed []bug.Label) LabelChangeOperation {
 55	return LabelChangeOperation{
 56		OpBase:  bug.NewOpBase(bug.LabelChangeOp, author),
 57		Added:   added,
 58		Removed: removed,
 59	}
 60}
 61
 62// ChangeLabels is a convenience function to apply the operation
 63func ChangeLabels(out io.Writer, b bug.Interface, author bug.Person, add, remove []string) error {
 64	// TODO: return a channel of result (like MergeAll) instead of formatting the result for the upper layers
 65	var added, removed []bug.Label
 66
 67	if out == nil {
 68		out = ioutil.Discard
 69	}
 70
 71	snap := b.Compile()
 72
 73	for _, str := range add {
 74		label := bug.Label(str)
 75
 76		// check for duplicate
 77		if labelExist(added, label) {
 78			fmt.Fprintf(out, "label \"%s\" is a duplicate\n", str)
 79			continue
 80		}
 81
 82		// check that the label doesn't already exist
 83		if labelExist(snap.Labels, label) {
 84			fmt.Fprintf(out, "label \"%s\" is already set on this bug\n", str)
 85			continue
 86		}
 87
 88		added = append(added, label)
 89	}
 90
 91	for _, str := range remove {
 92		label := bug.Label(str)
 93
 94		// check for duplicate
 95		if labelExist(removed, label) {
 96			fmt.Fprintf(out, "label \"%s\" is a duplicate\n", str)
 97			continue
 98		}
 99
100		// check that the label actually exist
101		if !labelExist(snap.Labels, label) {
102			fmt.Fprintf(out, "label \"%s\" doesn't exist on this bug\n", str)
103			continue
104		}
105
106		removed = append(removed, label)
107	}
108
109	if len(added) == 0 && len(removed) == 0 {
110		return fmt.Errorf("no label added or removed")
111	}
112
113	labelOp := NewLabelChangeOperation(author, added, removed)
114
115	b.Append(labelOp)
116
117	return nil
118}
119
120func labelExist(labels []bug.Label, label bug.Label) bool {
121	for _, l := range labels {
122		if l == label {
123			return true
124		}
125	}
126
127	return false
128}