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