snapshot.go

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