snapshot.go

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