snapshot.go

  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	if snap.id == "" {
 32		// simply panic as it would be a coding error
 33		// (using an id of a bug not stored yet)
 34		panic("no id yet")
 35	}
 36	return snap.id
 37}
 38
 39// Return the last time a bug was modified
 40func (snap *Snapshot) EditTime() 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// GetCreateMetadata return the creation metadata
 49func (snap *Snapshot) GetCreateMetadata(key string) (string, bool) {
 50	return snap.Operations[0].GetMetadata(key)
 51}
 52
 53// SearchTimelineItem will search in the timeline for an item matching the given hash
 54func (snap *Snapshot) SearchTimelineItem(id entity.Id) (TimelineItem, error) {
 55	for i := range snap.Timeline {
 56		if snap.Timeline[i].Id() == id {
 57			return snap.Timeline[i], nil
 58		}
 59	}
 60
 61	return nil, fmt.Errorf("timeline item not found")
 62}
 63
 64// SearchComment will search for a comment matching the given hash
 65func (snap *Snapshot) SearchComment(id entity.Id) (*Comment, error) {
 66	for _, c := range snap.Comments {
 67		if c.id == id {
 68			return &c, nil
 69		}
 70	}
 71
 72	return nil, fmt.Errorf("comment item not found")
 73}
 74
 75// append the operation author to the actors list
 76func (snap *Snapshot) addActor(actor identity.Interface) {
 77	for _, a := range snap.Actors {
 78		if actor.Id() == a.Id() {
 79			return
 80		}
 81	}
 82
 83	snap.Actors = append(snap.Actors, actor)
 84}
 85
 86// append the operation author to the participants list
 87func (snap *Snapshot) addParticipant(participant identity.Interface) {
 88	for _, p := range snap.Participants {
 89		if participant.Id() == p.Id() {
 90			return
 91		}
 92	}
 93
 94	snap.Participants = append(snap.Participants, participant)
 95}
 96
 97// HasParticipant return true if the id is a participant
 98func (snap *Snapshot) HasParticipant(id entity.Id) bool {
 99	for _, p := range snap.Participants {
100		if p.Id() == id {
101			return true
102		}
103	}
104	return false
105}
106
107// HasAnyParticipant return true if one of the ids is a participant
108func (snap *Snapshot) HasAnyParticipant(ids ...entity.Id) bool {
109	for _, id := range ids {
110		if snap.HasParticipant(id) {
111			return true
112		}
113	}
114	return false
115}
116
117// HasActor return true if the id is a actor
118func (snap *Snapshot) HasActor(id entity.Id) bool {
119	for _, p := range snap.Actors {
120		if p.Id() == id {
121			return true
122		}
123	}
124	return false
125}
126
127// HasAnyActor return true if one of the ids is a actor
128func (snap *Snapshot) HasAnyActor(ids ...entity.Id) bool {
129	for _, id := range ids {
130		if snap.HasActor(id) {
131			return true
132		}
133	}
134	return false
135}
136
137// Sign post method for gqlgen
138func (snap *Snapshot) IsAuthored() {}