snapshot.go

  1package bug
  2
  3import (
  4	"fmt"
  5	"time"
  6
  7	"github.com/MichaelMure/git-bug/entities/common"
  8	"github.com/MichaelMure/git-bug/entity"
  9)
 10
 11var _ entity.Snapshot = &Snapshot{}
 12
 13// Snapshot is a compiled form of the Bug data structure used for storage and merge
 14type Snapshot struct {
 15	id entity.Id
 16
 17	Status       common.Status
 18	Title        string
 19	Comments     []Comment
 20	Labels       []Label
 21	Author       entity.Identity
 22	Actors       []entity.Identity
 23	Participants []entity.Identity
 24	CreateTime   time.Time
 25
 26	Timeline []TimelineItem
 27
 28	Operations []entity.Operation
 29}
 30
 31// Id returns the Bug identifier
 32func (snap *Snapshot) Id() entity.Id {
 33	if snap.id == "" {
 34		// simply panic as it would be a coding error (no id provided at construction)
 35		panic("no id")
 36	}
 37	return snap.id
 38}
 39
 40func (snap *Snapshot) AllOperations() []entity.Operation {
 41	return snap.Operations
 42}
 43
 44func (snap *Snapshot) AppendOperation(op entity.Operation) {
 45	snap.Operations = append(snap.Operations, op)
 46}
 47
 48// EditTime returns the last time a bug was modified
 49func (snap *Snapshot) EditTime() time.Time {
 50	if len(snap.Operations) == 0 {
 51		return time.Unix(0, 0)
 52	}
 53
 54	return snap.Operations[len(snap.Operations)-1].Time()
 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(id entity.CombinedId) (TimelineItem, error) {
 64	for i := range snap.Timeline {
 65		if snap.Timeline[i].CombinedId() == id {
 66			return snap.Timeline[i], nil
 67		}
 68	}
 69
 70	return nil, fmt.Errorf("timeline item not found")
 71}
 72
 73// SearchComment will search for a comment matching the given id
 74func (snap *Snapshot) SearchComment(id entity.CombinedId) (*Comment, error) {
 75	for _, c := range snap.Comments {
 76		if c.combinedId == id {
 77			return &c, nil
 78		}
 79	}
 80
 81	return nil, fmt.Errorf("comment not found")
 82}
 83
 84// SearchCommentByOpId will search for a comment generated by the given operation Id
 85func (snap *Snapshot) SearchCommentByOpId(id entity.Id) (*Comment, error) {
 86	for _, c := range snap.Comments {
 87		if c.targetId == id {
 88			return &c, nil
 89		}
 90	}
 91
 92	return nil, fmt.Errorf("comment not found")
 93}
 94
 95// append the operation author to the actors list
 96func (snap *Snapshot) addActor(actor entity.Identity) {
 97	for _, a := range snap.Actors {
 98		if actor.Id() == a.Id() {
 99			return
100		}
101	}
102
103	snap.Actors = append(snap.Actors, actor)
104}
105
106// append the operation author to the participants list
107func (snap *Snapshot) addParticipant(participant entity.Identity) {
108	for _, p := range snap.Participants {
109		if participant.Id() == p.Id() {
110			return
111		}
112	}
113
114	snap.Participants = append(snap.Participants, participant)
115}
116
117// HasParticipant return true if the id is a participant
118func (snap *Snapshot) HasParticipant(id entity.Id) bool {
119	for _, p := range snap.Participants {
120		if p.Id() == id {
121			return true
122		}
123	}
124	return false
125}
126
127// HasAnyParticipant return true if one of the ids is a participant
128func (snap *Snapshot) HasAnyParticipant(ids ...entity.Id) bool {
129	for _, id := range ids {
130		if snap.HasParticipant(id) {
131			return true
132		}
133	}
134	return false
135}
136
137// HasActor return true if the id is a actor
138func (snap *Snapshot) HasActor(id entity.Id) bool {
139	for _, p := range snap.Actors {
140		if p.Id() == id {
141			return true
142		}
143	}
144	return false
145}
146
147// HasAnyActor return true if one of the ids is a actor
148func (snap *Snapshot) HasAnyActor(ids ...entity.Id) bool {
149	for _, id := range ids {
150		if snap.HasActor(id) {
151			return true
152		}
153	}
154	return false
155}
156
157// IsAuthored is a sign post method for gqlgen
158func (snap *Snapshot) IsAuthored() {}