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