From 5844dd0a6a08f496e6018c0bad0b38d82a8846fc Mon Sep 17 00:00:00 2001 From: Michael Muré Date: Thu, 29 Dec 2022 12:49:12 +0100 Subject: commands: share JSON creation --- commands/cmdjson/bug.go | 123 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 123 insertions(+) create mode 100644 commands/cmdjson/bug.go (limited to 'commands/cmdjson/bug.go') diff --git a/commands/cmdjson/bug.go b/commands/cmdjson/bug.go new file mode 100644 index 00000000..c5489caa --- /dev/null +++ b/commands/cmdjson/bug.go @@ -0,0 +1,123 @@ +package cmdjson + +import ( + "github.com/MichaelMure/git-bug/cache" + "github.com/MichaelMure/git-bug/entities/bug" +) + +type BugSnapshot struct { + Id string `json:"id"` + HumanId string `json:"human_id"` + CreateTime Time `json:"create_time"` + EditTime Time `json:"edit_time"` + Status string `json:"status"` + Labels []bug.Label `json:"labels"` + Title string `json:"title"` + Author Identity `json:"author"` + Actors []Identity `json:"actors"` + Participants []Identity `json:"participants"` + Comments []BugComment `json:"comments"` +} + +func NewBugSnapshot(snap *bug.Snapshot) BugSnapshot { + jsonBug := BugSnapshot{ + Id: snap.Id().String(), + HumanId: snap.Id().Human(), + CreateTime: NewTime(snap.CreateTime, 0), + EditTime: NewTime(snap.EditTime(), 0), + Status: snap.Status.String(), + Labels: snap.Labels, + Title: snap.Title, + Author: NewIdentity(snap.Author), + } + + jsonBug.Actors = make([]Identity, len(snap.Actors)) + for i, element := range snap.Actors { + jsonBug.Actors[i] = NewIdentity(element) + } + + jsonBug.Participants = make([]Identity, len(snap.Participants)) + for i, element := range snap.Participants { + jsonBug.Participants[i] = NewIdentity(element) + } + + jsonBug.Comments = make([]BugComment, len(snap.Comments)) + for i, comment := range snap.Comments { + jsonBug.Comments[i] = NewBugComment(comment) + } + + return jsonBug +} + +type BugComment struct { + Id string `json:"id"` + HumanId string `json:"human_id"` + Author Identity `json:"author"` + Message string `json:"message"` +} + +func NewBugComment(comment bug.Comment) BugComment { + return BugComment{ + Id: comment.CombinedId().String(), + HumanId: comment.CombinedId().Human(), + Author: NewIdentity(comment.Author), + Message: comment.Message, + } +} + +type BugExcerpt struct { + Id string `json:"id"` + HumanId string `json:"human_id"` + CreateTime Time `json:"create_time"` + EditTime Time `json:"edit_time"` + + Status string `json:"status"` + Labels []bug.Label `json:"labels"` + Title string `json:"title"` + Actors []Identity `json:"actors"` + Participants []Identity `json:"participants"` + Author Identity `json:"author"` + + Comments int `json:"comments"` + Metadata map[string]string `json:"metadata"` +} + +func NewBugExcerpt(backend *cache.RepoCache, excerpt *cache.BugExcerpt) (BugExcerpt, error) { + jsonBug := BugExcerpt{ + Id: excerpt.Id().String(), + HumanId: excerpt.Id().Human(), + CreateTime: NewTime(excerpt.CreateTime(), excerpt.CreateLamportTime), + EditTime: NewTime(excerpt.EditTime(), excerpt.EditLamportTime), + Status: excerpt.Status.String(), + Labels: excerpt.Labels, + Title: excerpt.Title, + Comments: excerpt.LenComments, + Metadata: excerpt.CreateMetadata, + } + + author, err := backend.Identities().ResolveExcerpt(excerpt.AuthorId) + if err != nil { + return BugExcerpt{}, err + } + jsonBug.Author = NewIdentityFromExcerpt(author) + + jsonBug.Actors = make([]Identity, len(excerpt.Actors)) + for i, element := range excerpt.Actors { + actor, err := backend.Identities().ResolveExcerpt(element) + if err != nil { + return BugExcerpt{}, err + } + jsonBug.Actors[i] = NewIdentityFromExcerpt(actor) + } + + jsonBug.Participants = make([]Identity, len(excerpt.Participants)) + for i, element := range excerpt.Participants { + participant, err := backend.Identities().ResolveExcerpt(element) + if err != nil { + return BugExcerpt{}, err + } + jsonBug.Participants[i] = NewIdentityFromExcerpt(participant) + } + + return jsonBug, nil +} -- cgit