snapshot.go

 1package bug
 2
 3import (
 4	"fmt"
 5	"time"
 6
 7	"github.com/MichaelMure/git-bug/identity"
 8	"github.com/MichaelMure/git-bug/util/git"
 9)
10
11// Snapshot is a compiled form of the Bug data structure used for storage and merge
12type Snapshot struct {
13	id string
14
15	Status    Status
16	Title     string
17	Comments  []Comment
18	Labels    []Label
19	Author    identity.Interface
20	CreatedAt time.Time
21
22	Timeline []TimelineItem
23
24	Operations []Operation
25}
26
27// Return the Bug identifier
28func (snap *Snapshot) Id() string {
29	return snap.id
30}
31
32// Return the Bug identifier truncated for human consumption
33func (snap *Snapshot) HumanId() string {
34	return FormatHumanID(snap.id)
35}
36
37// Deprecated:should be moved in UI code
38func (snap *Snapshot) Summary() string {
39	return fmt.Sprintf("C:%d L:%d",
40		len(snap.Comments)-1,
41		len(snap.Labels),
42	)
43}
44
45// Return the last time a bug was modified
46func (snap *Snapshot) LastEditTime() time.Time {
47	if len(snap.Operations) == 0 {
48		return time.Unix(0, 0)
49	}
50
51	return snap.Operations[len(snap.Operations)-1].Time()
52}
53
54// Return the last timestamp a bug was modified
55func (snap *Snapshot) LastEditUnix() int64 {
56	if len(snap.Operations) == 0 {
57		return 0
58	}
59
60	return snap.Operations[len(snap.Operations)-1].GetUnixTime()
61}
62
63// SearchTimelineItem will search in the timeline for an item matching the given hash
64func (snap *Snapshot) SearchTimelineItem(hash git.Hash) (TimelineItem, error) {
65	for i := range snap.Timeline {
66		if snap.Timeline[i].Hash() == hash {
67			return snap.Timeline[i], nil
68		}
69	}
70
71	return nil, fmt.Errorf("timeline item not found")
72}