1package cache
2
3import (
4 "fmt"
5 "sync"
6 "time"
7
8 "github.com/MichaelMure/git-bug/bug"
9 "github.com/MichaelMure/git-bug/entity"
10 "github.com/MichaelMure/git-bug/repository"
11)
12
13var ErrNoMatchingOp = fmt.Errorf("no matching operation found")
14
15// BugCache is a wrapper around a Bug. It provide multiple functions:
16//
17// 1. Provide a higher level API to use than the raw API from Bug.
18// 2. Maintain an up to date Snapshot available.
19// 3. Deal with concurrency.
20type BugCache struct {
21 repoCache *RepoCache
22 mu sync.RWMutex
23 bug *bug.WithSnapshot
24}
25
26func NewBugCache(repoCache *RepoCache, b *bug.Bug) *BugCache {
27 return &BugCache{
28 repoCache: repoCache,
29 bug: &bug.WithSnapshot{Bug: b},
30 }
31}
32
33func (c *BugCache) Snapshot() *bug.Snapshot {
34 c.mu.RLock()
35 defer c.mu.RUnlock()
36 return c.bug.Snapshot()
37}
38
39func (c *BugCache) Id() entity.Id {
40 return c.bug.Id()
41}
42
43func (c *BugCache) notifyUpdated() error {
44 return c.repoCache.bugUpdated(c.bug.Id())
45}
46
47// ResolveOperationWithMetadata will find an operation that has the matching metadata
48func (c *BugCache) ResolveOperationWithMetadata(key string, value string) (entity.Id, error) {
49 c.mu.RLock()
50 defer c.mu.RUnlock()
51 // preallocate but empty
52 matching := make([]entity.Id, 0, 5)
53
54 for _, op := range c.bug.Operations() {
55 opValue, ok := op.GetMetadata(key)
56 if ok && value == opValue {
57 matching = append(matching, op.Id())
58 }
59 }
60
61 if len(matching) == 0 {
62 return "", ErrNoMatchingOp
63 }
64
65 if len(matching) > 1 {
66 return "", bug.NewErrMultipleMatchOp(matching)
67 }
68
69 return matching[0], nil
70}
71
72func (c *BugCache) AddComment(message string) (*bug.AddCommentOperation, error) {
73 return c.AddCommentWithFiles(message, nil)
74}
75
76func (c *BugCache) AddCommentWithFiles(message string, files []repository.Hash) (*bug.AddCommentOperation, error) {
77 author, err := c.repoCache.GetUserIdentity()
78 if err != nil {
79 return nil, err
80 }
81
82 return c.AddCommentRaw(author, time.Now().Unix(), message, files, nil)
83}
84
85func (c *BugCache) AddCommentRaw(author *IdentityCache, unixTime int64, message string, files []repository.Hash, metadata map[string]string) (*bug.AddCommentOperation, error) {
86 c.mu.Lock()
87 op, err := bug.AddCommentWithFiles(c.bug, author.Identity, unixTime, message, files)
88 if err != nil {
89 c.mu.Unlock()
90 return nil, err
91 }
92
93 for key, value := range metadata {
94 op.SetMetadata(key, value)
95 }
96
97 c.mu.Unlock()
98
99 return op, c.notifyUpdated()
100}
101
102func (c *BugCache) ChangeLabels(added []string, removed []string) ([]bug.LabelChangeResult, *bug.LabelChangeOperation, error) {
103 author, err := c.repoCache.GetUserIdentity()
104 if err != nil {
105 return nil, nil, err
106 }
107
108 return c.ChangeLabelsRaw(author, time.Now().Unix(), added, removed, nil)
109}
110
111func (c *BugCache) ChangeLabelsRaw(author *IdentityCache, unixTime int64, added []string, removed []string, metadata map[string]string) ([]bug.LabelChangeResult, *bug.LabelChangeOperation, error) {
112 c.mu.Lock()
113 changes, op, err := bug.ChangeLabels(c.bug, author.Identity, unixTime, added, removed)
114 if err != nil {
115 c.mu.Unlock()
116 return changes, nil, err
117 }
118
119 for key, value := range metadata {
120 op.SetMetadata(key, value)
121 }
122
123 c.mu.Unlock()
124
125 err = c.notifyUpdated()
126 if err != nil {
127 return nil, nil, err
128 }
129
130 return changes, op, nil
131}
132
133func (c *BugCache) ForceChangeLabels(added []string, removed []string) (*bug.LabelChangeOperation, error) {
134 author, err := c.repoCache.GetUserIdentity()
135 if err != nil {
136 return nil, err
137 }
138
139 return c.ForceChangeLabelsRaw(author, time.Now().Unix(), added, removed, nil)
140}
141
142func (c *BugCache) ForceChangeLabelsRaw(author *IdentityCache, unixTime int64, added []string, removed []string, metadata map[string]string) (*bug.LabelChangeOperation, error) {
143 c.mu.Lock()
144 op, err := bug.ForceChangeLabels(c.bug, author.Identity, unixTime, added, removed)
145 if err != nil {
146 c.mu.Unlock()
147 return nil, err
148 }
149
150 for key, value := range metadata {
151 op.SetMetadata(key, value)
152 }
153
154 c.mu.Unlock()
155 err = c.notifyUpdated()
156 if err != nil {
157 return nil, err
158 }
159
160 return op, nil
161}
162
163func (c *BugCache) Open() (*bug.SetStatusOperation, error) {
164 author, err := c.repoCache.GetUserIdentity()
165 if err != nil {
166 return nil, err
167 }
168
169 return c.OpenRaw(author, time.Now().Unix(), nil)
170}
171
172func (c *BugCache) OpenRaw(author *IdentityCache, unixTime int64, metadata map[string]string) (*bug.SetStatusOperation, error) {
173 c.mu.Lock()
174 op, err := bug.Open(c.bug, author.Identity, unixTime)
175 if err != nil {
176 c.mu.Unlock()
177 return nil, err
178 }
179
180 for key, value := range metadata {
181 op.SetMetadata(key, value)
182 }
183
184 c.mu.Unlock()
185 return op, c.notifyUpdated()
186}
187
188func (c *BugCache) Close() (*bug.SetStatusOperation, error) {
189 author, err := c.repoCache.GetUserIdentity()
190 if err != nil {
191 return nil, err
192 }
193
194 return c.CloseRaw(author, time.Now().Unix(), nil)
195}
196
197func (c *BugCache) CloseRaw(author *IdentityCache, unixTime int64, metadata map[string]string) (*bug.SetStatusOperation, error) {
198 c.mu.Lock()
199 op, err := bug.Close(c.bug, author.Identity, unixTime)
200 if err != nil {
201 c.mu.Unlock()
202 return nil, err
203 }
204
205 for key, value := range metadata {
206 op.SetMetadata(key, value)
207 }
208
209 c.mu.Unlock()
210 return op, c.notifyUpdated()
211}
212
213func (c *BugCache) SetTitle(title string) (*bug.SetTitleOperation, error) {
214 author, err := c.repoCache.GetUserIdentity()
215 if err != nil {
216 return nil, err
217 }
218
219 return c.SetTitleRaw(author, time.Now().Unix(), title, nil)
220}
221
222func (c *BugCache) SetTitleRaw(author *IdentityCache, unixTime int64, title string, metadata map[string]string) (*bug.SetTitleOperation, error) {
223 c.mu.Lock()
224 op, err := bug.SetTitle(c.bug, author.Identity, unixTime, title)
225 if err != nil {
226 c.mu.Unlock()
227 return nil, err
228 }
229
230 for key, value := range metadata {
231 op.SetMetadata(key, value)
232 }
233
234 c.mu.Unlock()
235 return op, c.notifyUpdated()
236}
237
238func (c *BugCache) EditCreateComment(body string) (*bug.EditCommentOperation, error) {
239 author, err := c.repoCache.GetUserIdentity()
240 if err != nil {
241 return nil, err
242 }
243
244 return c.EditCreateCommentRaw(author, time.Now().Unix(), body, nil)
245}
246
247func (c *BugCache) EditCreateCommentRaw(author *IdentityCache, unixTime int64, body string, metadata map[string]string) (*bug.EditCommentOperation, error) {
248 c.mu.Lock()
249 op, err := bug.EditCreateComment(c.bug, author.Identity, unixTime, body)
250 if err != nil {
251 c.mu.Unlock()
252 return nil, err
253 }
254
255 for key, value := range metadata {
256 op.SetMetadata(key, value)
257 }
258
259 c.mu.Unlock()
260 return op, c.notifyUpdated()
261}
262
263func (c *BugCache) EditComment(target entity.Id, message string) (*bug.EditCommentOperation, error) {
264 author, err := c.repoCache.GetUserIdentity()
265 if err != nil {
266 return nil, err
267 }
268
269 return c.EditCommentRaw(author, time.Now().Unix(), target, message, nil)
270}
271
272func (c *BugCache) EditCommentRaw(author *IdentityCache, unixTime int64, target entity.Id, message string, metadata map[string]string) (*bug.EditCommentOperation, error) {
273 c.mu.Lock()
274 op, err := bug.EditComment(c.bug, author.Identity, unixTime, target, message)
275 if err != nil {
276 c.mu.Unlock()
277 return nil, err
278 }
279
280 for key, value := range metadata {
281 op.SetMetadata(key, value)
282 }
283
284 c.mu.Unlock()
285 return op, c.notifyUpdated()
286}
287
288func (c *BugCache) SetMetadata(target entity.Id, newMetadata map[string]string) (*bug.SetMetadataOperation, error) {
289 author, err := c.repoCache.GetUserIdentity()
290 if err != nil {
291 return nil, err
292 }
293
294 return c.SetMetadataRaw(author, time.Now().Unix(), target, newMetadata)
295}
296
297func (c *BugCache) SetMetadataRaw(author *IdentityCache, unixTime int64, target entity.Id, newMetadata map[string]string) (*bug.SetMetadataOperation, error) {
298 c.mu.Lock()
299 op, err := bug.SetMetadata(c.bug, author.Identity, unixTime, target, newMetadata)
300 if err != nil {
301 c.mu.Unlock()
302 return nil, err
303 }
304
305 c.mu.Unlock()
306 return op, c.notifyUpdated()
307}
308
309func (c *BugCache) Commit() error {
310 c.mu.Lock()
311 err := c.bug.Commit(c.repoCache.repo)
312 if err != nil {
313 c.mu.Unlock()
314 return err
315 }
316 c.mu.Unlock()
317 return c.notifyUpdated()
318}
319
320func (c *BugCache) CommitAsNeeded() error {
321 c.mu.Lock()
322 err := c.bug.CommitAsNeeded(c.repoCache.repo)
323 if err != nil {
324 c.mu.Unlock()
325 return err
326 }
327 c.mu.Unlock()
328 return c.notifyUpdated()
329}
330
331func (c *BugCache) NeedCommit() bool {
332 c.mu.RLock()
333 defer c.mu.RUnlock()
334 return c.bug.NeedCommit()
335}