plane_project.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. package plane_v1
  2. import (
  3. "fmt"
  4. "go.uber.org/zap"
  5. )
  6. // ==================== Project API ====================
  7. // ListProjects 列出项目
  8. func ListProjects() ([]byte, error) {
  9. url := fmt.Sprintf("/api/v1/workspaces/%s/projects/", planeWorkspaceSlug)
  10. resp, err := planeHttpGet(url)
  11. if err != nil {
  12. zap.L().Error("[Plane] ListProjects 请求失败", zap.Error(err))
  13. return nil, err
  14. }
  15. if !isSuccess(resp.StatusCode) {
  16. zap.L().Error("[Plane] ListProjects 返回错误", zap.String("status", resp.Status))
  17. return nil, fmt.Errorf("ListProjects 失败: %s", resp.Status)
  18. }
  19. return resp.Body, nil
  20. }
  21. // CreateProject 创建项目
  22. func CreateProject(data map[string]interface{}) ([]byte, error) {
  23. url := fmt.Sprintf("/api/v1/workspaces/%s/projects/", planeWorkspaceSlug)
  24. resp, err := planeHttpPost(url, data)
  25. if err != nil {
  26. zap.L().Error("[Plane] CreateProject 请求失败", zap.Error(err))
  27. return nil, err
  28. }
  29. if resp.StatusCode != 201 {
  30. zap.L().Error("[Plane] CreateProject 返回错误", zap.String("status", resp.Status), zap.String("body", string(resp.Body)))
  31. return nil, fmt.Errorf("CreateProject 失败: %s", resp.Status)
  32. }
  33. return resp.Body, nil
  34. }
  35. // GetProject 获取项目详情
  36. func GetProject(projectID string) ([]byte, error) {
  37. url := fmt.Sprintf("/api/v1/workspaces/%s/projects/%s/", planeWorkspaceSlug, projectID)
  38. resp, err := planeHttpGet(url)
  39. if err != nil {
  40. zap.L().Error("[Plane] GetProject 请求失败", zap.Error(err))
  41. return nil, err
  42. }
  43. if !isSuccess(resp.StatusCode) {
  44. zap.L().Error("[Plane] GetProject 返回错误", zap.String("status", resp.Status))
  45. return nil, fmt.Errorf("GetProject 失败: %s", resp.Status)
  46. }
  47. return resp.Body, nil
  48. }
  49. // UpdateProject 更新项目
  50. func UpdateProject(projectID string, data map[string]interface{}) ([]byte, error) {
  51. url := fmt.Sprintf("/api/v1/workspaces/%s/projects/%s/", planeWorkspaceSlug, projectID)
  52. resp, err := planeHttpPatch(url, data)
  53. if err != nil {
  54. zap.L().Error("[Plane] UpdateProject 请求失败", zap.Error(err))
  55. return nil, err
  56. }
  57. if !isSuccess(resp.StatusCode) {
  58. zap.L().Error("[Plane] UpdateProject 返回错误", zap.String("status", resp.Status), zap.String("body", string(resp.Body)))
  59. return nil, fmt.Errorf("UpdateProject 失败: %s", resp.Status)
  60. }
  61. return resp.Body, nil
  62. }
  63. // DeleteProject 删除项目
  64. func DeleteProject(projectID string) error {
  65. url := fmt.Sprintf("/api/v1/workspaces/%s/projects/%s/", planeWorkspaceSlug, projectID)
  66. resp, err := planeHttpDelete(url)
  67. if err != nil {
  68. zap.L().Error("[Plane] DeleteProject 请求失败", zap.Error(err))
  69. return err
  70. }
  71. if resp.StatusCode != 204 {
  72. zap.L().Error("[Plane] DeleteProject 返回错误", zap.String("status", resp.Status))
  73. return fmt.Errorf("DeleteProject 失败: %s", resp.Status)
  74. }
  75. return nil
  76. }
  77. // ArchiveProject 归档项目
  78. func ArchiveProject(projectID string) error {
  79. url := fmt.Sprintf("/api/v1/workspaces/%s/projects/%s/archive/", planeWorkspaceSlug, projectID)
  80. resp, err := planeHttpPost(url, map[string]interface{}{})
  81. if err != nil {
  82. zap.L().Error("[Plane] ArchiveProject 请求失败", zap.Error(err))
  83. return err
  84. }
  85. if resp.StatusCode != 204 {
  86. zap.L().Error("[Plane] ArchiveProject 返回错误", zap.String("status", resp.Status))
  87. return fmt.Errorf("ArchiveProject 失败: %s", resp.Status)
  88. }
  89. return nil
  90. }
  91. // UnarchiveProject 取消归档项目
  92. func UnarchiveProject(projectID string) error {
  93. url := fmt.Sprintf("/api/v1/workspaces/%s/projects/%s/archive/", planeWorkspaceSlug, projectID)
  94. resp, err := planeHttpDelete(url)
  95. if err != nil {
  96. zap.L().Error("[Plane] UnarchiveProject 请求失败", zap.Error(err))
  97. return err
  98. }
  99. if resp.StatusCode != 204 {
  100. zap.L().Error("[Plane] UnarchiveProject 返回错误", zap.String("status", resp.Status))
  101. return fmt.Errorf("UnarchiveProject 失败: %s", resp.Status)
  102. }
  103. return nil
  104. }
  105. // GetProjectSummary 获取项目摘要
  106. func GetProjectSummary(projectID, fields string) ([]byte, error) {
  107. url := fmt.Sprintf("/api/v1/workspaces/%s/projects/%s/summary/", planeWorkspaceSlug, projectID)
  108. if fields != "" {
  109. url += "?fields=" + fields
  110. }
  111. resp, err := planeHttpGet(url)
  112. if err != nil {
  113. zap.L().Error("[Plane] GetProjectSummary 请求失败", zap.Error(err))
  114. return nil, err
  115. }
  116. if !isSuccess(resp.StatusCode) {
  117. zap.L().Error("[Plane] GetProjectSummary 返回错误", zap.String("status", resp.Status))
  118. return nil, fmt.Errorf("GetProjectSummary 失败: %s", resp.Status)
  119. }
  120. return resp.Body, nil
  121. }