2018-08-21 19:46:53 +03:00
|
|
|
package cache
|
|
|
|
|
|
|
|
import (
|
2018-10-02 00:34:45 +03:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
2018-09-25 18:56:58 +03:00
|
|
|
"time"
|
|
|
|
|
2018-08-21 19:46:53 +03:00
|
|
|
"github.com/MichaelMure/git-bug/bug"
|
2018-09-11 23:04:16 +03:00
|
|
|
"github.com/MichaelMure/git-bug/util/git"
|
2018-08-21 19:46:53 +03:00
|
|
|
)
|
|
|
|
|
2019-01-19 18:01:06 +03:00
|
|
|
// BugCache is a wrapper around a Bug. It provide multiple functions:
|
|
|
|
//
|
|
|
|
// 1. Provide a higher level API to use than the raw API from Bug.
|
|
|
|
// 2. Maintain an up to date Snapshot available.
|
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-10-02 00:34:45 +03:00
|
|
|
var ErrNoMatchingOp = fmt.Errorf("no matching operation found")
|
|
|
|
|
|
|
|
type ErrMultipleMatchOp struct {
|
2019-08-07 20:14:12 +03:00
|
|
|
Matching []string
|
2018-10-02 00:34:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e ErrMultipleMatchOp) Error() string {
|
|
|
|
casted := make([]string, len(e.Matching))
|
|
|
|
|
|
|
|
for i := range e.Matching {
|
|
|
|
casted[i] = string(e.Matching[i])
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintf("Multiple matching operation found:\n%s", strings.Join(casted, "\n"))
|
|
|
|
}
|
|
|
|
|
2019-02-24 14:58:04 +03:00
|
|
|
// ResolveOperationWithMetadata will find an operation that has the matching metadata
|
2019-08-07 20:14:12 +03:00
|
|
|
func (c *BugCache) ResolveOperationWithMetadata(key string, value string) (string, error) {
|
2018-10-02 00:34:45 +03:00
|
|
|
// preallocate but empty
|
2019-08-07 20:14:12 +03:00
|
|
|
matching := make([]string, 0, 5)
|
2018-10-02 00:34:45 +03:00
|
|
|
|
|
|
|
it := bug.NewOperationIterator(c.bug)
|
|
|
|
for it.Next() {
|
|
|
|
op := it.Value()
|
|
|
|
opValue, ok := op.GetMetadata(key)
|
|
|
|
if ok && value == opValue {
|
2019-08-07 20:14:12 +03:00
|
|
|
matching = append(matching, op.ID())
|
2018-10-02 00:34:45 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(matching) == 0 {
|
|
|
|
return "", ErrNoMatchingOp
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(matching) > 1 {
|
|
|
|
return "", ErrMultipleMatchOp{Matching: matching}
|
|
|
|
}
|
|
|
|
|
|
|
|
return matching[0], nil
|
|
|
|
}
|
|
|
|
|
2019-02-24 14:58:04 +03:00
|
|
|
func (c *BugCache) AddComment(message string) (*bug.AddCommentOperation, error) {
|
2018-09-25 18:56:58 +03:00
|
|
|
return c.AddCommentWithFiles(message, nil)
|
2018-08-21 19:46:53 +03:00
|
|
|
}
|
|
|
|
|
2019-02-24 14:58:04 +03:00
|
|
|
func (c *BugCache) AddCommentWithFiles(message string, files []git.Hash) (*bug.AddCommentOperation, error) {
|
2019-02-17 18:12:06 +03:00
|
|
|
author, err := c.repoCache.GetUserIdentity()
|
2018-08-21 19:46:53 +03:00
|
|
|
if err != nil {
|
2019-02-24 14:58:04 +03:00
|
|
|
return nil, err
|
2018-08-21 19:46:53 +03:00
|
|
|
}
|
|
|
|
|
2018-09-25 18:56:58 +03:00
|
|
|
return c.AddCommentRaw(author, time.Now().Unix(), message, files, nil)
|
|
|
|
}
|
|
|
|
|
2019-02-24 14:58:04 +03:00
|
|
|
func (c *BugCache) AddCommentRaw(author *IdentityCache, unixTime int64, message string, files []git.Hash, metadata map[string]string) (*bug.AddCommentOperation, error) {
|
2019-02-17 18:12:06 +03:00
|
|
|
op, err := bug.AddCommentWithFiles(c.bug, author.Identity, unixTime, message, files)
|
2018-09-15 14:15:00 +03:00
|
|
|
if err != nil {
|
2019-02-24 14:58:04 +03:00
|
|
|
return nil, err
|
2018-09-15 14:15:00 +03:00
|
|
|
}
|
2018-08-21 19:46:53 +03:00
|
|
|
|
2018-10-02 00:31:16 +03:00
|
|
|
for key, value := range metadata {
|
|
|
|
op.SetMetadata(key, value)
|
|
|
|
}
|
|
|
|
|
2019-02-24 14:58:04 +03:00
|
|
|
return op, c.notifyUpdated()
|
2018-08-21 19:46:53 +03:00
|
|
|
}
|
|
|
|
|
2019-02-24 14:58:04 +03:00
|
|
|
func (c *BugCache) ChangeLabels(added []string, removed []string) ([]bug.LabelChangeResult, *bug.LabelChangeOperation, error) {
|
2019-02-17 18:12:06 +03:00
|
|
|
author, err := c.repoCache.GetUserIdentity()
|
2018-08-21 19:46:53 +03:00
|
|
|
if err != nil {
|
2019-02-24 14:58:04 +03:00
|
|
|
return nil, nil, err
|
2018-08-21 19:46:53 +03:00
|
|
|
}
|
|
|
|
|
2018-10-02 00:31:16 +03:00
|
|
|
return c.ChangeLabelsRaw(author, time.Now().Unix(), added, removed, nil)
|
2018-09-25 18:56:58 +03:00
|
|
|
}
|
|
|
|
|
2019-02-24 14:58:04 +03:00
|
|
|
func (c *BugCache) ChangeLabelsRaw(author *IdentityCache, unixTime int64, added []string, removed []string, metadata map[string]string) ([]bug.LabelChangeResult, *bug.LabelChangeOperation, error) {
|
2019-02-17 18:12:06 +03:00
|
|
|
changes, op, err := bug.ChangeLabels(c.bug, author.Identity, unixTime, added, removed)
|
2018-08-21 19:46:53 +03:00
|
|
|
if err != nil {
|
2019-02-24 14:58:04 +03:00
|
|
|
return changes, nil, err
|
2018-08-21 19:46:53 +03:00
|
|
|
}
|
|
|
|
|
2018-10-02 00:31:16 +03:00
|
|
|
for key, value := range metadata {
|
|
|
|
op.SetMetadata(key, value)
|
|
|
|
}
|
|
|
|
|
2018-09-13 13:20:28 +03:00
|
|
|
err = c.notifyUpdated()
|
|
|
|
if err != nil {
|
2019-02-24 14:58:04 +03:00
|
|
|
return nil, nil, err
|
2018-09-13 13:20:28 +03:00
|
|
|
}
|
|
|
|
|
2019-02-24 14:58:04 +03:00
|
|
|
return changes, op, nil
|
2018-08-21 19:46:53 +03:00
|
|
|
}
|
|
|
|
|
2019-05-04 14:19:56 +03:00
|
|
|
func (c *BugCache) ForceChangeLabels(added []string, removed []string) (*bug.LabelChangeOperation, error) {
|
|
|
|
author, err := c.repoCache.GetUserIdentity()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.ForceChangeLabelsRaw(author, time.Now().Unix(), added, removed, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *BugCache) ForceChangeLabelsRaw(author *IdentityCache, unixTime int64, added []string, removed []string, metadata map[string]string) (*bug.LabelChangeOperation, error) {
|
|
|
|
op, err := bug.ForceChangeLabels(c.bug, author.Identity, unixTime, added, removed)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for key, value := range metadata {
|
|
|
|
op.SetMetadata(key, value)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = c.notifyUpdated()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return op, nil
|
|
|
|
}
|
|
|
|
|
2019-02-24 14:58:04 +03:00
|
|
|
func (c *BugCache) Open() (*bug.SetStatusOperation, error) {
|
2019-02-17 18:12:06 +03:00
|
|
|
author, err := c.repoCache.GetUserIdentity()
|
2018-08-21 19:46:53 +03:00
|
|
|
if err != nil {
|
2019-02-24 14:58:04 +03:00
|
|
|
return nil, err
|
2018-08-21 19:46:53 +03:00
|
|
|
}
|
|
|
|
|
2018-10-02 00:31:16 +03:00
|
|
|
return c.OpenRaw(author, time.Now().Unix(), nil)
|
2018-09-25 18:56:58 +03:00
|
|
|
}
|
|
|
|
|
2019-02-24 14:58:04 +03:00
|
|
|
func (c *BugCache) OpenRaw(author *IdentityCache, unixTime int64, metadata map[string]string) (*bug.SetStatusOperation, error) {
|
2019-02-17 18:12:06 +03:00
|
|
|
op, err := bug.Open(c.bug, author.Identity, unixTime)
|
2018-09-15 14:15:00 +03:00
|
|
|
if err != nil {
|
2019-02-24 14:58:04 +03:00
|
|
|
return nil, err
|
2018-09-15 14:15:00 +03:00
|
|
|
}
|
2018-08-21 19:46:53 +03:00
|
|
|
|
2018-10-02 00:31:16 +03:00
|
|
|
for key, value := range metadata {
|
|
|
|
op.SetMetadata(key, value)
|
|
|
|
}
|
|
|
|
|
2019-02-24 14:58:04 +03:00
|
|
|
return op, c.notifyUpdated()
|
2018-08-21 19:46:53 +03:00
|
|
|
}
|
|
|
|
|
2019-02-24 14:58:04 +03:00
|
|
|
func (c *BugCache) Close() (*bug.SetStatusOperation, error) {
|
2019-02-17 18:12:06 +03:00
|
|
|
author, err := c.repoCache.GetUserIdentity()
|
2018-08-21 19:46:53 +03:00
|
|
|
if err != nil {
|
2019-02-24 14:58:04 +03:00
|
|
|
return nil, err
|
2018-08-21 19:46:53 +03:00
|
|
|
}
|
|
|
|
|
2018-10-02 00:31:16 +03:00
|
|
|
return c.CloseRaw(author, time.Now().Unix(), nil)
|
2018-09-25 18:56:58 +03:00
|
|
|
}
|
|
|
|
|
2019-02-24 14:58:04 +03:00
|
|
|
func (c *BugCache) CloseRaw(author *IdentityCache, unixTime int64, metadata map[string]string) (*bug.SetStatusOperation, error) {
|
2019-02-17 18:12:06 +03:00
|
|
|
op, err := bug.Close(c.bug, author.Identity, unixTime)
|
2018-09-15 14:15:00 +03:00
|
|
|
if err != nil {
|
2019-02-24 14:58:04 +03:00
|
|
|
return nil, err
|
2018-09-15 14:15:00 +03:00
|
|
|
}
|
2018-08-21 19:46:53 +03:00
|
|
|
|
2018-10-02 00:31:16 +03:00
|
|
|
for key, value := range metadata {
|
|
|
|
op.SetMetadata(key, value)
|
|
|
|
}
|
|
|
|
|
2019-02-24 14:58:04 +03:00
|
|
|
return op, c.notifyUpdated()
|
2018-08-21 19:46:53 +03:00
|
|
|
}
|
|
|
|
|
2019-02-24 14:58:04 +03:00
|
|
|
func (c *BugCache) SetTitle(title string) (*bug.SetTitleOperation, error) {
|
2019-02-17 18:12:06 +03:00
|
|
|
author, err := c.repoCache.GetUserIdentity()
|
2018-08-21 19:46:53 +03:00
|
|
|
if err != nil {
|
2019-02-24 14:58:04 +03:00
|
|
|
return nil, err
|
2018-08-21 19:46:53 +03:00
|
|
|
}
|
|
|
|
|
2018-10-02 00:31:16 +03:00
|
|
|
return c.SetTitleRaw(author, time.Now().Unix(), title, nil)
|
|
|
|
}
|
|
|
|
|
2019-02-24 14:58:04 +03:00
|
|
|
func (c *BugCache) SetTitleRaw(author *IdentityCache, unixTime int64, title string, metadata map[string]string) (*bug.SetTitleOperation, error) {
|
2019-02-17 18:12:06 +03:00
|
|
|
op, err := bug.SetTitle(c.bug, author.Identity, unixTime, title)
|
2018-10-02 00:31:16 +03:00
|
|
|
if err != nil {
|
2019-02-24 14:58:04 +03:00
|
|
|
return nil, err
|
2018-10-02 00:31:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for key, value := range metadata {
|
|
|
|
op.SetMetadata(key, value)
|
|
|
|
}
|
|
|
|
|
2019-02-24 14:58:04 +03:00
|
|
|
return op, c.notifyUpdated()
|
2018-10-02 00:31:16 +03:00
|
|
|
}
|
|
|
|
|
2019-08-07 20:14:12 +03:00
|
|
|
func (c *BugCache) EditComment(target string, message string) (*bug.EditCommentOperation, error) {
|
2019-02-17 18:12:06 +03:00
|
|
|
author, err := c.repoCache.GetUserIdentity()
|
2018-10-02 00:31:16 +03:00
|
|
|
if err != nil {
|
2019-02-24 14:58:04 +03:00
|
|
|
return nil, err
|
2018-10-02 00:31:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return c.EditCommentRaw(author, time.Now().Unix(), target, message, nil)
|
2018-09-25 18:56:58 +03:00
|
|
|
}
|
|
|
|
|
2019-08-07 20:14:12 +03:00
|
|
|
func (c *BugCache) EditCommentRaw(author *IdentityCache, unixTime int64, target string, message string, metadata map[string]string) (*bug.EditCommentOperation, error) {
|
2019-02-17 18:12:06 +03:00
|
|
|
op, err := bug.EditComment(c.bug, author.Identity, unixTime, target, message)
|
2018-09-15 14:15:00 +03:00
|
|
|
if err != nil {
|
2019-02-24 14:58:04 +03:00
|
|
|
return nil, err
|
2018-09-15 14:15:00 +03:00
|
|
|
}
|
2018-08-21 19:46:53 +03:00
|
|
|
|
2018-10-02 00:31:16 +03:00
|
|
|
for key, value := range metadata {
|
|
|
|
op.SetMetadata(key, value)
|
|
|
|
}
|
|
|
|
|
2019-02-24 14:58:04 +03:00
|
|
|
return op, c.notifyUpdated()
|
2018-08-21 19:46:53 +03:00
|
|
|
}
|
|
|
|
|
2019-08-07 20:14:12 +03:00
|
|
|
func (c *BugCache) SetMetadata(target string, newMetadata map[string]string) (*bug.SetMetadataOperation, error) {
|
2019-06-15 03:50:09 +03:00
|
|
|
author, err := c.repoCache.GetUserIdentity()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-06-22 14:40:17 +03:00
|
|
|
return c.SetMetadataRaw(author, time.Now().Unix(), target, newMetadata)
|
2019-06-15 03:50:09 +03:00
|
|
|
}
|
|
|
|
|
2019-08-07 20:14:12 +03:00
|
|
|
func (c *BugCache) SetMetadataRaw(author *IdentityCache, unixTime int64, target string, newMetadata map[string]string) (*bug.SetMetadataOperation, error) {
|
2019-06-15 03:50:09 +03:00
|
|
|
op, err := bug.SetMetadata(c.bug, author.Identity, unixTime, target, newMetadata)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return op, c.notifyUpdated()
|
|
|
|
}
|
|
|
|
|
2018-08-21 19:46:53 +03:00
|
|
|
func (c *BugCache) Commit() error {
|
2019-02-18 16:11:37 +03:00
|
|
|
err := c.bug.Commit(c.repoCache.repo)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return c.notifyUpdated()
|
2018-08-21 19:46:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *BugCache) CommitAsNeeded() error {
|
2019-02-18 16:11:37 +03:00
|
|
|
err := c.bug.CommitAsNeeded(c.repoCache.repo)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return c.notifyUpdated()
|
2018-08-21 19:46:53 +03:00
|
|
|
}
|