Released v1
This commit is contained in:
@@ -0,0 +1,98 @@
|
||||
package client
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
|
||||
"git.franklin.lab/steve.cliff/pcli/model"
|
||||
)
|
||||
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user