project.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package plane_v2_api
  2. import (
  3. "fmt"
  4. plane "fwh-toolbox-server/repositories/api/plane_v2"
  5. )
  6. // ListProjects 列出工作空间下的所有项目
  7. func ListProjects(c *plane.PlaneClient, slug string) ([]byte, error) {
  8. url := fmt.Sprintf("/api/workspaces/%s/projects/", slug)
  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("ListProjects 失败: %s", resp.Status)
  15. }
  16. return resp.Body, nil
  17. }
  18. // CreateProject 创建项目
  19. func CreateProject(c *plane.PlaneClient, slug string, data map[string]interface{}) ([]byte, error) {
  20. url := fmt.Sprintf("/api/workspaces/%s/projects/", slug)
  21. resp, err := c.Post(url, data)
  22. if err != nil {
  23. return nil, err
  24. }
  25. if !plane.IsSuccess(resp.StatusCode) {
  26. return nil, fmt.Errorf("CreateProject 失败: %s, body=%s", resp.Status, string(resp.Body))
  27. }
  28. return resp.Body, nil
  29. }
  30. // GetProject 获取项目详情
  31. func GetProject(c *plane.PlaneClient, slug, projectID string) ([]byte, error) {
  32. url := fmt.Sprintf("/api/workspaces/%s/projects/%s/", slug, projectID)
  33. resp, err := c.Get(url)
  34. if err != nil {
  35. return nil, err
  36. }
  37. if !plane.IsSuccess(resp.StatusCode) {
  38. return nil, fmt.Errorf("GetProject 失败: %s", resp.Status)
  39. }
  40. return resp.Body, nil
  41. }
  42. // UpdateProject 更新项目
  43. func UpdateProject(c *plane.PlaneClient, slug, projectID string, data map[string]interface{}) ([]byte, error) {
  44. url := fmt.Sprintf("/api/workspaces/%s/projects/%s/", slug, projectID)
  45. resp, err := c.Patch(url, data)
  46. if err != nil {
  47. return nil, err
  48. }
  49. if !plane.IsSuccess(resp.StatusCode) {
  50. return nil, fmt.Errorf("UpdateProject 失败: %s, body=%s", resp.Status, string(resp.Body))
  51. }
  52. return resp.Body, nil
  53. }
  54. // DeleteProject 删除项目
  55. func DeleteProject(c *plane.PlaneClient, slug, projectID string) error {
  56. url := fmt.Sprintf("/api/workspaces/%s/projects/%s/", slug, projectID)
  57. resp, err := c.Delete(url)
  58. if err != nil {
  59. return err
  60. }
  61. if resp.StatusCode != 204 {
  62. return fmt.Errorf("DeleteProject 失败: %s", resp.Status)
  63. }
  64. return nil
  65. }
  66. // ArchiveProject 归档项目
  67. func ArchiveProject(c *plane.PlaneClient, slug, projectID string) error {
  68. url := fmt.Sprintf("/api/workspaces/%s/projects/%s/archive/", slug, projectID)
  69. resp, err := c.Post(url, nil)
  70. if err != nil {
  71. return err
  72. }
  73. if !plane.IsSuccess(resp.StatusCode) {
  74. return fmt.Errorf("ArchiveProject 失败: %s", resp.Status)
  75. }
  76. return nil
  77. }
  78. // UnarchiveProject 取消归档项目
  79. func UnarchiveProject(c *plane.PlaneClient, slug, projectID string) error {
  80. url := fmt.Sprintf("/api/workspaces/%s/projects/%s/archive/", slug, projectID)
  81. resp, err := c.Delete(url)
  82. if err != nil {
  83. return err
  84. }
  85. if !plane.IsSuccess(resp.StatusCode) {
  86. return fmt.Errorf("UnarchiveProject 失败: %s", resp.Status)
  87. }
  88. return nil
  89. }