aboutsummaryrefslogtreecommitdiffstats
path: root/commands/cmdjson/bug.go
diff options
context:
space:
mode:
authorMichael Muré <batolettre@gmail.com>2022-12-29 12:49:12 +0100
committerMichael Muré <batolettre@gmail.com>2022-12-29 12:53:06 +0100
commit5844dd0a6a08f496e6018c0bad0b38d82a8846fc (patch)
treee0a22258e29c12bec617b18298bea6bb377aede6 /commands/cmdjson/bug.go
parent211a038c6057c5cfdcbf4c7d4cc8b4e93d8dc8db (diff)
downloadgit-bug-5844dd0a6a08f496e6018c0bad0b38d82a8846fc.tar.gz
commands: share JSON creation
Diffstat (limited to 'commands/cmdjson/bug.go')
-rw-r--r--commands/cmdjson/bug.go123
1 files changed, 123 insertions, 0 deletions
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
+}