bug.go

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