1package cache
2
3import (
4 "time"
5
6 "github.com/MichaelMure/git-bug/bug"
7 "github.com/MichaelMure/git-bug/util/git"
8)
9
10type BugCache struct {
11 repoCache *RepoCache
12 bug *bug.WithSnapshot
13}
14
15func NewBugCache(repoCache *RepoCache, b *bug.Bug) *BugCache {
16 return &BugCache{
17 repoCache: repoCache,
18 bug: &bug.WithSnapshot{Bug: b},
19 }
20}
21
22func (c *BugCache) Snapshot() *bug.Snapshot {
23 return c.bug.Snapshot()
24}
25
26func (c *BugCache) Id() string {
27 return c.bug.Id()
28}
29
30func (c *BugCache) HumanId() string {
31 return c.bug.HumanId()
32}
33
34func (c *BugCache) notifyUpdated() error {
35 return c.repoCache.bugUpdated(c.bug.Id())
36}
37
38func (c *BugCache) AddComment(message string) error {
39 return c.AddCommentWithFiles(message, nil)
40}
41
42func (c *BugCache) AddCommentWithFiles(message string, files []git.Hash) error {
43 author, err := bug.GetUser(c.repoCache.repo)
44 if err != nil {
45 return err
46 }
47
48 return c.AddCommentRaw(author, time.Now().Unix(), message, files, nil)
49}
50
51func (c *BugCache) AddCommentRaw(author bug.Person, unixTime int64, message string, files []git.Hash, metadata map[string]string) error {
52 err := bug.AddCommentWithFiles(c.bug, author, unixTime, message, files)
53 if err != nil {
54 return err
55 }
56
57 return c.notifyUpdated()
58}
59
60func (c *BugCache) ChangeLabels(added []string, removed []string) ([]bug.LabelChangeResult, error) {
61 author, err := bug.GetUser(c.repoCache.repo)
62 if err != nil {
63 return nil, err
64 }
65
66 return c.ChangeLabelsRaw(author, time.Now().Unix(), added, removed)
67}
68
69func (c *BugCache) ChangeLabelsRaw(author bug.Person, unixTime int64, added []string, removed []string) ([]bug.LabelChangeResult, error) {
70 changes, err := bug.ChangeLabels(c.bug, author, unixTime, added, removed)
71 if err != nil {
72 return changes, err
73 }
74
75 err = c.notifyUpdated()
76 if err != nil {
77 return nil, err
78 }
79
80 return changes, nil
81}
82
83func (c *BugCache) Open() error {
84 author, err := bug.GetUser(c.repoCache.repo)
85 if err != nil {
86 return err
87 }
88
89 return c.OpenRaw(author, time.Now().Unix())
90}
91
92func (c *BugCache) OpenRaw(author bug.Person, unixTime int64) error {
93 err := bug.Open(c.bug, author, unixTime)
94 if err != nil {
95 return err
96 }
97
98 return c.notifyUpdated()
99}
100
101func (c *BugCache) Close() error {
102 author, err := bug.GetUser(c.repoCache.repo)
103 if err != nil {
104 return err
105 }
106
107 return c.CloseRaw(author, time.Now().Unix())
108}
109
110func (c *BugCache) CloseRaw(author bug.Person, unixTime int64) error {
111 err := bug.Close(c.bug, author, unixTime)
112 if err != nil {
113 return err
114 }
115
116 return c.notifyUpdated()
117}
118
119func (c *BugCache) SetTitle(title string) error {
120 author, err := bug.GetUser(c.repoCache.repo)
121 if err != nil {
122 return err
123 }
124
125 return c.SetTitleRaw(author, time.Now().Unix(), title)
126}
127
128func (c *BugCache) SetTitleRaw(author bug.Person, unixTime int64, title string) error {
129 err := bug.SetTitle(c.bug, author, unixTime, title)
130 if err != nil {
131 return err
132 }
133
134 return c.notifyUpdated()
135}
136
137func (c *BugCache) Commit() error {
138 return c.bug.Commit(c.repoCache.repo)
139}
140
141func (c *BugCache) CommitAsNeeded() error {
142 if c.bug.HasPendingOp() {
143 return c.bug.Commit(c.repoCache.repo)
144 }
145 return nil
146}