comment.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. package plane_v2_api
  2. import (
  3. "fmt"
  4. plane "fwh-toolbox-server/repositories/api/plane_v2"
  5. )
  6. // ListComments 列出工作项评论
  7. func ListComments(c *plane.PlaneClient, slug, projectID, issueID string) ([]byte, error) {
  8. url := fmt.Sprintf("/api/workspaces/%s/projects/%s/issues/%s/comments/", slug, projectID, issueID)
  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("ListComments 失败: %s", resp.Status)
  15. }
  16. return resp.Body, nil
  17. }
  18. // CreateComment 创建评论
  19. func CreateComment(c *plane.PlaneClient, slug, projectID, issueID, commentHTML string) ([]byte, error) {
  20. url := fmt.Sprintf("/api/workspaces/%s/projects/%s/issues/%s/comments/", slug, projectID, issueID)
  21. payload := map[string]interface{}{
  22. "comment_html": commentHTML,
  23. }
  24. resp, err := c.Post(url, payload)
  25. if err != nil {
  26. return nil, err
  27. }
  28. if !plane.IsSuccess(resp.StatusCode) {
  29. return nil, fmt.Errorf("CreateComment 失败: %s, body=%s", resp.Status, string(resp.Body))
  30. }
  31. return resp.Body, nil
  32. }
  33. // GetComment 获取评论详情
  34. func GetComment(c *plane.PlaneClient, slug, projectID, issueID, commentID string) ([]byte, error) {
  35. url := fmt.Sprintf("/api/workspaces/%s/projects/%s/issues/%s/comments/%s/", slug, projectID, issueID, commentID)
  36. resp, err := c.Get(url)
  37. if err != nil {
  38. return nil, err
  39. }
  40. if !plane.IsSuccess(resp.StatusCode) {
  41. return nil, fmt.Errorf("GetComment 失败: %s", resp.Status)
  42. }
  43. return resp.Body, nil
  44. }
  45. // UpdateComment 更新评论
  46. func UpdateComment(c *plane.PlaneClient, slug, projectID, issueID, commentID, commentHTML string) ([]byte, error) {
  47. url := fmt.Sprintf("/api/workspaces/%s/projects/%s/issues/%s/comments/%s/", slug, projectID, issueID, commentID)
  48. payload := map[string]interface{}{
  49. "comment_html": commentHTML,
  50. }
  51. resp, err := c.Patch(url, payload)
  52. if err != nil {
  53. return nil, err
  54. }
  55. if !plane.IsSuccess(resp.StatusCode) {
  56. return nil, fmt.Errorf("UpdateComment 失败: %s, body=%s", resp.Status, string(resp.Body))
  57. }
  58. return resp.Body, nil
  59. }
  60. // DeleteComment 删除评论
  61. func DeleteComment(c *plane.PlaneClient, slug, projectID, issueID, commentID string) error {
  62. url := fmt.Sprintf("/api/workspaces/%s/projects/%s/issues/%s/comments/%s/", slug, projectID, issueID, commentID)
  63. resp, err := c.Delete(url)
  64. if err != nil {
  65. return err
  66. }
  67. if resp.StatusCode != 204 {
  68. return fmt.Errorf("DeleteComment 失败: %s", resp.Status)
  69. }
  70. return nil
  71. }