127 lines
2.9 KiB
Go
127 lines
2.9 KiB
Go
package client
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"log/slog"
|
|
|
|
"git.franklin.lab/steve.cliff/pcli/model"
|
|
)
|
|
|
|
// BoardCreateFields represents the fields required to create a board
|
|
type BoardCreateFields struct {
|
|
Name string `json:"name"`
|
|
Position float64 `json:"position"`
|
|
}
|
|
|
|
func (c *Client) ListBoards(ctx context.Context) ([]model.Board, error) {
|
|
data, err := c.DoNoBody(ctx, "GET", "/api/projects")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var response struct {
|
|
Included struct {
|
|
Boards []model.Board `json:"boards"`
|
|
} `json:"included"`
|
|
}
|
|
|
|
if err := json.Unmarshal(data, &response); err != nil {
|
|
return nil, fmt.Errorf("failed to unmarshal projects response: %w", err)
|
|
}
|
|
|
|
return response.Included.Boards, nil
|
|
}
|
|
|
|
func (c *Client) GetBoard(ctx context.Context, id string) (*model.Board, error) {
|
|
data, err := c.DoNoBody(ctx, "GET", fmt.Sprintf("/api/boards/%s", id))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var response struct {
|
|
Item model.Board `json:"item"`
|
|
Included struct {
|
|
Lists []model.List `json:"lists"`
|
|
Cards []model.Card `json:"cards"`
|
|
} `json:"included"`
|
|
}
|
|
|
|
if err := json.Unmarshal(data, &response); err != nil {
|
|
return nil, fmt.Errorf("failed to unmarshal board response: %w", err)
|
|
}
|
|
|
|
response.Item.Lists = response.Included.Lists
|
|
response.Item.Cards = response.Included.Cards
|
|
|
|
return &response.Item, nil
|
|
}
|
|
|
|
func (c *Client) ListBoardActions(ctx context.Context, boardId string, limit int) ([]model.Action, error) {
|
|
var all []model.Action
|
|
var beforeId string
|
|
|
|
for {
|
|
path := fmt.Sprintf("/api/boards/%s/actions", boardId)
|
|
if beforeId != "" {
|
|
path = fmt.Sprintf("%s?beforeId=%s", path, beforeId)
|
|
}
|
|
|
|
c.Logger.Debug("Fetching board actions page",
|
|
slog.String("boardId", boardId),
|
|
slog.String("beforeId", beforeId),
|
|
)
|
|
|
|
data, err := c.DoNoBody(ctx, "GET", path)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var response struct {
|
|
Items []model.Action `json:"items"`
|
|
}
|
|
|
|
if err := json.Unmarshal(data, &response); err != nil {
|
|
return nil, fmt.Errorf("failed to unmarshal board actions response: %w", err)
|
|
}
|
|
|
|
if len(response.Items) == 0 {
|
|
break
|
|
}
|
|
|
|
all = append(all, response.Items...)
|
|
|
|
if limit > 0 && len(all) >= limit {
|
|
all = all[:limit]
|
|
break
|
|
}
|
|
|
|
beforeId = response.Items[len(response.Items)-1].ID
|
|
}
|
|
|
|
return all, nil
|
|
}
|
|
|
|
func (c *Client) CreateBoard(ctx context.Context, projectId string, fields BoardCreateFields) (*model.Board, error) {
|
|
data, err := c.Do(ctx, "POST", fmt.Sprintf("/api/projects/%s/boards", projectId), fields)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var response struct {
|
|
Item model.Board `json:"item"`
|
|
}
|
|
|
|
if err := json.Unmarshal(data, &response); err != nil {
|
|
return nil, fmt.Errorf("failed to unmarshal board response: %w", err)
|
|
}
|
|
|
|
return &response.Item, nil
|
|
}
|
|
|
|
func (c *Client) DeleteBoard(ctx context.Context, id string) error {
|
|
_, err := c.DoNoBody(ctx, "DELETE", fmt.Sprintf("/api/boards/%s", id))
|
|
return err
|
|
}
|