snapshot.go

  1package bug
  2
  3import (
  4	"fmt"
  5	"time"
  6
  7	"github.com/MichaelMure/git-bug/identity"
  8	"github.com/MichaelMure/git-bug/util/git"
  9)
 10
 11// Snapshot is a compiled form of the Bug data structure used for storage and merge
 12type Snapshot struct {
 13	id string
 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	CreatedAt    time.Time
 23
 24	Timeline []TimelineItem
 25
 26	Operations []Operation
 27}
 28
 29// Return the Bug identifier
 30func (snap *Snapshot) Id() string {
 31	return snap.id
 32}
 33
 34// Return the Bug identifier truncated for human consumption
 35func (snap *Snapshot) HumanId() string {
 36	return FormatHumanID(snap.id)
 37}
 38
 39// Return the last time a bug was modified
 40func (snap *Snapshot) LastEditTime() 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// Return the last timestamp a bug was modified
 49func (snap *Snapshot) LastEditUnix() int64 {
 50	if len(snap.Operations) == 0 {
 51		return 0
 52	}
 53
 54	return snap.Operations[len(snap.Operations)-1].GetUnixTime()
 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(hash git.Hash) (TimelineItem, error) {
 64	for i := range snap.Timeline {
 65		if snap.Timeline[i].Hash() == hash {
 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 hash
 74func (snap *Snapshot) SearchComment(hash git.Hash) (*Comment, error) {
 75	for _, c := range snap.Comments {
 76		if c.id == hash.String() {
 77			return &c, nil
 78		}
 79	}
 80
 81	return nil, fmt.Errorf("comment item not found")
 82}
 83
 84// append the operation author to the actors list
 85func (snap *Snapshot) addActor(actor identity.Interface) {
 86	for _, a := range snap.Actors {
 87		if actor.Id() == a.Id() {
 88			return
 89		}
 90	}
 91
 92	snap.Actors = append(snap.Actors, actor)
 93}
 94
 95// append the operation author to the participants list
 96func (snap *Snapshot) addParticipant(participant identity.Interface) {
 97	for _, p := range snap.Participants {
 98		if participant.Id() == p.Id() {
 99			return
100		}
101	}
102
103	snap.Participants = append(snap.Participants, participant)
104}
105
106// HasParticipant return true if the id is a participant
107func (snap *Snapshot) HasParticipant(id string) bool {
108	for _, p := range snap.Participants {
109		if p.Id() == id {
110			return true
111		}
112	}
113	return false
114}
115
116// HasAnyParticipant return true if one of the ids is a participant
117func (snap *Snapshot) HasAnyParticipant(ids ...string) bool {
118	for _, id := range ids {
119		if snap.HasParticipant(id) {
120			return true
121		}
122	}
123	return false
124}
125
126// HasActor return true if the id is a actor
127func (snap *Snapshot) HasActor(id string) bool {
128	for _, p := range snap.Actors {
129		if p.Id() == id {
130			return true
131		}
132	}
133	return false
134}
135
136// HasAnyActor return true if one of the ids is a actor
137func (snap *Snapshot) HasAnyActor(ids ...string) bool {
138	for _, id := range ids {
139		if snap.HasActor(id) {
140			return true
141		}
142	}
143	return false
144}
145
146// Sign post method for gqlgen
147func (snap *Snapshot) IsAuthored() {}