1package cache
2
3import (
4 "github.com/MichaelMure/git-bug/bug"
5 "github.com/MichaelMure/git-bug/operations"
6 "github.com/MichaelMure/git-bug/util/git"
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) Id() string {
26 return c.bug.Id()
27}
28
29func (c *BugCache) HumanId() string {
30 return c.bug.HumanId()
31}
32
33func (c *BugCache) notifyUpdated() error {
34 return c.repoCache.bugUpdated(c.bug.Id())
35}
36
37func (c *BugCache) AddComment(message string) error {
38 if err := c.AddCommentWithFiles(message, nil); err != nil {
39 return err
40 }
41
42 return c.notifyUpdated()
43}
44
45func (c *BugCache) AddCommentWithFiles(message string, files []git.Hash) error {
46 author, err := bug.GetUser(c.repoCache.repo)
47 if err != nil {
48 return err
49 }
50
51 err = operations.CommentWithFiles(c.bug, author, message, files)
52 if err != nil {
53 return err
54 }
55
56 return c.notifyUpdated()
57}
58
59func (c *BugCache) ChangeLabels(added []string, removed []string) ([]operations.LabelChangeResult, error) {
60 author, err := bug.GetUser(c.repoCache.repo)
61 if err != nil {
62 return nil, err
63 }
64
65 changes, err := operations.ChangeLabels(c.bug, author, added, removed)
66 if err != nil {
67 return changes, err
68 }
69
70 err = c.notifyUpdated()
71 if err != nil {
72 return nil, err
73 }
74
75 return changes, nil
76}
77
78func (c *BugCache) Open() error {
79 author, err := bug.GetUser(c.repoCache.repo)
80 if err != nil {
81 return err
82 }
83
84 err = operations.Open(c.bug, author)
85 if err != nil {
86 return err
87 }
88
89 return c.notifyUpdated()
90}
91
92func (c *BugCache) Close() error {
93 author, err := bug.GetUser(c.repoCache.repo)
94 if err != nil {
95 return err
96 }
97
98 err = operations.Close(c.bug, author)
99 if err != nil {
100 return err
101 }
102
103 return c.notifyUpdated()
104}
105
106func (c *BugCache) SetTitle(title string) error {
107 author, err := bug.GetUser(c.repoCache.repo)
108 if err != nil {
109 return err
110 }
111
112 err = operations.SetTitle(c.bug, author, title)
113 if err != nil {
114 return err
115 }
116
117 return c.notifyUpdated()
118}
119
120func (c *BugCache) Commit() error {
121 return c.bug.Commit(c.repoCache.repo)
122}
123
124func (c *BugCache) CommitAsNeeded() error {
125 if c.bug.HasPendingOp() {
126 return c.bug.Commit(c.repoCache.repo)
127 }
128 return nil
129}