1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
|
package bug
import (
"fmt"
"sort"
"github.com/MichaelMure/git-bug/util/git"
"github.com/pkg/errors"
)
var _ Operation = &LabelChangeOperation{}
// LabelChangeOperation define a Bug operation to add or remove labels
type LabelChangeOperation struct {
*OpBase
Added []Label `json:"added"`
Removed []Label `json:"removed"`
}
func (op *LabelChangeOperation) base() *OpBase {
return op.OpBase
}
func (op *LabelChangeOperation) Hash() (git.Hash, error) {
return hashOperation(op)
}
// Apply apply the operation
func (op *LabelChangeOperation) Apply(snapshot *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])
})
hash, err := op.Hash()
if err != nil {
// Should never error unless a programming error happened
// (covered in OpBase.Validate())
panic(err)
}
item := &LabelChangeTimelineItem{
hash: hash,
Author: op.Author,
UnixTime: Timestamp(op.UnixTime),
Added: op.Added,
Removed: op.Removed,
}
snapshot.Timeline = append(snapshot.Timeline, item)
}
func (op *LabelChangeOperation) Validate() error {
if err := opBaseValidate(op, 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 Person, unixTime int64, added, removed []Label) *LabelChangeOperation {
return &LabelChangeOperation{
OpBase: newOpBase(LabelChangeOp, author, unixTime),
Added: added,
Removed: removed,
}
}
type LabelChangeTimelineItem struct {
hash git.Hash
Author Person
UnixTime Timestamp
Added []Label
Removed []Label
}
func (l LabelChangeTimelineItem) Hash() git.Hash {
return l.hash
}
// ChangeLabels is a convenience function to apply the operation
func ChangeLabels(b Interface, author Person, unixTime int64, add, remove []string) ([]LabelChangeResult, error) {
var added, removed []Label
var results []LabelChangeResult
snap := b.Compile()
for _, str := range add {
label := 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 := 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 []Label, label 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 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))
}
}
|