label.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. package plane_v2_api
  2. import (
  3. "fmt"
  4. plane "fwh-toolbox-server/repositories/api/plane_v2"
  5. )
  6. // ListLabels 列出项目标签
  7. func ListLabels(c *plane.PlaneClient, slug, projectID string) ([]byte, error) {
  8. url := fmt.Sprintf("/api/workspaces/%s/projects/%s/labels/", slug, projectID)
  9. resp, err := c.Get(url)
  10. if err != nil {
  11. return nil, err
  12. }
  13. if !plane.IsSuccess(resp.StatusCode) {
  14. return nil, fmt.Errorf("ListLabels 失败: %s", resp.Status)
  15. }
  16. return resp.Body, nil
  17. }
  18. // CreateLabel 创建标签
  19. func CreateLabel(c *plane.PlaneClient, slug, projectID, name, color string) ([]byte, error) {
  20. url := fmt.Sprintf("/api/workspaces/%s/projects/%s/labels/", slug, projectID)
  21. payload := map[string]interface{}{
  22. "name": name,
  23. "color": color,
  24. }
  25. resp, err := c.Post(url, payload)
  26. if err != nil {
  27. return nil, err
  28. }
  29. if !plane.IsSuccess(resp.StatusCode) {
  30. return nil, fmt.Errorf("CreateLabel 失败: %s, body=%s", resp.Status, string(resp.Body))
  31. }
  32. return resp.Body, nil
  33. }
  34. // GetLabel 获取标签详情
  35. func GetLabel(c *plane.PlaneClient, slug, projectID, labelID string) ([]byte, error) {
  36. url := fmt.Sprintf("/api/workspaces/%s/projects/%s/labels/%s/", slug, projectID, labelID)
  37. resp, err := c.Get(url)
  38. if err != nil {
  39. return nil, err
  40. }
  41. if !plane.IsSuccess(resp.StatusCode) {
  42. return nil, fmt.Errorf("GetLabel 失败: %s", resp.Status)
  43. }
  44. return resp.Body, nil
  45. }
  46. // UpdateLabel 更新标签
  47. func UpdateLabel(c *plane.PlaneClient, slug, projectID, labelID string, data map[string]interface{}) ([]byte, error) {
  48. url := fmt.Sprintf("/api/workspaces/%s/projects/%s/labels/%s/", slug, projectID, labelID)
  49. resp, err := c.Patch(url, data)
  50. if err != nil {
  51. return nil, err
  52. }
  53. if !plane.IsSuccess(resp.StatusCode) {
  54. return nil, fmt.Errorf("UpdateLabel 失败: %s, body=%s", resp.Status, string(resp.Body))
  55. }
  56. return resp.Body, nil
  57. }
  58. // DeleteLabel 删除标签
  59. func DeleteLabel(c *plane.PlaneClient, slug, projectID, labelID string) error {
  60. url := fmt.Sprintf("/api/workspaces/%s/projects/%s/labels/%s/", slug, projectID, labelID)
  61. resp, err := c.Delete(url)
  62. if err != nil {
  63. return err
  64. }
  65. if resp.StatusCode != 204 {
  66. return fmt.Errorf("DeleteLabel 失败: %s", resp.Status)
  67. }
  68. return nil
  69. }