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	operations.CommentWithFiles(c.bug, author, message, files)
 48
 49	return c.notifyUpdated()
 50}
 51
 52func (c *BugCache) ChangeLabels(added []string, removed []string) ([]operations.LabelChangeResult, error) {
 53	author, err := bug.GetUser(c.repoCache.repo)
 54	if err != nil {
 55		return nil, err
 56	}
 57
 58	changes, err := operations.ChangeLabels(c.bug, author, added, removed)
 59	if err != nil {
 60		return changes, err
 61	}
 62
 63	err = c.notifyUpdated()
 64	if err != nil {
 65		return nil, err
 66	}
 67
 68	return changes, nil
 69}
 70
 71func (c *BugCache) Open() error {
 72	author, err := bug.GetUser(c.repoCache.repo)
 73	if err != nil {
 74		return err
 75	}
 76
 77	operations.Open(c.bug, author)
 78
 79	return c.notifyUpdated()
 80}
 81
 82func (c *BugCache) Close() error {
 83	author, err := bug.GetUser(c.repoCache.repo)
 84	if err != nil {
 85		return err
 86	}
 87
 88	operations.Close(c.bug, author)
 89
 90	return c.notifyUpdated()
 91}
 92
 93func (c *BugCache) SetTitle(title string) error {
 94	author, err := bug.GetUser(c.repoCache.repo)
 95	if err != nil {
 96		return err
 97	}
 98
 99	operations.SetTitle(c.bug, author, title)
100
101	return c.notifyUpdated()
102}
103
104func (c *BugCache) Commit() error {
105	return c.bug.Commit(c.repoCache.repo)
106}
107
108func (c *BugCache) CommitAsNeeded() error {
109	if c.bug.HasPendingOp() {
110		return c.bug.Commit(c.repoCache.repo)
111	}
112	return nil
113}