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