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 []git.Hash
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) (git.Hash, error) {
62 // preallocate but empty
63 matching := make([]git.Hash, 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 h, err := op.Hash()
71 if err != nil {
72 return "", err
73 }
74 matching = append(matching, h)
75 }
76 }
77
78 if len(matching) == 0 {
79 return "", ErrNoMatchingOp
80 }
81
82 if len(matching) > 1 {
83 return "", ErrMultipleMatchOp{Matching: matching}
84 }
85
86 return matching[0], nil
87}
88
89func (c *BugCache) AddComment(message string) (*bug.AddCommentOperation, error) {
90 return c.AddCommentWithFiles(message, nil)
91}
92
93func (c *BugCache) AddCommentWithFiles(message string, files []git.Hash) (*bug.AddCommentOperation, error) {
94 author, err := c.repoCache.GetUserIdentity()
95 if err != nil {
96 return nil, err
97 }
98
99 return c.AddCommentRaw(author, time.Now().Unix(), message, files, nil)
100}
101
102func (c *BugCache) AddCommentRaw(author *IdentityCache, unixTime int64, message string, files []git.Hash, metadata map[string]string) (*bug.AddCommentOperation, error) {
103 op, err := bug.AddCommentWithFiles(c.bug, author.Identity, unixTime, message, files)
104 if err != nil {
105 return nil, err
106 }
107
108 for key, value := range metadata {
109 op.SetMetadata(key, value)
110 }
111
112 return op, c.notifyUpdated()
113}
114
115func (c *BugCache) ChangeLabels(added []string, removed []string) ([]bug.LabelChangeResult, *bug.LabelChangeOperation, error) {
116 author, err := c.repoCache.GetUserIdentity()
117 if err != nil {
118 return nil, nil, err
119 }
120
121 return c.ChangeLabelsRaw(author, time.Now().Unix(), added, removed, nil)
122}
123
124func (c *BugCache) ChangeLabelsRaw(author *IdentityCache, unixTime int64, added []string, removed []string, metadata map[string]string) ([]bug.LabelChangeResult, *bug.LabelChangeOperation, error) {
125 changes, op, err := bug.ChangeLabels(c.bug, author.Identity, unixTime, added, removed)
126 if err != nil {
127 return changes, nil, err
128 }
129
130 for key, value := range metadata {
131 op.SetMetadata(key, value)
132 }
133
134 err = c.notifyUpdated()
135 if err != nil {
136 return nil, nil, err
137 }
138
139 return changes, op, nil
140}
141
142func (c *BugCache) ForceChangeLabels(added []string, removed []string) (*bug.LabelChangeOperation, error) {
143 author, err := c.repoCache.GetUserIdentity()
144 if err != nil {
145 return nil, err
146 }
147
148 return c.ForceChangeLabelsRaw(author, time.Now().Unix(), added, removed, nil)
149}
150
151func (c *BugCache) ForceChangeLabelsRaw(author *IdentityCache, unixTime int64, added []string, removed []string, metadata map[string]string) (*bug.LabelChangeOperation, error) {
152 op, err := bug.ForceChangeLabels(c.bug, author.Identity, unixTime, added, removed)
153 if err != nil {
154 return nil, err
155 }
156
157 for key, value := range metadata {
158 op.SetMetadata(key, value)
159 }
160
161 err = c.notifyUpdated()
162 if err != nil {
163 return nil, err
164 }
165
166 return op, nil
167}
168
169func (c *BugCache) Open() (*bug.SetStatusOperation, error) {
170 author, err := c.repoCache.GetUserIdentity()
171 if err != nil {
172 return nil, err
173 }
174
175 return c.OpenRaw(author, time.Now().Unix(), nil)
176}
177
178func (c *BugCache) OpenRaw(author *IdentityCache, unixTime int64, metadata map[string]string) (*bug.SetStatusOperation, error) {
179 op, err := bug.Open(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) Close() (*bug.SetStatusOperation, error) {
192 author, err := c.repoCache.GetUserIdentity()
193 if err != nil {
194 return nil, err
195 }
196
197 return c.CloseRaw(author, time.Now().Unix(), nil)
198}
199
200func (c *BugCache) CloseRaw(author *IdentityCache, unixTime int64, metadata map[string]string) (*bug.SetStatusOperation, error) {
201 op, err := bug.Close(c.bug, author.Identity, unixTime)
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) 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 op, err := bug.SetTitle(c.bug, author.Identity, unixTime, title)
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 git.Hash, 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 git.Hash, 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 git.Hash, 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 git.Hash, 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}