2018-07-13 17:13:40 +03:00
|
|
|
package bug
|
|
|
|
|
2018-07-15 02:43:20 +03:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"time"
|
2018-09-29 21:41:19 +03:00
|
|
|
|
2019-08-11 15:08:03 +03:00
|
|
|
"github.com/MichaelMure/git-bug/entity"
|
2018-11-21 20:56:12 +03:00
|
|
|
"github.com/MichaelMure/git-bug/identity"
|
2018-07-15 02:43:20 +03:00
|
|
|
)
|
|
|
|
|
2018-07-13 17:13:40 +03:00
|
|
|
// Snapshot is a compiled form of the Bug data structure used for storage and merge
|
|
|
|
type Snapshot struct {
|
2019-08-11 15:08:03 +03:00
|
|
|
id entity.Id
|
2018-07-18 01:16:06 +03:00
|
|
|
|
2019-03-31 23:32:35 +03:00
|
|
|
Status Status
|
|
|
|
Title string
|
|
|
|
Comments []Comment
|
|
|
|
Labels []Label
|
|
|
|
Author identity.Interface
|
|
|
|
Actors []identity.Interface
|
|
|
|
Participants []identity.Interface
|
|
|
|
CreatedAt time.Time
|
2018-07-18 01:16:06 +03:00
|
|
|
|
2018-09-29 21:41:19 +03:00
|
|
|
Timeline []TimelineItem
|
|
|
|
|
2018-07-18 01:16:06 +03:00
|
|
|
Operations []Operation
|
2018-07-13 17:13:40 +03:00
|
|
|
}
|
2018-07-15 02:43:20 +03:00
|
|
|
|
2018-07-17 20:28:37 +03:00
|
|
|
// Return the Bug identifier
|
2019-08-11 15:08:03 +03:00
|
|
|
func (snap *Snapshot) Id() entity.Id {
|
2018-07-17 20:28:37 +03:00
|
|
|
return snap.id
|
|
|
|
}
|
|
|
|
|
2018-07-18 01:16:06 +03:00
|
|
|
// Return the last time a bug was modified
|
2018-09-29 21:41:19 +03:00
|
|
|
func (snap *Snapshot) LastEditTime() time.Time {
|
2018-07-18 01:16:06 +03:00
|
|
|
if len(snap.Operations) == 0 {
|
2018-07-17 21:51:09 +03:00
|
|
|
return time.Unix(0, 0)
|
|
|
|
}
|
2018-07-18 01:16:06 +03:00
|
|
|
|
|
|
|
return snap.Operations[len(snap.Operations)-1].Time()
|
2018-07-15 02:43:20 +03:00
|
|
|
}
|
2018-08-23 20:19:16 +03:00
|
|
|
|
|
|
|
// Return the last timestamp a bug was modified
|
2018-09-29 21:41:19 +03:00
|
|
|
func (snap *Snapshot) LastEditUnix() int64 {
|
2018-08-23 20:19:16 +03:00
|
|
|
if len(snap.Operations) == 0 {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2018-09-10 20:03:17 +03:00
|
|
|
return snap.Operations[len(snap.Operations)-1].GetUnixTime()
|
2018-08-23 20:19:16 +03:00
|
|
|
}
|
2018-09-29 21:41:19 +03:00
|
|
|
|
2019-06-23 20:20:10 +03:00
|
|
|
// GetCreateMetadata return the creation metadata
|
|
|
|
func (snap *Snapshot) GetCreateMetadata(key string) (string, bool) {
|
|
|
|
return snap.Operations[0].GetMetadata(key)
|
|
|
|
}
|
|
|
|
|
2018-09-29 21:41:19 +03:00
|
|
|
// SearchTimelineItem will search in the timeline for an item matching the given hash
|
2019-08-11 15:08:03 +03:00
|
|
|
func (snap *Snapshot) SearchTimelineItem(id entity.Id) (TimelineItem, error) {
|
2018-09-29 21:41:19 +03:00
|
|
|
for i := range snap.Timeline {
|
2019-08-11 15:08:03 +03:00
|
|
|
if snap.Timeline[i].Id() == id {
|
2018-09-29 21:41:19 +03:00
|
|
|
return snap.Timeline[i], nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, fmt.Errorf("timeline item not found")
|
|
|
|
}
|
2019-03-31 23:32:35 +03:00
|
|
|
|
2019-07-17 19:47:13 +03:00
|
|
|
// SearchComment will search for a comment matching the given hash
|
2019-08-12 17:12:14 +03:00
|
|
|
func (snap *Snapshot) SearchComment(id entity.Id) (*Comment, error) {
|
2019-07-17 19:47:13 +03:00
|
|
|
for _, c := range snap.Comments {
|
2019-08-12 17:12:14 +03:00
|
|
|
if c.id == id {
|
2019-07-17 19:47:13 +03:00
|
|
|
return &c, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, fmt.Errorf("comment item not found")
|
|
|
|
}
|
|
|
|
|
2019-03-31 23:32:35 +03:00
|
|
|
// 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)
|
|
|
|
}
|
2019-05-22 22:46:43 +03:00
|
|
|
|
2019-06-23 20:20:10 +03:00
|
|
|
// HasParticipant return true if the id is a participant
|
2019-08-11 15:08:03 +03:00
|
|
|
func (snap *Snapshot) HasParticipant(id entity.Id) bool {
|
2019-06-23 20:20:10 +03:00
|
|
|
for _, p := range snap.Participants {
|
|
|
|
if p.Id() == id {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// HasAnyParticipant return true if one of the ids is a participant
|
2019-08-11 15:08:03 +03:00
|
|
|
func (snap *Snapshot) HasAnyParticipant(ids ...entity.Id) bool {
|
2019-06-23 20:20:10 +03:00
|
|
|
for _, id := range ids {
|
|
|
|
if snap.HasParticipant(id) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// HasActor return true if the id is a actor
|
2019-08-11 15:08:03 +03:00
|
|
|
func (snap *Snapshot) HasActor(id entity.Id) bool {
|
2019-06-23 20:20:10 +03:00
|
|
|
for _, p := range snap.Actors {
|
|
|
|
if p.Id() == id {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// HasAnyActor return true if one of the ids is a actor
|
2019-08-11 15:08:03 +03:00
|
|
|
func (snap *Snapshot) HasAnyActor(ids ...entity.Id) bool {
|
2019-06-23 20:20:10 +03:00
|
|
|
for _, id := range ids {
|
|
|
|
if snap.HasActor(id) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-05-22 22:46:43 +03:00
|
|
|
// Sign post method for gqlgen
|
|
|
|
func (snap *Snapshot) IsAuthored() {}
|