1package cmdjson
  2
  3import (
  4	"github.com/git-bug/git-bug/cache"
  5	"github.com/git-bug/git-bug/entities/board"
  6)
  7
  8type BoardSnapshot 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
 14	Title        string        `json:"title"`
 15	Description  string        `json:"description"`
 16	Participants []Identity    `json:"participants"`
 17	Columns      []BoardColumn `json:"columns"`
 18}
 19
 20func NewBoardSnapshot(snapshot *board.Snapshot) BoardSnapshot {
 21	jsonBoard := BoardSnapshot{
 22		Id:          snapshot.Id().String(),
 23		HumanId:     snapshot.Id().Human(),
 24		CreateTime:  NewTime(snapshot.CreateTime, 0),
 25		EditTime:    NewTime(snapshot.EditTime(), 0),
 26		Title:       snapshot.Title,
 27		Description: snapshot.Description,
 28	}
 29
 30	jsonBoard.Participants = make([]Identity, len(snapshot.Participants))
 31	for i, element := range snapshot.Participants {
 32		jsonBoard.Participants[i] = NewIdentity(element)
 33	}
 34
 35	jsonBoard.Columns = make([]BoardColumn, len(snapshot.Columns))
 36	for i, column := range snapshot.Columns {
 37		jsonBoard.Columns[i] = NewBoardColumn(column)
 38	}
 39
 40	return jsonBoard
 41}
 42
 43type BoardColumn struct {
 44	Id      string `json:"id"`
 45	HumanId string `json:"human_id"`
 46	Name    string `json:"name"`
 47	Items   []any  `json:"items"`
 48}
 49
 50func NewBoardColumn(column *board.Column) BoardColumn {
 51	jsonColumn := BoardColumn{
 52		Id:      column.CombinedId.String(),
 53		HumanId: column.CombinedId.Human(),
 54		Name:    column.Name,
 55	}
 56	jsonColumn.Items = make([]any, len(column.Items))
 57	for j, item := range column.Items {
 58		switch item := item.(type) {
 59		case *board.Draft:
 60			jsonColumn.Items[j] = NewBoardDraftItem(item)
 61		case *board.BugItem:
 62			jsonColumn.Items[j] = NewBoardBugItem(item)
 63		default:
 64			panic("unknown item type")
 65		}
 66	}
 67	return jsonColumn
 68}
 69
 70type BoardDraftItem struct {
 71	Type    string   `json:"type"`
 72	Id      string   `json:"id"`
 73	HumanId string   `json:"human_id"`
 74	Author  Identity `json:"author"`
 75	Title   string   `json:"title"`
 76	Message string   `json:"message"`
 77}
 78
 79func NewBoardDraftItem(item *board.Draft) BoardDraftItem {
 80	return BoardDraftItem{
 81		Type:    "draft",
 82		Id:      item.CombinedId().String(),
 83		HumanId: item.CombinedId().Human(),
 84		Author:  NewIdentity(item.Author()),
 85		Title:   item.Title(),
 86		Message: item.Message,
 87	}
 88}
 89
 90type BoardBugItem struct {
 91	Type    string   `json:"type"`
 92	Id      string   `json:"id"`
 93	HumanId string   `json:"human_id"`
 94	Author  Identity `json:"author"`
 95	BugId   string   `json:"bug_id"`
 96}
 97
 98func NewBoardBugItem(item *board.BugItem) BoardBugItem {
 99	return BoardBugItem{
100		Type:    "bug",
101		Id:      item.CombinedId().String(),
102		HumanId: item.CombinedId().Human(),
103		Author:  NewIdentity(item.Author()),
104		BugId:   item.Bug.Snapshot().Id().String(),
105		// TODO: add more?
106	}
107}
108
109type BoardExcerpt struct {
110	Id         string `json:"id"`
111	HumanId    string `json:"human_id"`
112	CreateTime Time   `json:"create_time"`
113	EditTime   Time   `json:"edit_time"`
114
115	Title        string     `json:"title"`
116	Description  string     `json:"description"`
117	Participants []Identity `json:"participants"`
118
119	Items    int               `json:"items"`
120	Metadata map[string]string `json:"metadata"`
121}
122
123func NewBoardExcerpt(backend *cache.RepoCache, b *cache.BoardExcerpt) (BoardExcerpt, error) {
124	jsonBoard := BoardExcerpt{
125		Id:          b.Id().String(),
126		HumanId:     b.Id().Human(),
127		CreateTime:  NewTime(b.CreateTime(), b.CreateLamportTime),
128		EditTime:    NewTime(b.EditTime(), b.EditLamportTime),
129		Title:       b.Title,
130		Description: b.Description,
131		Items:       b.ItemCount,
132		Metadata:    b.CreateMetadata,
133	}
134
135	jsonBoard.Participants = make([]Identity, len(b.Participants))
136	for i, element := range b.Participants {
137		participant, err := backend.Identities().ResolveExcerpt(element)
138		if err != nil {
139			return BoardExcerpt{}, err
140		}
141		jsonBoard.Participants[i] = NewIdentityFromExcerpt(participant)
142	}
143	return jsonBoard, nil
144}