aboutsummaryrefslogtreecommitdiffstats
path: root/cache/bug_cache.go
blob: a248e872a28cea48875696a2739e861e7c64dd2e (plain) (blame)
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
package cache

import (
	"fmt"
	"sync"
	"time"

	"github.com/MichaelMure/git-bug/bug"
	"github.com/MichaelMure/git-bug/entity"
	"github.com/MichaelMure/git-bug/repository"
)

var ErrNoMatchingOp = fmt.Errorf("no matching operation found")

// BugCache is a wrapper around a Bug. It provide multiple functions:
//
// 1. Provide a higher level API to use than the raw API from Bug.
// 2. Maintain an up to date Snapshot available.
// 3. Deal with concurrency.
type BugCache struct {
	repoCache *RepoCache
	mu        sync.RWMutex
	bug       *bug.WithSnapshot
}

func NewBugCache(repoCache *RepoCache, b *bug.Bug) *BugCache {
	return &BugCache{
		repoCache: repoCache,
		bug:       &bug.WithSnapshot{Bug: b},
	}
}

func (c *BugCache) Snapshot() *bug.Snapshot {
	c.mu.RLock()
	defer c.mu.RUnlock()
	return c.bug.Snapshot()
}

func (c *BugCache) Id() entity.Id {
	return c.bug.Id()
}

func (c *BugCache) notifyUpdated() error {
	return c.repoCache.bugUpdated(c.bug.Id())
}

// ResolveOperationWithMetadata will find an operation that has the matching metadata
func (c *BugCache) ResolveOperationWithMetadata(key string, value string) (entity.Id, error) {
	c.mu.RLock()
	defer c.mu.RUnlock()
	// preallocate but empty
	matching := make([]entity.Id, 0, 5)

	for _, op := range c.bug.Operations() {
		opValue, ok := op.GetMetadata(key)
		if ok && value == opValue {
			matching = append(matching, op.Id())
		}
	}

	if len(matching) == 0 {
		return "", ErrNoMatchingOp
	}

	if len(matching) > 1 {
		return "", bug.NewErrMultipleMatchOp(matching)
	}

	return matching[0], nil
}

func (c *BugCache) AddComment(message string) (*bug.AddCommentOperation, error) {
	return c.AddCommentWithFiles(message, nil)
}

func (c *BugCache) AddCommentWithFiles(message string, files []repository.Hash) (*bug.AddCommentOperation, error) {
	author, err := c.repoCache.GetUserIdentity()
	if err != nil {
		return nil, err
	}

	return c.AddCommentRaw(author, time.Now().Unix(), message, files, nil)
}

func (c *BugCache) AddCommentRaw(author *IdentityCache, unixTime int64, message string, files []repository.Hash, metadata map[string]string) (*bug.AddCommentOperation, error) {
	c.mu.Lock()
	op, err := bug.AddCommentWithFiles(c.bug, author.Identity, unixTime, message, files)
	if err != nil {
		c.mu.Unlock()
		return nil, err
	}

	for key, value := range metadata {
		op.SetMetadata(key, value)
	}

	c.mu.Unlock()

	return op, c.notifyUpdated()
}

func (c *BugCache) ChangeLabels(added []string, removed []string) ([]bug.LabelChangeResult, *bug.LabelChangeOperation, error) {
	author, err := c.repoCache.GetUserIdentity()
	if err != nil {
		return nil, nil, err
	}

	return c.ChangeLabelsRaw(author, time.Now().Unix(), added, removed, nil)
}

func (c *BugCache) ChangeLabelsRaw(author *IdentityCache, unixTime int64, added []string, removed []string, metadata map[string]string) ([]bug.LabelChangeResult, *bug.LabelChangeOperation, error) {
	c.mu.Lock()
	changes, op, err := bug.ChangeLabels(c.bug, author.Identity, unixTime, added, removed)
	if err != nil {
		c.mu.Unlock()
		return changes, nil, err
	}

	for key, value := range metadata {
		op.SetMetadata(key, value)
	}

	c.mu.Unlock()

	err = c.notifyUpdated()
	if err != nil {
		return nil, nil, err
	}

	return changes, op, nil
}

func (c *BugCache) ForceChangeLabels(added []string, removed []string) (*bug.LabelChangeOperation, error) {
	author, err := c.repoCache.GetUserIdentity()
	if err != nil {
		return nil, err
	}

	return c.ForceChangeLabelsRaw(author, time.Now().Unix(), added, removed, nil)
}

func (c *BugCache) ForceChangeLabelsRaw(author *IdentityCache, unixTime int64, added []string, removed []string, metadata map[string]string) (*bug.LabelChangeOperation, error) {
	c.mu.Lock()
	op, err := bug.ForceChangeLabels(c.bug, author.Identity, unixTime, added, removed)
	if err != nil {
		c.mu.Unlock()
		return nil, err
	}

	for key, value := range metadata {
		op.SetMetadata(key, value)
	}

	c.mu.Unlock()
	err = c.notifyUpdated()
	if err != nil {
		return nil, err
	}

	return op, nil
}

func (c *BugCache) Open() (*bug.SetStatusOperation, error) {
	author, err := c.repoCache.GetUserIdentity()
	if err != nil {
		return nil, err
	}

	return c.OpenRaw(author, time.Now().Unix(), nil)
}

func (c *BugCache) OpenRaw(author *IdentityCache, unixTime int64, metadata map[string]string) (*bug.SetStatusOperation, error) {
	c.mu.Lock()
	op, err := bug.Open(c.bug, author.Identity, unixTime)
	if err != nil {
		c.mu.Unlock()
		return nil, err
	}

	for key, value := range metadata {
		op.SetMetadata(key, value)
	}

	c.mu.Unlock()
	return op, c.notifyUpdated()
}

func (c *BugCache) Close() (*bug.SetStatusOperation, error) {
	author, err := c.repoCache.GetUserIdentity()
	if err != nil {
		return nil, err
	}

	return c.CloseRaw(author, time.Now().Unix(), nil)
}

func (c *BugCache) CloseRaw(author *IdentityCache, unixTime int64, metadata map[string]string) (*bug.SetStatusOperation, error) {
	c.mu.Lock()
	op, err := bug.Close(c.bug, author.Identity, unixTime)
	if err != nil {
		c.mu.Unlock()
		return nil, err
	}

	for key, value := range metadata {
		op.SetMetadata(key, value)
	}

	c.mu.Unlock()
	return op, c.notifyUpdated()
}

func (c *BugCache) SetTitle(title string) (*bug.SetTitleOperation, error) {
	author, err := c.repoCache.GetUserIdentity()
	if err != nil {
		return nil, err
	}

	return c.SetTitleRaw(author, time.Now().Unix(), title, nil)
}

func (c *BugCache) SetTitleRaw(author *IdentityCache, unixTime int64, title string, metadata map[string]string) (*bug.SetTitleOperation, error) {
	c.mu.Lock()
	op, err := bug.SetTitle(c.bug, author.Identity, unixTime, title)
	if err != nil {
		c.mu.Unlock()
		return nil, err
	}

	for key, value := range metadata {
		op.SetMetadata(key, value)
	}

	c.mu.Unlock()
	return op, c.notifyUpdated()
}

// Convenience function to edit the body of a bug (the first comment)
func (c *BugCache) EditCreateComment(body string) (*bug.EditCommentOperation, error) {
	author, err := c.repoCache.GetUserIdentity()
	if err != nil {
		return nil, err
	}

	return c.EditCreateCommentRaw(author, time.Now().Unix(), body, nil)
}

// Convenience function to edit the body of a bug (the first comment)
func (c *BugCache) EditCreateCommentRaw(author *IdentityCache, unixTime int64, body string, metadata map[string]string) (*bug.EditCommentOperation, error) {
	c.mu.Lock()
	op, err := bug.EditCreateComment(c.bug, author.Identity, unixTime, body)
	if err != nil {
		c.mu.Unlock()
		return nil, err
	}

	for key, value := range metadata {
		op.SetMetadata(key, value)
	}

	c.mu.Unlock()
	return op, c.notifyUpdated()
}

func (c *BugCache) EditComment(target entity.Id, message string) (*bug.EditCommentOperation, error) {
	author, err := c.repoCache.GetUserIdentity()
	if err != nil {
		return nil, err
	}

	return c.EditCommentRaw(author, time.Now().Unix(), target, message, nil)
}

func (c *BugCache) EditCommentRaw(author *IdentityCache, unixTime int64, target entity.Id, message string, metadata map[string]string) (*bug.EditCommentOperation, error) {
	c.mu.Lock()
	op, err := bug.EditComment(c.bug, author.Identity, unixTime, target, message)
	if err != nil {
		c.mu.Unlock()
		return nil, err
	}

	for key, value := range metadata {
		op.SetMetadata(key, value)
	}

	c.mu.Unlock()
	return op, c.notifyUpdated()
}

func (c *BugCache) SetMetadata(target entity.Id, newMetadata map[string]string) (*bug.SetMetadataOperation, error) {
	author, err := c.repoCache.GetUserIdentity()
	if err != nil {
		return nil, err
	}

	return c.SetMetadataRaw(author, time.Now().Unix(), target, newMetadata)
}

func (c *BugCache) SetMetadataRaw(author *IdentityCache, unixTime int64, target entity.Id, newMetadata map[string]string) (*bug.SetMetadataOperation, error) {
	c.mu.Lock()
	op, err := bug.SetMetadata(c.bug, author.Identity, unixTime, target, newMetadata)
	if err != nil {
		c.mu.Unlock()
		return nil, err
	}

	c.mu.Unlock()
	return op, c.notifyUpdated()
}

func (c *BugCache) Commit() error {
	c.mu.Lock()
	err := c.bug.Commit(c.repoCache.repo)
	if err != nil {
		c.mu.Unlock()
		return err
	}
	c.mu.Unlock()
	return c.notifyUpdated()
}

func (c *BugCache) CommitAsNeeded() error {
	c.mu.Lock()
	err := c.bug.CommitAsNeeded(c.repoCache.repo)
	if err != nil {
		c.mu.Unlock()
		return err
	}
	c.mu.Unlock()
	return c.notifyUpdated()
}

func (c *BugCache) NeedCommit() bool {
	c.mu.RLock()
	defer c.mu.RUnlock()
	return c.bug.NeedCommit()
}