bug_cache.go

  1package cache
  2
  3import (
  4	"time"
  5
  6	"github.com/MichaelMure/git-bug/bug"
  7	"github.com/MichaelMure/git-bug/util/git"
  8)
  9
 10type BugCache struct {
 11	repoCache *RepoCache
 12	bug       *bug.WithSnapshot
 13}
 14
 15func NewBugCache(repoCache *RepoCache, b *bug.Bug) *BugCache {
 16	return &BugCache{
 17		repoCache: repoCache,
 18		bug:       &bug.WithSnapshot{Bug: b},
 19	}
 20}
 21
 22func (c *BugCache) Snapshot() *bug.Snapshot {
 23	return c.bug.Snapshot()
 24}
 25
 26func (c *BugCache) Id() string {
 27	return c.bug.Id()
 28}
 29
 30func (c *BugCache) HumanId() string {
 31	return c.bug.HumanId()
 32}
 33
 34func (c *BugCache) notifyUpdated() error {
 35	return c.repoCache.bugUpdated(c.bug.Id())
 36}
 37
 38func (c *BugCache) AddComment(message string) error {
 39	return c.AddCommentWithFiles(message, nil)
 40}
 41
 42func (c *BugCache) AddCommentWithFiles(message string, files []git.Hash) error {
 43	author, err := bug.GetUser(c.repoCache.repo)
 44	if err != nil {
 45		return err
 46	}
 47
 48	return c.AddCommentRaw(author, time.Now().Unix(), message, files, nil)
 49}
 50
 51func (c *BugCache) AddCommentRaw(author bug.Person, unixTime int64, message string, files []git.Hash, metadata map[string]string) error {
 52	op, err := bug.AddCommentWithFiles(c.bug, author, unixTime, message, files)
 53	if err != nil {
 54		return err
 55	}
 56
 57	for key, value := range metadata {
 58		op.SetMetadata(key, value)
 59	}
 60
 61	return c.notifyUpdated()
 62}
 63
 64func (c *BugCache) ChangeLabels(added []string, removed []string) ([]bug.LabelChangeResult, error) {
 65	author, err := bug.GetUser(c.repoCache.repo)
 66	if err != nil {
 67		return nil, err
 68	}
 69
 70	return c.ChangeLabelsRaw(author, time.Now().Unix(), added, removed, nil)
 71}
 72
 73func (c *BugCache) ChangeLabelsRaw(author bug.Person, unixTime int64, added []string, removed []string, metadata map[string]string) ([]bug.LabelChangeResult, error) {
 74	changes, op, err := bug.ChangeLabels(c.bug, author, unixTime, added, removed)
 75	if err != nil {
 76		return changes, err
 77	}
 78
 79	for key, value := range metadata {
 80		op.SetMetadata(key, value)
 81	}
 82
 83	err = c.notifyUpdated()
 84	if err != nil {
 85		return nil, err
 86	}
 87
 88	return changes, nil
 89}
 90
 91func (c *BugCache) Open() error {
 92	author, err := bug.GetUser(c.repoCache.repo)
 93	if err != nil {
 94		return err
 95	}
 96
 97	return c.OpenRaw(author, time.Now().Unix(), nil)
 98}
 99
100func (c *BugCache) OpenRaw(author bug.Person, unixTime int64, metadata map[string]string) error {
101	op, err := bug.Open(c.bug, author, unixTime)
102	if err != nil {
103		return err
104	}
105
106	for key, value := range metadata {
107		op.SetMetadata(key, value)
108	}
109
110	return c.notifyUpdated()
111}
112
113func (c *BugCache) Close() error {
114	author, err := bug.GetUser(c.repoCache.repo)
115	if err != nil {
116		return err
117	}
118
119	return c.CloseRaw(author, time.Now().Unix(), nil)
120}
121
122func (c *BugCache) CloseRaw(author bug.Person, unixTime int64, metadata map[string]string) error {
123	op, err := bug.Close(c.bug, author, unixTime)
124	if err != nil {
125		return err
126	}
127
128	for key, value := range metadata {
129		op.SetMetadata(key, value)
130	}
131
132	return c.notifyUpdated()
133}
134
135func (c *BugCache) SetTitle(title string) error {
136	author, err := bug.GetUser(c.repoCache.repo)
137	if err != nil {
138		return err
139	}
140
141	return c.SetTitleRaw(author, time.Now().Unix(), title, nil)
142}
143
144func (c *BugCache) SetTitleRaw(author bug.Person, unixTime int64, title string, metadata map[string]string) error {
145	op, err := bug.SetTitle(c.bug, author, unixTime, title)
146	if err != nil {
147		return err
148	}
149
150	for key, value := range metadata {
151		op.SetMetadata(key, value)
152	}
153
154	return c.notifyUpdated()
155}
156
157func (c *BugCache) EditComment(target git.Hash, message string) error {
158	author, err := bug.GetUser(c.repoCache.repo)
159	if err != nil {
160		return err
161	}
162
163	return c.EditCommentRaw(author, time.Now().Unix(), target, message, nil)
164}
165
166func (c *BugCache) EditCommentRaw(author bug.Person, unixTime int64, target git.Hash, message string, metadata map[string]string) error {
167	op, err := bug.EditComment(c.bug, author, unixTime, target, message)
168	if err != nil {
169		return err
170	}
171
172	for key, value := range metadata {
173		op.SetMetadata(key, value)
174	}
175
176	return c.notifyUpdated()
177}
178
179func (c *BugCache) Commit() error {
180	return c.bug.Commit(c.repoCache.repo)
181}
182
183func (c *BugCache) CommitAsNeeded() error {
184	if c.bug.HasPendingOp() {
185		return c.bug.Commit(c.repoCache.repo)
186	}
187	return nil
188}