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 Actors []identity.Interface
21 Participants []identity.Interface
22 CreatedAt time.Time
23
24 Timeline []TimelineItem
25
26 Operations []Operation
27}
28
29// Return the Bug identifier
30func (snap *Snapshot) Id() string {
31 return snap.id
32}
33
34// Return the Bug identifier truncated for human consumption
35func (snap *Snapshot) HumanId() string {
36 return FormatHumanID(snap.id)
37}
38
39// Return the last time a bug was modified
40func (snap *Snapshot) LastEditTime() time.Time {
41 if len(snap.Operations) == 0 {
42 return time.Unix(0, 0)
43 }
44
45 return snap.Operations[len(snap.Operations)-1].Time()
46}
47
48// Return the last timestamp a bug was modified
49func (snap *Snapshot) LastEditUnix() int64 {
50 if len(snap.Operations) == 0 {
51 return 0
52 }
53
54 return snap.Operations[len(snap.Operations)-1].GetUnixTime()
55}
56
57// GetCreateMetadata return the creation metadata
58func (snap *Snapshot) GetCreateMetadata(key string) (string, bool) {
59 return snap.Operations[0].GetMetadata(key)
60}
61
62// SearchTimelineItem will search in the timeline for an item matching the given hash
63func (snap *Snapshot) SearchTimelineItem(hash git.Hash) (TimelineItem, error) {
64 for i := range snap.Timeline {
65 if snap.Timeline[i].Hash() == hash {
66 return snap.Timeline[i], nil
67 }
68 }
69
70 return nil, fmt.Errorf("timeline item not found")
71}
72
73// append the operation author to the actors list
74func (snap *Snapshot) addActor(actor identity.Interface) {
75 for _, a := range snap.Actors {
76 if actor.Id() == a.Id() {
77 return
78 }
79 }
80
81 snap.Actors = append(snap.Actors, actor)
82}
83
84// append the operation author to the participants list
85func (snap *Snapshot) addParticipant(participant identity.Interface) {
86 for _, p := range snap.Participants {
87 if participant.Id() == p.Id() {
88 return
89 }
90 }
91
92 snap.Participants = append(snap.Participants, participant)
93}
94
95// HasParticipant return true if the id is a participant
96func (snap *Snapshot) HasParticipant(id string) bool {
97 for _, p := range snap.Participants {
98 if p.Id() == id {
99 return true
100 }
101 }
102 return false
103}
104
105// HasAnyParticipant return true if one of the ids is a participant
106func (snap *Snapshot) HasAnyParticipant(ids ...string) bool {
107 for _, id := range ids {
108 if snap.HasParticipant(id) {
109 return true
110 }
111 }
112 return false
113}
114
115// HasActor return true if the id is a actor
116func (snap *Snapshot) HasActor(id string) bool {
117 for _, p := range snap.Actors {
118 if p.Id() == id {
119 return true
120 }
121 }
122 return false
123}
124
125// HasAnyActor return true if one of the ids is a actor
126func (snap *Snapshot) HasAnyActor(ids ...string) bool {
127 for _, id := range ids {
128 if snap.HasActor(id) {
129 return true
130 }
131 }
132 return false
133}
134
135// Sign post method for gqlgen
136func (snap *Snapshot) IsAuthored() {}