From 5511c230b678a181cc596238bf6669428d1b1902 Mon Sep 17 00:00:00 2001 From: Michael Muré Date: Thu, 18 Aug 2022 23:34:05 +0200 Subject: move {bug,identity} to /entities, move input to /commands --- entities/bug/snapshot.go | 144 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 144 insertions(+) create mode 100644 entities/bug/snapshot.go (limited to 'entities/bug/snapshot.go') diff --git a/entities/bug/snapshot.go b/entities/bug/snapshot.go new file mode 100644 index 00000000..cece09b8 --- /dev/null +++ b/entities/bug/snapshot.go @@ -0,0 +1,144 @@ +package bug + +import ( + "fmt" + "time" + + "github.com/MichaelMure/git-bug/entities/identity" + "github.com/MichaelMure/git-bug/entity" + "github.com/MichaelMure/git-bug/entity/dag" +) + +var _ dag.Snapshot = &Snapshot{} + +// Snapshot is a compiled form of the Bug data structure used for storage and merge +type Snapshot struct { + id entity.Id + + Status Status + Title string + Comments []Comment + Labels []Label + Author identity.Interface + Actors []identity.Interface + Participants []identity.Interface + CreateTime time.Time + + Timeline []TimelineItem + + Operations []dag.Operation +} + +// Id returns the Bug identifier +func (snap *Snapshot) Id() entity.Id { + if snap.id == "" { + // simply panic as it would be a coding error (no id provided at construction) + panic("no id") + } + return snap.id +} + +func (snap *Snapshot) AllOperations() []dag.Operation { + return snap.Operations +} + +// EditTime returns the last time a bug was modified +func (snap *Snapshot) EditTime() time.Time { + if len(snap.Operations) == 0 { + return time.Unix(0, 0) + } + + return snap.Operations[len(snap.Operations)-1].Time() +} + +// GetCreateMetadata return the creation metadata +func (snap *Snapshot) GetCreateMetadata(key string) (string, bool) { + return snap.Operations[0].GetMetadata(key) +} + +// SearchTimelineItem will search in the timeline for an item matching the given hash +func (snap *Snapshot) SearchTimelineItem(id entity.Id) (TimelineItem, error) { + for i := range snap.Timeline { + if snap.Timeline[i].Id() == id { + return snap.Timeline[i], nil + } + } + + return nil, fmt.Errorf("timeline item not found") +} + +// SearchComment will search for a comment matching the given hash +func (snap *Snapshot) SearchComment(id entity.Id) (*Comment, error) { + for _, c := range snap.Comments { + if c.id == id { + return &c, nil + } + } + + return nil, fmt.Errorf("comment item not found") +} + +// append the operation author to the actors list +func (snap *Snapshot) addActor(actor identity.Interface) { + for _, a := range snap.Actors { + if actor.Id() == a.Id() { + return + } + } + + snap.Actors = append(snap.Actors, actor) +} + +// append the operation author to the participants list +func (snap *Snapshot) addParticipant(participant identity.Interface) { + for _, p := range snap.Participants { + if participant.Id() == p.Id() { + return + } + } + + snap.Participants = append(snap.Participants, participant) +} + +// HasParticipant return true if the id is a participant +func (snap *Snapshot) HasParticipant(id entity.Id) bool { + for _, p := range snap.Participants { + if p.Id() == id { + return true + } + } + return false +} + +// HasAnyParticipant return true if one of the ids is a participant +func (snap *Snapshot) HasAnyParticipant(ids ...entity.Id) bool { + for _, id := range ids { + if snap.HasParticipant(id) { + return true + } + } + return false +} + +// HasActor return true if the id is a actor +func (snap *Snapshot) HasActor(id entity.Id) bool { + for _, p := range snap.Actors { + if p.Id() == id { + return true + } + } + return false +} + +// HasAnyActor return true if one of the ids is a actor +func (snap *Snapshot) HasAnyActor(ids ...entity.Id) bool { + for _, id := range ids { + if snap.HasActor(id) { + return true + } + } + return false +} + +// IsAuthored is a sign post method for gqlgen +func (snap *Snapshot) IsAuthored() {} -- cgit