plane_label.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package plane_v2
  2. import (
  3. "fmt"
  4. "go.uber.org/zap"
  5. )
  6. // ==================== Label API ====================
  7. // ListLabels 列出标签
  8. func ListLabels(slug, projectID string) ([]byte, error) {
  9. url := fmt.Sprintf("/api/workspaces/%s/projects/%s/issue-labels/", slug, projectID)
  10. resp, err := planeWebGet(url)
  11. if err != nil {
  12. zap.L().Error("[PlaneWeb] ListLabels 请求失败", zap.Error(err))
  13. return nil, err
  14. }
  15. if !isWebSuccess(resp.StatusCode) {
  16. zap.L().Error("[PlaneWeb] ListLabels 返回错误", zap.String("status", resp.Status))
  17. return nil, fmt.Errorf("ListLabels 失败: %s", resp.Status)
  18. }
  19. return resp.Body, nil
  20. }
  21. // CreateLabel 创建标签
  22. func CreateLabel(slug, projectID, name, color string) ([]byte, error) {
  23. url := fmt.Sprintf("/api/workspaces/%s/projects/%s/issue-labels/", slug, projectID)
  24. payload := map[string]interface{}{
  25. "name": name,
  26. "color": color,
  27. }
  28. resp, err := planeWebPost(url, payload)
  29. if err != nil {
  30. zap.L().Error("[PlaneWeb] CreateLabel 请求失败", zap.Error(err))
  31. return nil, err
  32. }
  33. if resp.StatusCode != 201 {
  34. zap.L().Error("[PlaneWeb] CreateLabel 返回错误",
  35. zap.String("status", resp.Status),
  36. zap.String("body", string(resp.Body)),
  37. )
  38. return nil, fmt.Errorf("CreateLabel 失败: %s", resp.Status)
  39. }
  40. return resp.Body, nil
  41. }
  42. // GetLabel 获取标签详情
  43. func GetLabel(slug, projectID, labelID string) ([]byte, error) {
  44. url := fmt.Sprintf("/api/workspaces/%s/projects/%s/issue-labels/%s/", slug, projectID, labelID)
  45. resp, err := planeWebGet(url)
  46. if err != nil {
  47. zap.L().Error("[PlaneWeb] GetLabel 请求失败", zap.Error(err))
  48. return nil, err
  49. }
  50. if !isWebSuccess(resp.StatusCode) {
  51. zap.L().Error("[PlaneWeb] GetLabel 返回错误", zap.String("status", resp.Status))
  52. return nil, fmt.Errorf("GetLabel 失败: %s", resp.Status)
  53. }
  54. return resp.Body, nil
  55. }
  56. // UpdateLabel 更新标签
  57. func UpdateLabel(slug, projectID, labelID string, data map[string]interface{}) ([]byte, error) {
  58. url := fmt.Sprintf("/api/workspaces/%s/projects/%s/issue-labels/%s/", slug, projectID, labelID)
  59. resp, err := planeWebPatch(url, data)
  60. if err != nil {
  61. zap.L().Error("[PlaneWeb] UpdateLabel 请求失败", zap.Error(err))
  62. return nil, err
  63. }
  64. if !isWebSuccess(resp.StatusCode) {
  65. zap.L().Error("[PlaneWeb] UpdateLabel 返回错误",
  66. zap.String("status", resp.Status),
  67. zap.String("body", string(resp.Body)),
  68. )
  69. return nil, fmt.Errorf("UpdateLabel 失败: %s", resp.Status)
  70. }
  71. return resp.Body, nil
  72. }
  73. // DeleteLabel 删除标签
  74. func DeleteLabel(slug, projectID, labelID string) error {
  75. url := fmt.Sprintf("/api/workspaces/%s/projects/%s/issue-labels/%s/", slug, projectID, labelID)
  76. resp, err := planeWebDelete(url)
  77. if err != nil {
  78. zap.L().Error("[PlaneWeb] DeleteLabel 请求失败", zap.Error(err))
  79. return err
  80. }
  81. if resp.StatusCode != 204 {
  82. zap.L().Error("[PlaneWeb] DeleteLabel 返回错误", zap.String("status", resp.Status))
  83. return fmt.Errorf("DeleteLabel 失败: %s", resp.Status)
  84. }
  85. return nil
  86. }
  87. // ListCurrentLabels 列出当前项目下的标签
  88. func ListCurrentLabels() ([]byte, error) {
  89. return ListLabels(webWorkspaceSlug, webProjectId)
  90. }