plane_project.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. package plane_v2
  2. import (
  3. "fmt"
  4. "go.uber.org/zap"
  5. )
  6. // ==================== Project API ====================
  7. // ListProjects 列出项目
  8. func ListProjects(slug string) ([]byte, error) {
  9. url := fmt.Sprintf("/api/workspaces/%s/projects/", slug)
  10. resp, err := planeWebGet(url)
  11. if err != nil {
  12. zap.L().Error("[PlaneWeb] ListProjects 请求失败", zap.Error(err))
  13. return nil, err
  14. }
  15. if !isWebSuccess(resp.StatusCode) {
  16. zap.L().Error("[PlaneWeb] 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(slug string, data map[string]interface{}) ([]byte, error) {
  23. url := fmt.Sprintf("/api/workspaces/%s/projects/", slug)
  24. resp, err := planeWebPost(url, data)
  25. if err != nil {
  26. zap.L().Error("[PlaneWeb] CreateProject 请求失败", zap.Error(err))
  27. return nil, err
  28. }
  29. if resp.StatusCode != 201 {
  30. zap.L().Error("[PlaneWeb] CreateProject 返回错误",
  31. zap.String("status", resp.Status),
  32. zap.String("body", string(resp.Body)),
  33. )
  34. return nil, fmt.Errorf("CreateProject 失败: %s", resp.Status)
  35. }
  36. return resp.Body, nil
  37. }
  38. // GetProject 获取项目详情
  39. func GetProject(slug, projectID string) ([]byte, error) {
  40. url := fmt.Sprintf("/api/workspaces/%s/projects/%s/", slug, projectID)
  41. resp, err := planeWebGet(url)
  42. if err != nil {
  43. zap.L().Error("[PlaneWeb] GetProject 请求失败", zap.Error(err))
  44. return nil, err
  45. }
  46. if !isWebSuccess(resp.StatusCode) {
  47. zap.L().Error("[PlaneWeb] GetProject 返回错误", zap.String("status", resp.Status))
  48. return nil, fmt.Errorf("GetProject 失败: %s", resp.Status)
  49. }
  50. return resp.Body, nil
  51. }
  52. // UpdateProject 更新项目
  53. func UpdateProject(slug, projectID string, data map[string]interface{}) ([]byte, error) {
  54. url := fmt.Sprintf("/api/workspaces/%s/projects/%s/", slug, projectID)
  55. resp, err := planeWebPatch(url, data)
  56. if err != nil {
  57. zap.L().Error("[PlaneWeb] UpdateProject 请求失败", zap.Error(err))
  58. return nil, err
  59. }
  60. if !isWebSuccess(resp.StatusCode) {
  61. zap.L().Error("[PlaneWeb] UpdateProject 返回错误",
  62. zap.String("status", resp.Status),
  63. zap.String("body", string(resp.Body)),
  64. )
  65. return nil, fmt.Errorf("UpdateProject 失败: %s", resp.Status)
  66. }
  67. return resp.Body, nil
  68. }
  69. // DeleteProject 删除项目
  70. func DeleteProject(slug, projectID string) error {
  71. url := fmt.Sprintf("/api/workspaces/%s/projects/%s/", slug, projectID)
  72. resp, err := planeWebDelete(url)
  73. if err != nil {
  74. zap.L().Error("[PlaneWeb] DeleteProject 请求失败", zap.Error(err))
  75. return err
  76. }
  77. if resp.StatusCode != 204 {
  78. zap.L().Error("[PlaneWeb] DeleteProject 返回错误", zap.String("status", resp.Status))
  79. return fmt.Errorf("DeleteProject 失败: %s", resp.Status)
  80. }
  81. return nil
  82. }
  83. // ArchiveProject 归档项目
  84. func ArchiveProject(slug, projectID string) error {
  85. url := fmt.Sprintf("/api/workspaces/%s/projects/%s/archive/", slug, projectID)
  86. resp, err := planeWebPost(url, map[string]interface{}{})
  87. if err != nil {
  88. zap.L().Error("[PlaneWeb] ArchiveProject 请求失败", zap.Error(err))
  89. return err
  90. }
  91. if resp.StatusCode != 204 {
  92. zap.L().Error("[PlaneWeb] ArchiveProject 返回错误", zap.String("status", resp.Status))
  93. return fmt.Errorf("ArchiveProject 失败: %s", resp.Status)
  94. }
  95. return nil
  96. }
  97. // UnarchiveProject 取消归档项目
  98. func UnarchiveProject(slug, projectID string) error {
  99. url := fmt.Sprintf("/api/workspaces/%s/projects/%s/archive/", slug, projectID)
  100. resp, err := planeWebDelete(url)
  101. if err != nil {
  102. zap.L().Error("[PlaneWeb] UnarchiveProject 请求失败", zap.Error(err))
  103. return err
  104. }
  105. if !isWebSuccess(resp.StatusCode) {
  106. zap.L().Error("[PlaneWeb] UnarchiveProject 返回错误", zap.String("status", resp.Status))
  107. return fmt.Errorf("UnarchiveProject 失败: %s", resp.Status)
  108. }
  109. return nil
  110. }
  111. // ListCurrentProjects 列出当前配置的工作空间下的项目
  112. func ListCurrentProjects() ([]byte, error) {
  113. return ListProjects(webWorkspaceSlug)
  114. }
  115. // GetCurrentProject 获取当前配置的项目详情
  116. func GetCurrentProject() ([]byte, error) {
  117. return GetProject(webWorkspaceSlug, webProjectId)
  118. }