bug_cache.go

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