plane_workspace.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. package plane_v2
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "go.uber.org/zap"
  6. )
  7. // ==================== Workspace API ====================
  8. // ListWorkspaces 列出当前用户的工作空间
  9. func ListWorkspaces() ([]byte, error) {
  10. resp, err := planeWebGet("/api/workspaces/")
  11. if err != nil {
  12. zap.L().Error("[PlaneWeb] ListWorkspaces 请求失败", zap.Error(err))
  13. return nil, err
  14. }
  15. if !isWebSuccess(resp.StatusCode) {
  16. zap.L().Error("[PlaneWeb] ListWorkspaces 返回错误", zap.String("status", resp.Status))
  17. return nil, fmt.Errorf("ListWorkspaces 失败: %s", resp.Status)
  18. }
  19. return resp.Body, nil
  20. }
  21. // CreateWorkspace 创建工作空间
  22. func CreateWorkspace(name, slug string) ([]byte, error) {
  23. payload := map[string]interface{}{
  24. "name": name,
  25. "slug": slug,
  26. }
  27. resp, err := planeWebPost("/api/workspaces/", payload)
  28. if err != nil {
  29. zap.L().Error("[PlaneWeb] CreateWorkspace 请求失败", zap.Error(err))
  30. return nil, err
  31. }
  32. if resp.StatusCode != 201 {
  33. zap.L().Error("[PlaneWeb] CreateWorkspace 返回错误",
  34. zap.String("status", resp.Status),
  35. zap.String("body", string(resp.Body)),
  36. )
  37. return nil, fmt.Errorf("CreateWorkspace 失败: %s", resp.Status)
  38. }
  39. return resp.Body, nil
  40. }
  41. // GetWorkspace 获取工作空间详情
  42. func GetWorkspace(slug string) ([]byte, error) {
  43. url := fmt.Sprintf("/api/workspaces/%s/", slug)
  44. resp, err := planeWebGet(url)
  45. if err != nil {
  46. zap.L().Error("[PlaneWeb] GetWorkspace 请求失败", zap.Error(err))
  47. return nil, err
  48. }
  49. if !isWebSuccess(resp.StatusCode) {
  50. zap.L().Error("[PlaneWeb] GetWorkspace 返回错误", zap.String("status", resp.Status))
  51. return nil, fmt.Errorf("GetWorkspace 失败: %s", resp.Status)
  52. }
  53. return resp.Body, nil
  54. }
  55. // UpdateWorkspace 更新工作空间
  56. func UpdateWorkspace(slug string, data map[string]interface{}) ([]byte, error) {
  57. url := fmt.Sprintf("/api/workspaces/%s/", slug)
  58. resp, err := planeWebPatch(url, data)
  59. if err != nil {
  60. zap.L().Error("[PlaneWeb] UpdateWorkspace 请求失败", zap.Error(err))
  61. return nil, err
  62. }
  63. if !isWebSuccess(resp.StatusCode) {
  64. zap.L().Error("[PlaneWeb] UpdateWorkspace 返回错误",
  65. zap.String("status", resp.Status),
  66. zap.String("body", string(resp.Body)),
  67. )
  68. return nil, fmt.Errorf("UpdateWorkspace 失败: %s", resp.Status)
  69. }
  70. return resp.Body, nil
  71. }
  72. // DeleteWorkspace 删除工作空间
  73. func DeleteWorkspace(slug string) error {
  74. url := fmt.Sprintf("/api/workspaces/%s/", slug)
  75. resp, err := planeWebDelete(url)
  76. if err != nil {
  77. zap.L().Error("[PlaneWeb] DeleteWorkspace 请求失败", zap.Error(err))
  78. return err
  79. }
  80. if resp.StatusCode != 204 {
  81. zap.L().Error("[PlaneWeb] DeleteWorkspace 返回错误", zap.String("status", resp.Status))
  82. return fmt.Errorf("DeleteWorkspace 失败: %s", resp.Status)
  83. }
  84. return nil
  85. }
  86. // GetCurrentWorkspace 获取当前配置的工作空间详情(使用配置中的slug)
  87. func GetCurrentWorkspace() ([]byte, error) {
  88. return GetWorkspace(webWorkspaceSlug)
  89. }
  90. // WorkspaceInfo 工作空间基础信息
  91. type WorkspaceInfo struct {
  92. ID string `json:"id"`
  93. Name string `json:"name"`
  94. Slug string `json:"slug"`
  95. }
  96. // ParseWorkspaceList 解析工作空间列表
  97. func ParseWorkspaceList(data []byte) ([]WorkspaceInfo, error) {
  98. var workspaces []WorkspaceInfo
  99. if err := json.Unmarshal(data, &workspaces); err != nil {
  100. return nil, fmt.Errorf("解析工作空间列表失败: %w", err)
  101. }
  102. return workspaces, nil
  103. }