board.go

  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	Actors      []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.Actors = make([]Identity, len(snapshot.Actors))
 31	for i, element := range snapshot.Actors {
 32		jsonBoard.Actors[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}
 77
 78func NewBoardDraftItem(item *board.Draft) BoardDraftItem {
 79	return BoardDraftItem{
 80		Type:    "draft",
 81		Id:      item.CombinedId().String(),
 82		HumanId: item.CombinedId().Human(),
 83		Author:  NewIdentity(item.Author()),
 84		Title:   item.Title(),
 85	}
 86}
 87
 88type BoardBugItem struct {
 89	Type    string   `json:"type"`
 90	Id      string   `json:"id"`
 91	HumanId string   `json:"human_id"`
 92	Author  Identity `json:"author"`
 93	BugId   string   `json:"bug_id"`
 94}
 95
 96func NewBoardBugItem(item *board.BugItem) BoardBugItem {
 97	return BoardBugItem{
 98		Type:    "bug",
 99		Id:      item.CombinedId().String(),
100		HumanId: item.CombinedId().Human(),
101		Author:  NewIdentity(item.Author()),
102		BugId:   item.Bug.Snapshot().Id().String(),
103	}
104}
105
106type BoardExcerpt struct {
107	Id         string `json:"id"`
108	HumanId    string `json:"human_id"`
109	CreateTime Time   `json:"create_time"`
110	EditTime   Time   `json:"edit_time"`
111
112	Title       string     `json:"title"`
113	Description string     `json:"description"`
114	Actors      []Identity `json:"participants"`
115
116	Items    int               `json:"items"`
117	Metadata map[string]string `json:"metadata"`
118}
119
120func NewBoardExcerpt(backend *cache.RepoCache, b *cache.BoardExcerpt) (BoardExcerpt, error) {
121	jsonBoard := BoardExcerpt{
122		Id:          b.Id().String(),
123		HumanId:     b.Id().Human(),
124		CreateTime:  NewTime(b.CreateTime(), b.CreateLamportTime),
125		EditTime:    NewTime(b.EditTime(), b.EditLamportTime),
126		Title:       b.Title,
127		Description: b.Description,
128		Items:       b.ItemCount,
129		Metadata:    b.CreateMetadata,
130	}
131
132	jsonBoard.Actors = make([]Identity, len(b.Actors))
133	for i, element := range b.Actors {
134		participant, err := backend.Identities().ResolveExcerpt(element)
135		if err != nil {
136			return BoardExcerpt{}, err
137		}
138		jsonBoard.Actors[i] = NewIdentityFromExcerpt(participant)
139	}
140	return jsonBoard, nil
141}