aboutsummaryrefslogtreecommitdiffstats
path: root/cache/cache.go
diff options
context:
space:
mode:
authorMichael Muré <batolettre@gmail.com>2018-08-01 21:57:12 +0200
committerMichael Muré <batolettre@gmail.com>2018-08-01 21:57:12 +0200
commite5a6a71b78b0d8c5ef0d52c3eeb279d5738b9cf7 (patch)
treeb61cfb5f121a6988aa811566c8cc04e4834c87b8 /cache/cache.go
parent15f282421941b90e1f62912cf68b7556a8ce7b33 (diff)
downloadgit-bug-e5a6a71b78b0d8c5ef0d52c3eeb279d5738b9cf7.tar.gz
graphql: implement the missing mutations
Diffstat (limited to 'cache/cache.go')
-rw-r--r--cache/cache.go124
1 files changed, 121 insertions, 3 deletions
diff --git a/cache/cache.go b/cache/cache.go
index b46ea83f..4d6a93d1 100644
--- a/cache/cache.go
+++ b/cache/cache.go
@@ -27,15 +27,23 @@ type RepoCacher interface {
ResolveBugPrefix(prefix string) (BugCacher, error)
AllBugIds() ([]string, error)
ClearAllBugs()
+ Commit(bug BugCacher) error
// Mutations
NewBug(title string, message string) (BugCacher, error)
+
+ AddComment(repoRef *string, prefix string, message string) (BugCacher, error)
+ ChangeLabels(repoRef *string, prefix string, added []string, removed []string) (BugCacher, error)
+ Open(repoRef *string, prefix string) (BugCacher, error)
+ Close(repoRef *string, prefix string) (BugCacher, error)
+ SetTitle(repoRef *string, prefix string, title string) (BugCacher, error)
}
type BugCacher interface {
Snapshot() *bug.Snapshot
ClearSnapshot()
+ bug() *bug.Bug
}
// Cacher ------------------------
@@ -174,6 +182,14 @@ func (c *RepoCache) ClearAllBugs() {
c.bugs = make(map[string]BugCacher)
}
+func (c *RepoCache) Commit(bug BugCacher) error {
+ err := bug.bug().Commit(c.repo)
+ if err != nil {
+ return err
+ }
+ return nil
+}
+
func (c *RepoCache) NewBug(title string, message string) (BugCacher, error) {
author, err := bug.GetUser(c.repo)
if err != nil {
@@ -196,22 +212,120 @@ func (c *RepoCache) NewBug(title string, message string) (BugCacher, error) {
return cached, nil
}
+func (c *RepoCache) AddComment(repoRef *string, prefix string, message string) (BugCacher, error) {
+ author, err := bug.GetUser(c.repo)
+ if err != nil {
+ return nil, err
+ }
+
+ cached, err := c.ResolveBugPrefix(prefix)
+ if err != nil {
+ return nil, err
+ }
+
+ operations.Comment(cached.bug(), author, message)
+
+ // TODO: perf --> the snapshot could simply be updated with the new op
+ cached.ClearSnapshot()
+
+ return cached, nil
+}
+
+func (c *RepoCache) ChangeLabels(repoRef *string, prefix string, added []string, removed []string) (BugCacher, error) {
+ author, err := bug.GetUser(c.repo)
+ if err != nil {
+ return nil, err
+ }
+
+ cached, err := c.ResolveBugPrefix(prefix)
+ if err != nil {
+ return nil, err
+ }
+
+ err = operations.ChangeLabels(nil, cached.bug(), author, added, removed)
+ if err != nil {
+ return nil, err
+ }
+
+ // TODO: perf --> the snapshot could simply be updated with the new op
+ cached.ClearSnapshot()
+
+ return cached, nil
+}
+
+func (c *RepoCache) Open(repoRef *string, prefix string) (BugCacher, error) {
+ author, err := bug.GetUser(c.repo)
+ if err != nil {
+ return nil, err
+ }
+
+ cached, err := c.ResolveBugPrefix(prefix)
+ if err != nil {
+ return nil, err
+ }
+
+ operations.Open(cached.bug(), author)
+
+ // TODO: perf --> the snapshot could simply be updated with the new op
+ cached.ClearSnapshot()
+
+ return cached, nil
+}
+
+func (c *RepoCache) Close(repoRef *string, prefix string) (BugCacher, error) {
+ author, err := bug.GetUser(c.repo)
+ if err != nil {
+ return nil, err
+ }
+
+ cached, err := c.ResolveBugPrefix(prefix)
+ if err != nil {
+ return nil, err
+ }
+
+ operations.Close(cached.bug(), author)
+
+ // TODO: perf --> the snapshot could simply be updated with the new op
+ cached.ClearSnapshot()
+
+ return cached, nil
+}
+
+func (c *RepoCache) SetTitle(repoRef *string, prefix string, title string) (BugCacher, error) {
+ author, err := bug.GetUser(c.repo)
+ if err != nil {
+ return nil, err
+ }
+
+ cached, err := c.ResolveBugPrefix(prefix)
+ if err != nil {
+ return nil, err
+ }
+
+ operations.SetTitle(cached.bug(), author, title)
+
+ // TODO: perf --> the snapshot could simply be updated with the new op
+ cached.ClearSnapshot()
+
+ return cached, nil
+}
+
// Bug ------------------------
type BugCache struct {
- bug *bug.Bug
+ b *bug.Bug
snap *bug.Snapshot
}
func NewBugCache(b *bug.Bug) BugCacher {
return &BugCache{
- bug: b,
+ b: b,
}
}
func (c *BugCache) Snapshot() *bug.Snapshot {
if c.snap == nil {
- snap := c.bug.Compile()
+ snap := c.b.Compile()
c.snap = &snap
}
return c.snap
@@ -220,3 +334,7 @@ func (c *BugCache) Snapshot() *bug.Snapshot {
func (c *BugCache) ClearSnapshot() {
c.snap = nil
}
+
+func (c *BugCache) bug() *bug.Bug {
+ return c.b
+}