1package operations
2
3import (
4 "github.com/MichaelMure/git-bug/bug"
5 "sort"
6)
7
8// LabelChangeOperation will add or remove a set of labels
9
10var _ bug.Operation = LabelChangeOperation{}
11
12type LabelChangeOperation struct {
13 bug.OpBase
14 Added []bug.Label
15 Removed []bug.Label
16}
17
18func NewLabelChangeOperation(author bug.Person, added, removed []bug.Label) LabelChangeOperation {
19 return LabelChangeOperation{
20 OpBase: bug.NewOpBase(bug.LabelChangeOp, author),
21 Added: added,
22 Removed: removed,
23 }
24}
25
26func (op LabelChangeOperation) Apply(snapshot bug.Snapshot) bug.Snapshot {
27 // Add in the set
28AddLoop:
29 for _, added := range op.Added {
30 for _, label := range snapshot.Labels {
31 if label == added {
32 // Already exist
33 continue AddLoop
34 }
35 }
36
37 snapshot.Labels = append(snapshot.Labels, added)
38 }
39
40 // Remove in the set
41 for _, removed := range op.Removed {
42 for i, label := range snapshot.Labels {
43 if label == removed {
44 snapshot.Labels[i] = snapshot.Labels[len(snapshot.Labels)-1]
45 snapshot.Labels = snapshot.Labels[:len(snapshot.Labels)-1]
46 }
47 }
48 }
49
50 // Sort
51 sort.Slice(snapshot.Labels, func(i, j int) bool {
52 return string(snapshot.Labels[i]) < string(snapshot.Labels[j])
53 })
54
55 return snapshot
56}