bug_cache.go

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