2018-08-21 19:46:53 +03:00
|
|
|
package cache
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/MichaelMure/git-bug/bug"
|
2018-09-11 23:04:16 +03:00
|
|
|
"github.com/MichaelMure/git-bug/operations"
|
|
|
|
"github.com/MichaelMure/git-bug/util/git"
|
2018-08-21 19:46:53 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type BugCache struct {
|
2018-08-23 22:24:57 +03:00
|
|
|
repoCache *RepoCache
|
|
|
|
bug *bug.WithSnapshot
|
2018-08-21 19:46:53 +03:00
|
|
|
}
|
|
|
|
|
2018-08-23 22:24:57 +03:00
|
|
|
func NewBugCache(repoCache *RepoCache, b *bug.Bug) *BugCache {
|
2018-08-21 19:46:53 +03:00
|
|
|
return &BugCache{
|
2018-08-23 22:24:57 +03:00
|
|
|
repoCache: repoCache,
|
|
|
|
bug: &bug.WithSnapshot{Bug: b},
|
2018-08-21 19:46:53 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *BugCache) Snapshot() *bug.Snapshot {
|
2018-08-23 20:11:38 +03:00
|
|
|
return c.bug.Snapshot()
|
2018-08-21 19:46:53 +03:00
|
|
|
}
|
|
|
|
|
2018-09-18 13:49:16 +03:00
|
|
|
func (c *BugCache) Id() string {
|
|
|
|
return c.bug.Id()
|
|
|
|
}
|
|
|
|
|
2018-08-31 14:18:03 +03:00
|
|
|
func (c *BugCache) HumanId() string {
|
|
|
|
return c.bug.HumanId()
|
|
|
|
}
|
|
|
|
|
2018-08-23 22:24:57 +03:00
|
|
|
func (c *BugCache) notifyUpdated() error {
|
|
|
|
return c.repoCache.bugUpdated(c.bug.Id())
|
|
|
|
}
|
|
|
|
|
2018-08-21 19:46:53 +03:00
|
|
|
func (c *BugCache) AddComment(message string) error {
|
2018-08-23 22:24:57 +03:00
|
|
|
if err := c.AddCommentWithFiles(message, nil); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.notifyUpdated()
|
2018-08-21 19:46:53 +03:00
|
|
|
}
|
|
|
|
|
2018-09-11 23:04:16 +03:00
|
|
|
func (c *BugCache) AddCommentWithFiles(message string, files []git.Hash) error {
|
2018-08-23 22:24:57 +03:00
|
|
|
author, err := bug.GetUser(c.repoCache.repo)
|
2018-08-21 19:46:53 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-09-15 14:15:00 +03:00
|
|
|
err = operations.CommentWithFiles(c.bug, author, message, files)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-08-21 19:46:53 +03:00
|
|
|
|
2018-08-23 22:24:57 +03:00
|
|
|
return c.notifyUpdated()
|
2018-08-21 19:46:53 +03:00
|
|
|
}
|
|
|
|
|
2018-09-13 13:20:28 +03:00
|
|
|
func (c *BugCache) ChangeLabels(added []string, removed []string) ([]operations.LabelChangeResult, error) {
|
2018-08-23 22:24:57 +03:00
|
|
|
author, err := bug.GetUser(c.repoCache.repo)
|
2018-08-21 19:46:53 +03:00
|
|
|
if err != nil {
|
2018-09-13 13:20:28 +03:00
|
|
|
return nil, err
|
2018-08-21 19:46:53 +03:00
|
|
|
}
|
|
|
|
|
2018-09-13 13:20:28 +03:00
|
|
|
changes, err := operations.ChangeLabels(c.bug, author, added, removed)
|
2018-08-21 19:46:53 +03:00
|
|
|
if err != nil {
|
2018-09-13 13:20:28 +03:00
|
|
|
return changes, err
|
2018-08-21 19:46:53 +03:00
|
|
|
}
|
|
|
|
|
2018-09-13 13:20:28 +03:00
|
|
|
err = c.notifyUpdated()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return changes, nil
|
2018-08-21 19:46:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *BugCache) Open() error {
|
2018-08-23 22:24:57 +03:00
|
|
|
author, err := bug.GetUser(c.repoCache.repo)
|
2018-08-21 19:46:53 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-09-15 14:15:00 +03:00
|
|
|
err = operations.Open(c.bug, author)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-08-21 19:46:53 +03:00
|
|
|
|
2018-08-23 22:24:57 +03:00
|
|
|
return c.notifyUpdated()
|
2018-08-21 19:46:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *BugCache) Close() error {
|
2018-08-23 22:24:57 +03:00
|
|
|
author, err := bug.GetUser(c.repoCache.repo)
|
2018-08-21 19:46:53 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-09-15 14:15:00 +03:00
|
|
|
err = operations.Close(c.bug, author)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-08-21 19:46:53 +03:00
|
|
|
|
2018-08-23 22:24:57 +03:00
|
|
|
return c.notifyUpdated()
|
2018-08-21 19:46:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *BugCache) SetTitle(title string) error {
|
2018-08-23 22:24:57 +03:00
|
|
|
author, err := bug.GetUser(c.repoCache.repo)
|
2018-08-21 19:46:53 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-09-15 14:15:00 +03:00
|
|
|
err = operations.SetTitle(c.bug, author, title)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-08-21 19:46:53 +03:00
|
|
|
|
2018-08-23 22:24:57 +03:00
|
|
|
return c.notifyUpdated()
|
2018-08-21 19:46:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *BugCache) Commit() error {
|
2018-08-23 22:24:57 +03:00
|
|
|
return c.bug.Commit(c.repoCache.repo)
|
2018-08-21 19:46:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *BugCache) CommitAsNeeded() error {
|
|
|
|
if c.bug.HasPendingOp() {
|
2018-08-23 22:24:57 +03:00
|
|
|
return c.bug.Commit(c.repoCache.repo)
|
2018-08-21 19:46:53 +03:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|