plane_state.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package plane_v1
  2. import (
  3. "fmt"
  4. "go.uber.org/zap"
  5. )
  6. // ==================== State API ====================
  7. // ListStates 列出状态
  8. func ListStates() ([]byte, error) {
  9. path := fmt.Sprintf("/api/v1/workspaces/%s/projects/%s/states/", planeWorkspaceSlug, planeProjectId)
  10. resp, err := planeHttpGet(path)
  11. if err != nil {
  12. zap.L().Error("[Plane] ListStates 请求失败", zap.Error(err))
  13. return nil, err
  14. }
  15. if !isSuccess(resp.StatusCode) {
  16. zap.L().Error("[Plane] ListStates 返回错误", zap.String("status", resp.Status))
  17. return nil, fmt.Errorf("ListStates 失败: %s", resp.Status)
  18. }
  19. return resp.Body, nil
  20. }
  21. // CreateState 创建状态
  22. // group: backlog / unstarted / started / completed / cancelled
  23. func CreateState(name, color, group string) ([]byte, error) {
  24. path := fmt.Sprintf("/api/v1/workspaces/%s/projects/%s/states/", planeWorkspaceSlug, planeProjectId)
  25. payload := map[string]interface{}{
  26. "name": name,
  27. "color": color,
  28. "group": group,
  29. }
  30. resp, err := planeHttpPost(path, payload)
  31. if err != nil {
  32. zap.L().Error("[Plane] CreateState 请求失败", zap.Error(err))
  33. return nil, err
  34. }
  35. if resp.StatusCode != 201 {
  36. zap.L().Error("[Plane] CreateState 返回错误", zap.String("status", resp.Status), zap.String("body", string(resp.Body)))
  37. return nil, fmt.Errorf("CreateState 失败: %s", resp.Status)
  38. }
  39. return resp.Body, nil
  40. }
  41. // GetState 获取状态详情
  42. func GetState(stateID string) ([]byte, error) {
  43. path := fmt.Sprintf("/api/v1/workspaces/%s/projects/%s/states/%s/", planeWorkspaceSlug, planeProjectId, stateID)
  44. resp, err := planeHttpGet(path)
  45. if err != nil {
  46. zap.L().Error("[Plane] GetState 请求失败", zap.Error(err))
  47. return nil, err
  48. }
  49. if !isSuccess(resp.StatusCode) {
  50. zap.L().Error("[Plane] GetState 返回错误", zap.String("status", resp.Status))
  51. return nil, fmt.Errorf("GetState 失败: %s", resp.Status)
  52. }
  53. return resp.Body, nil
  54. }
  55. // UpdateState 更新状态
  56. func UpdateState(stateID string, data map[string]interface{}) ([]byte, error) {
  57. path := fmt.Sprintf("/api/v1/workspaces/%s/projects/%s/states/%s/", planeWorkspaceSlug, planeProjectId, stateID)
  58. resp, err := planeHttpPatch(path, data)
  59. if err != nil {
  60. zap.L().Error("[Plane] UpdateState 请求失败", zap.Error(err))
  61. return nil, err
  62. }
  63. if !isSuccess(resp.StatusCode) {
  64. zap.L().Error("[Plane] UpdateState 返回错误", zap.String("status", resp.Status), zap.String("body", string(resp.Body)))
  65. return nil, fmt.Errorf("UpdateState 失败: %s", resp.Status)
  66. }
  67. return resp.Body, nil
  68. }
  69. // DeleteState 删除状态
  70. func DeleteState(stateID string) error {
  71. path := fmt.Sprintf("/api/v1/workspaces/%s/projects/%s/states/%s/", planeWorkspaceSlug, planeProjectId, stateID)
  72. resp, err := planeHttpDelete(path)
  73. if err != nil {
  74. zap.L().Error("[Plane] DeleteState 请求失败", zap.Error(err))
  75. return err
  76. }
  77. if resp.StatusCode != 204 {
  78. zap.L().Error("[Plane] DeleteState 返回错误", zap.String("status", resp.Status))
  79. return fmt.Errorf("DeleteState 失败: %s", resp.Status)
  80. }
  81. return nil
  82. }