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// ResolveTargetWithMetadata will find an operation that has the matching metadata
61func (c *BugCache) ResolveTargetWithMetadata(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) error {
90 return c.AddCommentWithFiles(message, nil)
91}
92
93func (c *BugCache) AddCommentWithFiles(message string, files []git.Hash) error {
94 author, err := c.repoCache.GetUserIdentity()
95 if err != nil {
96 return 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) error {
103 op, err := bug.AddCommentWithFiles(c.bug, author.Identity, unixTime, message, files)
104 if err != nil {
105 return err
106 }
107
108 for key, value := range metadata {
109 op.SetMetadata(key, value)
110 }
111
112 return c.notifyUpdated()
113}
114
115func (c *BugCache) ChangeLabels(added []string, removed []string) ([]bug.LabelChangeResult, error) {
116 author, err := c.repoCache.GetUserIdentity()
117 if err != nil {
118 return 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, error) {
125 changes, op, err := bug.ChangeLabels(c.bug, author.Identity, unixTime, added, removed)
126 if err != nil {
127 return changes, 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, err
137 }
138
139 return changes, nil
140}
141
142func (c *BugCache) Open() error {
143 author, err := c.repoCache.GetUserIdentity()
144 if err != nil {
145 return err
146 }
147
148 return c.OpenRaw(author, time.Now().Unix(), nil)
149}
150
151func (c *BugCache) OpenRaw(author *IdentityCache, unixTime int64, metadata map[string]string) error {
152 op, err := bug.Open(c.bug, author.Identity, unixTime)
153 if err != nil {
154 return err
155 }
156
157 for key, value := range metadata {
158 op.SetMetadata(key, value)
159 }
160
161 return c.notifyUpdated()
162}
163
164func (c *BugCache) Close() error {
165 author, err := c.repoCache.GetUserIdentity()
166 if err != nil {
167 return err
168 }
169
170 return c.CloseRaw(author, time.Now().Unix(), nil)
171}
172
173func (c *BugCache) CloseRaw(author *IdentityCache, unixTime int64, metadata map[string]string) error {
174 op, err := bug.Close(c.bug, author.Identity, unixTime)
175 if err != nil {
176 return err
177 }
178
179 for key, value := range metadata {
180 op.SetMetadata(key, value)
181 }
182
183 return c.notifyUpdated()
184}
185
186func (c *BugCache) SetTitle(title string) error {
187 author, err := c.repoCache.GetUserIdentity()
188 if err != nil {
189 return err
190 }
191
192 return c.SetTitleRaw(author, time.Now().Unix(), title, nil)
193}
194
195func (c *BugCache) SetTitleRaw(author *IdentityCache, unixTime int64, title string, metadata map[string]string) error {
196 op, err := bug.SetTitle(c.bug, author.Identity, unixTime, title)
197 if err != nil {
198 return err
199 }
200
201 for key, value := range metadata {
202 op.SetMetadata(key, value)
203 }
204
205 return c.notifyUpdated()
206}
207
208func (c *BugCache) EditComment(target git.Hash, message string) error {
209 author, err := c.repoCache.GetUserIdentity()
210 if err != nil {
211 return err
212 }
213
214 return c.EditCommentRaw(author, time.Now().Unix(), target, message, nil)
215}
216
217func (c *BugCache) EditCommentRaw(author *IdentityCache, unixTime int64, target git.Hash, message string, metadata map[string]string) error {
218 op, err := bug.EditComment(c.bug, author.Identity, unixTime, target, message)
219 if err != nil {
220 return err
221 }
222
223 for key, value := range metadata {
224 op.SetMetadata(key, value)
225 }
226
227 return c.notifyUpdated()
228}
229
230func (c *BugCache) Commit() error {
231 return c.bug.Commit(c.repoCache.repo)
232}
233
234func (c *BugCache) CommitAsNeeded() error {
235 return c.bug.CommitAsNeeded(c.repoCache.repo)
236}