1package cache
2
3import (
4 "github.com/MichaelMure/git-bug/bug"
5 "github.com/MichaelMure/git-bug/bug/operations"
6 "github.com/MichaelMure/git-bug/util"
7)
8
9type BugCache struct {
10 repoCache *RepoCache
11 bug *bug.WithSnapshot
12}
13
14func NewBugCache(repoCache *RepoCache, b *bug.Bug) *BugCache {
15 return &BugCache{
16 repoCache: repoCache,
17 bug: &bug.WithSnapshot{Bug: b},
18 }
19}
20
21func (c *BugCache) Snapshot() *bug.Snapshot {
22 return c.bug.Snapshot()
23}
24
25func (c *BugCache) notifyUpdated() error {
26 return c.repoCache.bugUpdated(c.bug.Id())
27}
28
29func (c *BugCache) AddComment(message string) error {
30 if err := c.AddCommentWithFiles(message, nil); err != nil {
31 return err
32 }
33
34 return c.notifyUpdated()
35}
36
37func (c *BugCache) AddCommentWithFiles(message string, files []util.Hash) error {
38 author, err := bug.GetUser(c.repoCache.repo)
39 if err != nil {
40 return err
41 }
42
43 operations.CommentWithFiles(c.bug, author, message, files)
44
45 return c.notifyUpdated()
46}
47
48func (c *BugCache) ChangeLabels(added []string, removed []string) error {
49 author, err := bug.GetUser(c.repoCache.repo)
50 if err != nil {
51 return err
52 }
53
54 err = operations.ChangeLabels(nil, c.bug, author, added, removed)
55 if err != nil {
56 return err
57 }
58
59 return c.notifyUpdated()
60}
61
62func (c *BugCache) Open() error {
63 author, err := bug.GetUser(c.repoCache.repo)
64 if err != nil {
65 return err
66 }
67
68 operations.Open(c.bug, author)
69
70 return c.notifyUpdated()
71}
72
73func (c *BugCache) Close() error {
74 author, err := bug.GetUser(c.repoCache.repo)
75 if err != nil {
76 return err
77 }
78
79 operations.Close(c.bug, author)
80
81 return c.notifyUpdated()
82}
83
84func (c *BugCache) SetTitle(title string) error {
85 author, err := bug.GetUser(c.repoCache.repo)
86 if err != nil {
87 return err
88 }
89
90 operations.SetTitle(c.bug, author, title)
91
92 return c.notifyUpdated()
93}
94
95func (c *BugCache) Commit() error {
96 return c.bug.Commit(c.repoCache.repo)
97}
98
99func (c *BugCache) CommitAsNeeded() error {
100 if c.bug.HasPendingOp() {
101 return c.bug.Commit(c.repoCache.repo)
102 }
103 return nil
104}