1package cmdjson
2
3import (
4 "github.com/MichaelMure/git-bug/cache"
5 "github.com/MichaelMure/git-bug/entities/bug"
6)
7
8type BugSnapshot struct {
9 Id string `json:"id"`
10 HumanId string `json:"human_id"`
11 CreateTime Time `json:"create_time"`
12 EditTime Time `json:"edit_time"`
13 Status string `json:"status"`
14 Labels []bug.Label `json:"labels"`
15 Title string `json:"title"`
16 Author Identity `json:"author"`
17 Actors []Identity `json:"actors"`
18 Participants []Identity `json:"participants"`
19 Comments []BugComment `json:"comments"`
20}
21
22func NewBugSnapshot(snap *bug.Snapshot) BugSnapshot {
23 jsonBug := BugSnapshot{
24 Id: snap.Id().String(),
25 HumanId: snap.Id().Human(),
26 CreateTime: NewTime(snap.CreateTime, 0),
27 EditTime: NewTime(snap.EditTime(), 0),
28 Status: snap.Status.String(),
29 Labels: snap.Labels,
30 Title: snap.Title,
31 Author: NewIdentity(snap.Author),
32 }
33
34 jsonBug.Actors = make([]Identity, len(snap.Actors))
35 for i, element := range snap.Actors {
36 jsonBug.Actors[i] = NewIdentity(element)
37 }
38
39 jsonBug.Participants = make([]Identity, len(snap.Participants))
40 for i, element := range snap.Participants {
41 jsonBug.Participants[i] = NewIdentity(element)
42 }
43
44 jsonBug.Comments = make([]BugComment, len(snap.Comments))
45 for i, comment := range snap.Comments {
46 jsonBug.Comments[i] = NewBugComment(comment)
47 }
48
49 return jsonBug
50}
51
52type BugComment struct {
53 Id string `json:"id"`
54 HumanId string `json:"human_id"`
55 Author Identity `json:"author"`
56 Message string `json:"message"`
57}
58
59func NewBugComment(comment bug.Comment) BugComment {
60 return BugComment{
61 Id: comment.CombinedId().String(),
62 HumanId: comment.CombinedId().Human(),
63 Author: NewIdentity(comment.Author),
64 Message: comment.Message,
65 }
66}
67
68type BugExcerpt struct {
69 Id string `json:"id"`
70 HumanId string `json:"human_id"`
71 CreateTime Time `json:"create_time"`
72 EditTime Time `json:"edit_time"`
73
74 Status string `json:"status"`
75 Labels []bug.Label `json:"labels"`
76 Title string `json:"title"`
77 Actors []Identity `json:"actors"`
78 Participants []Identity `json:"participants"`
79 Author Identity `json:"author"`
80
81 Comments int `json:"comments"`
82 Metadata map[string]string `json:"metadata"`
83}
84
85func NewBugExcerpt(backend *cache.RepoCache, excerpt *cache.BugExcerpt) (BugExcerpt, error) {
86 jsonBug := BugExcerpt{
87 Id: excerpt.Id().String(),
88 HumanId: excerpt.Id().Human(),
89 CreateTime: NewTime(excerpt.CreateTime(), excerpt.CreateLamportTime),
90 EditTime: NewTime(excerpt.EditTime(), excerpt.EditLamportTime),
91 Status: excerpt.Status.String(),
92 Labels: excerpt.Labels,
93 Title: excerpt.Title,
94 Comments: excerpt.LenComments,
95 Metadata: excerpt.CreateMetadata,
96 }
97
98 author, err := backend.Identities().ResolveExcerpt(excerpt.AuthorId)
99 if err != nil {
100 return BugExcerpt{}, err
101 }
102 jsonBug.Author = NewIdentityFromExcerpt(author)
103
104 jsonBug.Actors = make([]Identity, len(excerpt.Actors))
105 for i, element := range excerpt.Actors {
106 actor, err := backend.Identities().ResolveExcerpt(element)
107 if err != nil {
108 return BugExcerpt{}, err
109 }
110 jsonBug.Actors[i] = NewIdentityFromExcerpt(actor)
111 }
112
113 jsonBug.Participants = make([]Identity, len(excerpt.Participants))
114 for i, element := range excerpt.Participants {
115 participant, err := backend.Identities().ResolveExcerpt(element)
116 if err != nil {
117 return BugExcerpt{}, err
118 }
119 jsonBug.Participants[i] = NewIdentityFromExcerpt(participant)
120 }
121
122 return jsonBug, nil
123}