aboutsummaryrefslogtreecommitdiffstats
path: root/cache/bug_cache.go
blob: c9e1f2f5fb540a4afb18adc4cb7709fa5b6f0d48 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package cache

import (
	"io"

	"github.com/MichaelMure/git-bug/bug"
	"github.com/MichaelMure/git-bug/bug/operations"
	"github.com/MichaelMure/git-bug/util"
)

type BugCache struct {
	repoCache *RepoCache
	bug       *bug.WithSnapshot
}

func NewBugCache(repoCache *RepoCache, b *bug.Bug) *BugCache {
	return &BugCache{
		repoCache: repoCache,
		bug:       &bug.WithSnapshot{Bug: b},
	}
}

func (c *BugCache) Snapshot() *bug.Snapshot {
	return c.bug.Snapshot()
}

func (c *BugCache) HumanId() string {
	return c.bug.HumanId()
}

func (c *BugCache) notifyUpdated() error {
	return c.repoCache.bugUpdated(c.bug.Id())
}

func (c *BugCache) AddComment(message string) error {
	if err := c.AddCommentWithFiles(message, nil); err != nil {
		return err
	}

	return c.notifyUpdated()
}

func (c *BugCache) AddCommentWithFiles(message string, files []util.Hash) error {
	author, err := bug.GetUser(c.repoCache.repo)
	if err != nil {
		return err
	}

	operations.CommentWithFiles(c.bug, author, message, files)

	return c.notifyUpdated()
}

func (c *BugCache) ChangeLabels(out io.Writer, added []string, removed []string) error {
	author, err := bug.GetUser(c.repoCache.repo)
	if err != nil {
		return err
	}

	err = operations.ChangeLabels(out, c.bug, author, added, removed)
	if err != nil {
		return err
	}

	return c.notifyUpdated()
}

func (c *BugCache) Open() error {
	author, err := bug.GetUser(c.repoCache.repo)
	if err != nil {
		return err
	}

	operations.Open(c.bug, author)

	return c.notifyUpdated()
}

func (c *BugCache) Close() error {
	author, err := bug.GetUser(c.repoCache.repo)
	if err != nil {
		return err
	}

	operations.Close(c.bug, author)

	return c.notifyUpdated()
}

func (c *BugCache) SetTitle(title string) error {
	author, err := bug.GetUser(c.repoCache.repo)
	if err != nil {
		return err
	}

	operations.SetTitle(c.bug, author, title)

	return c.notifyUpdated()
}

func (c *BugCache) Commit() error {
	return c.bug.Commit(c.repoCache.repo)
}

func (c *BugCache) CommitAsNeeded() error {
	if c.bug.HasPendingOp() {
		return c.bug.Commit(c.repoCache.repo)
	}
	return nil
}