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