bug_cache.go

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