2018-08-21 19:46:53 +03:00
|
|
|
package cache
|
|
|
|
|
|
|
|
import (
|
2018-10-02 00:34:45 +03:00
|
|
|
"fmt"
|
2020-08-13 12:08:56 +03:00
|
|
|
"sync"
|
2018-09-25 18:56:58 +03:00
|
|
|
"time"
|
|
|
|
|
2022-08-19 00:34:05 +03:00
|
|
|
"github.com/MichaelMure/git-bug/entities/bug"
|
2019-08-12 17:12:14 +03:00
|
|
|
"github.com/MichaelMure/git-bug/entity"
|
2022-07-25 14:16:16 +03:00
|
|
|
"github.com/MichaelMure/git-bug/entity/dag"
|
2020-07-01 20:39:02 +03:00
|
|
|
"github.com/MichaelMure/git-bug/repository"
|
2018-08-21 19:46:53 +03:00
|
|
|
)
|
|
|
|
|
2019-08-12 17:12:14 +03:00
|
|
|
var ErrNoMatchingOp = fmt.Errorf("no matching operation found")
|
|
|
|
|
2022-03-10 18:27:22 +03:00
|
|
|
// BugCache is a wrapper around a Bug. It provides multiple functions:
|
2019-01-19 18:01:06 +03:00
|
|
|
//
|
|
|
|
// 1. Provide a higher level API to use than the raw API from Bug.
|
2022-03-10 18:27:22 +03:00
|
|
|
// 2. Maintain an up-to-date Snapshot available.
|
2020-08-13 12:08:56 +03:00
|
|
|
// 3. Deal with concurrency.
|
2018-08-21 19:46:53 +03:00
|
|
|
type BugCache struct {
|
2018-08-23 22:24:57 +03:00
|
|
|
repoCache *RepoCache
|
2020-08-13 15:26:41 +03:00
|
|
|
mu sync.RWMutex
|
2018-08-23 22:24:57 +03:00
|
|
|
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 {
|
2020-08-13 12:08:56 +03:00
|
|
|
c.mu.RLock()
|
|
|
|
defer c.mu.RUnlock()
|
2022-07-31 15:38:32 +03:00
|
|
|
return c.bug.Compile()
|
2018-08-21 19:46:53 +03:00
|
|
|
}
|
|
|
|
|
2019-08-12 17:12:14 +03:00
|
|
|
func (c *BugCache) Id() entity.Id {
|
2018-09-18 13:49:16 +03:00
|
|
|
return c.bug.Id()
|
|
|
|
}
|
|
|
|
|
2018-08-23 22:24:57 +03:00
|
|
|
func (c *BugCache) notifyUpdated() error {
|
|
|
|
return c.repoCache.bugUpdated(c.bug.Id())
|
|
|
|
}
|
|
|
|
|
2019-02-24 14:58:04 +03:00
|
|
|
// ResolveOperationWithMetadata will find an operation that has the matching metadata
|
2019-08-12 17:12:14 +03:00
|
|
|
func (c *BugCache) ResolveOperationWithMetadata(key string, value string) (entity.Id, error) {
|
2020-08-13 12:08:56 +03:00
|
|
|
c.mu.RLock()
|
|
|
|
defer c.mu.RUnlock()
|
2018-10-02 00:34:45 +03:00
|
|
|
// preallocate but empty
|
2019-08-12 17:12:14 +03:00
|
|
|
matching := make([]entity.Id, 0, 5)
|
2018-10-02 00:34:45 +03:00
|
|
|
|
2021-02-14 13:36:32 +03:00
|
|
|
for _, op := range c.bug.Operations() {
|
2018-10-02 00:34:45 +03:00
|
|
|
opValue, ok := op.GetMetadata(key)
|
|
|
|
if ok && value == opValue {
|
2019-08-12 17:12:14 +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 {
|
2019-08-12 17:12:14 +03:00
|
|
|
return "", bug.NewErrMultipleMatchOp(matching)
|
2018-10-02 00:34:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return matching[0], nil
|
|
|
|
}
|
|
|
|
|
2022-11-13 14:31:38 +03:00
|
|
|
func (c *BugCache) AddComment(message string) (entity.CombinedId, *bug.AddCommentOperation, error) {
|
2018-09-25 18:56:58 +03:00
|
|
|
return c.AddCommentWithFiles(message, nil)
|
2018-08-21 19:46:53 +03:00
|
|
|
}
|
|
|
|
|
2022-11-13 14:31:38 +03:00
|
|
|
func (c *BugCache) AddCommentWithFiles(message string, files []repository.Hash) (entity.CombinedId, *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 {
|
2022-11-13 14:31:38 +03:00
|
|
|
return entity.UnsetCombinedId, 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)
|
|
|
|
}
|
|
|
|
|
2022-11-13 14:31:38 +03:00
|
|
|
func (c *BugCache) AddCommentRaw(author *IdentityCache, unixTime int64, message string, files []repository.Hash, metadata map[string]string) (entity.CombinedId, *bug.AddCommentOperation, error) {
|
2020-08-13 12:08:56 +03:00
|
|
|
c.mu.Lock()
|
2022-11-13 14:31:38 +03:00
|
|
|
commentId, op, err := bug.AddComment(c.bug, author, unixTime, message, files, metadata)
|
2022-07-31 15:38:32 +03:00
|
|
|
c.mu.Unlock()
|
2018-09-15 14:15:00 +03:00
|
|
|
if err != nil {
|
2022-11-13 14:31:38 +03:00
|
|
|
return entity.UnsetCombinedId, nil, err
|
2018-09-15 14:15:00 +03:00
|
|
|
}
|
2022-11-13 14:31:38 +03:00
|
|
|
return commentId, 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) {
|
2020-08-13 12:08:56 +03:00
|
|
|
c.mu.Lock()
|
2022-07-31 15:38:32 +03:00
|
|
|
changes, op, err := bug.ChangeLabels(c.bug, author.Identity, unixTime, added, removed, metadata)
|
2020-08-18 15:22:04 +03:00
|
|
|
c.mu.Unlock()
|
2018-09-13 13:20:28 +03:00
|
|
|
if err != nil {
|
2022-07-31 15:38:32 +03:00
|
|
|
return changes, nil, err
|
2018-09-13 13:20:28 +03:00
|
|
|
}
|
2022-07-31 15:38:32 +03:00
|
|
|
return changes, op, c.notifyUpdated()
|
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) {
|
2020-08-13 12:08:56 +03:00
|
|
|
c.mu.Lock()
|
2022-07-31 15:38:32 +03:00
|
|
|
op, err := bug.ForceChangeLabels(c.bug, author.Identity, unixTime, added, removed, metadata)
|
2020-08-18 15:22:04 +03:00
|
|
|
c.mu.Unlock()
|
2019-05-04 14:19:56 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-07-31 15:38:32 +03:00
|
|
|
return op, c.notifyUpdated()
|
2019-05-04 14:19:56 +03:00
|
|
|
}
|
|
|
|
|
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) {
|
2020-08-13 12:08:56 +03:00
|
|
|
c.mu.Lock()
|
2022-07-31 15:38:32 +03:00
|
|
|
op, err := bug.Open(c.bug, author.Identity, unixTime, metadata)
|
|
|
|
c.mu.Unlock()
|
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
|
|
|
}
|
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) {
|
2020-08-13 12:08:56 +03:00
|
|
|
c.mu.Lock()
|
2022-07-31 15:38:32 +03:00
|
|
|
op, err := bug.Close(c.bug, author.Identity, unixTime, metadata)
|
|
|
|
c.mu.Unlock()
|
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
|
|
|
}
|
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) {
|
2020-08-13 12:08:56 +03:00
|
|
|
c.mu.Lock()
|
2022-07-31 15:38:32 +03:00
|
|
|
op, err := bug.SetTitle(c.bug, author.Identity, unixTime, title, metadata)
|
|
|
|
c.mu.Unlock()
|
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
|
|
|
}
|
2019-02-24 14:58:04 +03:00
|
|
|
return op, c.notifyUpdated()
|
2018-10-02 00:31:16 +03:00
|
|
|
}
|
|
|
|
|
2022-03-10 18:27:22 +03:00
|
|
|
// EditCreateComment is a convenience function to edit the body of a bug (the first comment)
|
2022-11-13 14:31:38 +03:00
|
|
|
func (c *BugCache) EditCreateComment(body string) (entity.CombinedId, *bug.EditCommentOperation, error) {
|
2019-11-07 19:01:08 +03:00
|
|
|
author, err := c.repoCache.GetUserIdentity()
|
|
|
|
if err != nil {
|
2022-11-13 14:31:38 +03:00
|
|
|
return entity.UnsetCombinedId, nil, err
|
2019-11-07 19:01:08 +03:00
|
|
|
}
|
|
|
|
|
2019-11-23 09:34:19 +03:00
|
|
|
return c.EditCreateCommentRaw(author, time.Now().Unix(), body, nil)
|
2019-11-07 19:01:08 +03:00
|
|
|
}
|
|
|
|
|
2022-03-10 18:27:22 +03:00
|
|
|
// EditCreateCommentRaw is a convenience function to edit the body of a bug (the first comment)
|
2022-11-13 14:31:38 +03:00
|
|
|
func (c *BugCache) EditCreateCommentRaw(author *IdentityCache, unixTime int64, body string, metadata map[string]string) (entity.CombinedId, *bug.EditCommentOperation, error) {
|
2020-08-13 12:08:56 +03:00
|
|
|
c.mu.Lock()
|
2022-11-13 14:31:38 +03:00
|
|
|
commentId, op, err := bug.EditCreateComment(c.bug, author.Identity, unixTime, body, nil, metadata)
|
2022-07-31 15:38:32 +03:00
|
|
|
c.mu.Unlock()
|
2019-11-07 19:01:08 +03:00
|
|
|
if err != nil {
|
2022-11-13 14:31:38 +03:00
|
|
|
return entity.UnsetCombinedId, nil, err
|
2019-11-07 19:01:08 +03:00
|
|
|
}
|
2022-11-13 14:31:38 +03:00
|
|
|
return commentId, op, c.notifyUpdated()
|
2019-11-07 19:01:08 +03:00
|
|
|
}
|
|
|
|
|
2021-05-03 12:45:15 +03:00
|
|
|
func (c *BugCache) EditComment(target entity.CombinedId, 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
|
|
|
}
|
|
|
|
|
2021-05-03 12:45:15 +03:00
|
|
|
func (c *BugCache) EditCommentRaw(author *IdentityCache, unixTime int64, target entity.CombinedId, message string, metadata map[string]string) (*bug.EditCommentOperation, error) {
|
|
|
|
comment, err := c.Snapshot().SearchComment(target)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-08-13 12:08:56 +03:00
|
|
|
c.mu.Lock()
|
2022-11-13 14:31:38 +03:00
|
|
|
commentId, op, err := bug.EditComment(c.bug, author.Identity, unixTime, comment.TargetId(), message, nil, metadata)
|
2022-07-31 15:38:32 +03:00
|
|
|
c.mu.Unlock()
|
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
|
|
|
}
|
2022-11-13 14:31:38 +03:00
|
|
|
if commentId != target {
|
|
|
|
panic("EditComment returned unexpected comment id")
|
|
|
|
}
|
2019-02-24 14:58:04 +03:00
|
|
|
return op, c.notifyUpdated()
|
2018-08-21 19:46:53 +03:00
|
|
|
}
|
|
|
|
|
2022-07-25 14:16:16 +03:00
|
|
|
func (c *BugCache) SetMetadata(target entity.Id, newMetadata map[string]string) (*dag.SetMetadataOperation[*bug.Snapshot], 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
|
|
|
}
|
|
|
|
|
2022-07-25 14:16:16 +03:00
|
|
|
func (c *BugCache) SetMetadataRaw(author *IdentityCache, unixTime int64, target entity.Id, newMetadata map[string]string) (*dag.SetMetadataOperation[*bug.Snapshot], error) {
|
2020-08-13 12:08:56 +03:00
|
|
|
c.mu.Lock()
|
2019-06-15 03:50:09 +03:00
|
|
|
op, err := bug.SetMetadata(c.bug, author.Identity, unixTime, target, newMetadata)
|
2022-07-31 15:38:32 +03:00
|
|
|
c.mu.Unlock()
|
2019-06-15 03:50:09 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return op, c.notifyUpdated()
|
|
|
|
}
|
|
|
|
|
2018-08-21 19:46:53 +03:00
|
|
|
func (c *BugCache) Commit() error {
|
2020-08-13 12:08:56 +03:00
|
|
|
c.mu.Lock()
|
2019-02-18 16:11:37 +03:00
|
|
|
err := c.bug.Commit(c.repoCache.repo)
|
|
|
|
if err != nil {
|
2020-08-18 15:22:04 +03:00
|
|
|
c.mu.Unlock()
|
2019-02-18 16:11:37 +03:00
|
|
|
return err
|
|
|
|
}
|
2020-08-18 15:22:04 +03:00
|
|
|
c.mu.Unlock()
|
2019-02-18 16:11:37 +03:00
|
|
|
return c.notifyUpdated()
|
2018-08-21 19:46:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *BugCache) CommitAsNeeded() error {
|
2020-08-13 12:08:56 +03:00
|
|
|
c.mu.Lock()
|
2019-02-18 16:11:37 +03:00
|
|
|
err := c.bug.CommitAsNeeded(c.repoCache.repo)
|
|
|
|
if err != nil {
|
2020-08-18 15:22:04 +03:00
|
|
|
c.mu.Unlock()
|
2019-02-18 16:11:37 +03:00
|
|
|
return err
|
|
|
|
}
|
2020-08-18 15:22:04 +03:00
|
|
|
c.mu.Unlock()
|
2019-02-18 16:11:37 +03:00
|
|
|
return c.notifyUpdated()
|
2018-08-21 19:46:53 +03:00
|
|
|
}
|
2019-11-18 03:31:43 +03:00
|
|
|
|
|
|
|
func (c *BugCache) NeedCommit() bool {
|
2020-08-13 12:08:56 +03:00
|
|
|
c.mu.RLock()
|
|
|
|
defer c.mu.RUnlock()
|
2019-11-18 03:31:43 +03:00
|
|
|
return c.bug.NeedCommit()
|
|
|
|
}
|