bug_cache.go

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