1package bug
  2
  3import (
  4	"encoding/json"
  5	"fmt"
  6	"sort"
  7
  8	"github.com/MichaelMure/git-bug/identity"
  9	"github.com/MichaelMure/git-bug/util/timestamp"
 10
 11	"github.com/MichaelMure/git-bug/util/git"
 12	"github.com/pkg/errors"
 13)
 14
 15var _ Operation = &LabelChangeOperation{}
 16
 17// LabelChangeOperation define a Bug operation to add or remove labels
 18type LabelChangeOperation struct {
 19	OpBase
 20	Added   []Label
 21	Removed []Label
 22}
 23
 24func (op *LabelChangeOperation) base() *OpBase {
 25	return &op.OpBase
 26}
 27
 28func (op *LabelChangeOperation) Hash() (git.Hash, error) {
 29	return hashOperation(op)
 30}
 31
 32// Apply apply the operation
 33func (op *LabelChangeOperation) Apply(snapshot *Snapshot) {
 34	snapshot.addActor(op.Author)
 35
 36	// Add in the set
 37AddLoop:
 38	for _, added := range op.Added {
 39		for _, label := range snapshot.Labels {
 40			if label == added {
 41				// Already exist
 42				continue AddLoop
 43			}
 44		}
 45
 46		snapshot.Labels = append(snapshot.Labels, added)
 47	}
 48
 49	// Remove in the set
 50	for _, removed := range op.Removed {
 51		for i, label := range snapshot.Labels {
 52			if label == removed {
 53				snapshot.Labels[i] = snapshot.Labels[len(snapshot.Labels)-1]
 54				snapshot.Labels = snapshot.Labels[:len(snapshot.Labels)-1]
 55			}
 56		}
 57	}
 58
 59	// Sort
 60	sort.Slice(snapshot.Labels, func(i, j int) bool {
 61		return string(snapshot.Labels[i]) < string(snapshot.Labels[j])
 62	})
 63
 64	hash, err := op.Hash()
 65	if err != nil {
 66		// Should never error unless a programming error happened
 67		// (covered in OpBase.Validate())
 68		panic(err)
 69	}
 70
 71	item := &LabelChangeTimelineItem{
 72		hash:     hash,
 73		Author:   op.Author,
 74		UnixTime: timestamp.Timestamp(op.UnixTime),
 75		Added:    op.Added,
 76		Removed:  op.Removed,
 77	}
 78
 79	snapshot.Timeline = append(snapshot.Timeline, item)
 80}
 81
 82func (op *LabelChangeOperation) Validate() error {
 83	if err := opBaseValidate(op, LabelChangeOp); err != nil {
 84		return err
 85	}
 86
 87	for _, l := range op.Added {
 88		if err := l.Validate(); err != nil {
 89			return errors.Wrap(err, "added label")
 90		}
 91	}
 92
 93	for _, l := range op.Removed {
 94		if err := l.Validate(); err != nil {
 95			return errors.Wrap(err, "removed label")
 96		}
 97	}
 98
 99	if len(op.Added)+len(op.Removed) <= 0 {
100		return fmt.Errorf("no label change")
101	}
102
103	return nil
104}
105
106// Workaround to avoid the inner OpBase.MarshalJSON overriding the outer op
107// MarshalJSON
108func (op *LabelChangeOperation) MarshalJSON() ([]byte, error) {
109	base, err := json.Marshal(op.OpBase)
110	if err != nil {
111		return nil, err
112	}
113
114	// revert back to a flat map to be able to add our own fields
115	var data map[string]interface{}
116	if err := json.Unmarshal(base, &data); err != nil {
117		return nil, err
118	}
119
120	data["added"] = op.Added
121	data["removed"] = op.Removed
122
123	return json.Marshal(data)
124}
125
126// Workaround to avoid the inner OpBase.MarshalJSON overriding the outer op
127// MarshalJSON
128func (op *LabelChangeOperation) UnmarshalJSON(data []byte) error {
129	// Unmarshal OpBase and the op separately
130
131	base := OpBase{}
132	err := json.Unmarshal(data, &base)
133	if err != nil {
134		return err
135	}
136
137	aux := struct {
138		Added   []Label `json:"added"`
139		Removed []Label `json:"removed"`
140	}{}
141
142	err = json.Unmarshal(data, &aux)
143	if err != nil {
144		return err
145	}
146
147	op.OpBase = base
148	op.Added = aux.Added
149	op.Removed = aux.Removed
150
151	return nil
152}
153
154// Sign post method for gqlgen
155func (op *LabelChangeOperation) IsAuthored() {}
156
157func NewLabelChangeOperation(author identity.Interface, unixTime int64, added, removed []Label) *LabelChangeOperation {
158	return &LabelChangeOperation{
159		OpBase:  newOpBase(LabelChangeOp, author, unixTime),
160		Added:   added,
161		Removed: removed,
162	}
163}
164
165type LabelChangeTimelineItem struct {
166	hash     git.Hash
167	Author   identity.Interface
168	UnixTime timestamp.Timestamp
169	Added    []Label
170	Removed  []Label
171}
172
173func (l LabelChangeTimelineItem) Hash() git.Hash {
174	return l.hash
175}
176
177// ChangeLabels is a convenience function to apply the operation
178func ChangeLabels(b Interface, author identity.Interface, unixTime int64, add, remove []string) ([]LabelChangeResult, *LabelChangeOperation, error) {
179	var added, removed []Label
180	var results []LabelChangeResult
181
182	snap := b.Compile()
183
184	for _, str := range add {
185		label := Label(str)
186
187		// check for duplicate
188		if labelExist(added, label) {
189			results = append(results, LabelChangeResult{Label: label, Status: LabelChangeDuplicateInOp})
190			continue
191		}
192
193		// check that the label doesn't already exist
194		if labelExist(snap.Labels, label) {
195			results = append(results, LabelChangeResult{Label: label, Status: LabelChangeAlreadySet})
196			continue
197		}
198
199		added = append(added, label)
200		results = append(results, LabelChangeResult{Label: label, Status: LabelChangeAdded})
201	}
202
203	for _, str := range remove {
204		label := Label(str)
205
206		// check for duplicate
207		if labelExist(removed, label) {
208			results = append(results, LabelChangeResult{Label: label, Status: LabelChangeDuplicateInOp})
209			continue
210		}
211
212		// check that the label actually exist
213		if !labelExist(snap.Labels, label) {
214			results = append(results, LabelChangeResult{Label: label, Status: LabelChangeDoesntExist})
215			continue
216		}
217
218		removed = append(removed, label)
219		results = append(results, LabelChangeResult{Label: label, Status: LabelChangeRemoved})
220	}
221
222	if len(added) == 0 && len(removed) == 0 {
223		return results, nil, fmt.Errorf("no label added or removed")
224	}
225
226	labelOp := NewLabelChangeOperation(author, unixTime, added, removed)
227
228	if err := labelOp.Validate(); err != nil {
229		return nil, nil, err
230	}
231
232	b.Append(labelOp)
233
234	return results, labelOp, nil
235}
236
237func labelExist(labels []Label, label Label) bool {
238	for _, l := range labels {
239		if l == label {
240			return true
241		}
242	}
243
244	return false
245}
246
247type LabelChangeStatus int
248
249const (
250	_ LabelChangeStatus = iota
251	LabelChangeAdded
252	LabelChangeRemoved
253	LabelChangeDuplicateInOp
254	LabelChangeAlreadySet
255	LabelChangeDoesntExist
256)
257
258type LabelChangeResult struct {
259	Label  Label
260	Status LabelChangeStatus
261}
262
263func (l LabelChangeResult) String() string {
264	switch l.Status {
265	case LabelChangeAdded:
266		return fmt.Sprintf("label %s added", l.Label)
267	case LabelChangeRemoved:
268		return fmt.Sprintf("label %s removed", l.Label)
269	case LabelChangeDuplicateInOp:
270		return fmt.Sprintf("label %s is a duplicate", l.Label)
271	case LabelChangeAlreadySet:
272		return fmt.Sprintf("label %s was already set", l.Label)
273	case LabelChangeDoesntExist:
274		return fmt.Sprintf("label %s doesn't exist on this bug", l.Label)
275	default:
276		panic(fmt.Sprintf("unknown label change status %v", l.Status))
277	}
278}