issue_label.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package gitote
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. )
  7. type Label struct {
  8. ID int64 `json:"id"`
  9. Name string `json:"name"`
  10. Color string `json:"color"`
  11. URL string `json:"url"`
  12. }
  13. func (c *Client) ListRepoLabels(owner, repo string) ([]*Label, error) {
  14. labels := make([]*Label, 0, 10)
  15. return labels, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/labels", owner, repo), nil, nil, &labels)
  16. }
  17. func (c *Client) GetRepoLabel(owner, repo string, id int64) (*Label, error) {
  18. label := new(Label)
  19. return label, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/labels/%d", owner, repo, id), nil, nil, label)
  20. }
  21. type CreateLabelOption struct {
  22. Name string `json:"name" binding:"Required"`
  23. Color string `json:"color" binding:"Required;Size(7)"`
  24. }
  25. func (c *Client) CreateLabel(owner, repo string, opt CreateLabelOption) (*Label, error) {
  26. body, err := json.Marshal(&opt)
  27. if err != nil {
  28. return nil, err
  29. }
  30. label := new(Label)
  31. return label, c.getParsedResponse("POST", fmt.Sprintf("/repos/%s/%s/labels", owner, repo),
  32. jsonHeader, bytes.NewReader(body), label)
  33. }
  34. type EditLabelOption struct {
  35. Name *string `json:"name"`
  36. Color *string `json:"color"`
  37. }
  38. func (c *Client) EditLabel(owner, repo string, id int64, opt EditLabelOption) (*Label, error) {
  39. body, err := json.Marshal(&opt)
  40. if err != nil {
  41. return nil, err
  42. }
  43. label := new(Label)
  44. return label, c.getParsedResponse("PATCH", fmt.Sprintf("/repos/%s/%s/labels/%d", owner, repo, id), jsonHeader, bytes.NewReader(body), label)
  45. }
  46. func (c *Client) DeleteLabel(owner, repo string, id int64) error {
  47. _, err := c.getResponse("DELETE", fmt.Sprintf("/repos/%s/%s/labels/%d", owner, repo, id), nil, nil)
  48. return err
  49. }
  50. type IssueLabelsOption struct {
  51. Labels []int64 `json:"labels"`
  52. }
  53. func (c *Client) GetIssueLabels(owner, repo string, index int64) ([]*Label, error) {
  54. labels := make([]*Label, 0, 5)
  55. return labels, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/issues/%d/labels", owner, repo, index), nil, nil, &labels)
  56. }
  57. func (c *Client) AddIssueLabels(owner, repo string, index int64, opt IssueLabelsOption) ([]*Label, error) {
  58. body, err := json.Marshal(&opt)
  59. if err != nil {
  60. return nil, err
  61. }
  62. labels := make([]*Label, 0)
  63. return labels, c.getParsedResponse("POST", fmt.Sprintf("/repos/%s/%s/issues/%d/labels", owner, repo, index), jsonHeader, bytes.NewReader(body), &labels)
  64. }
  65. func (c *Client) ReplaceIssueLabels(owner, repo string, index int64, opt IssueLabelsOption) ([]*Label, error) {
  66. body, err := json.Marshal(&opt)
  67. if err != nil {
  68. return nil, err
  69. }
  70. labels := make([]*Label, 0)
  71. return labels, c.getParsedResponse("PUT", fmt.Sprintf("/repos/%s/%s/issues/%d/labels", owner, repo, index), jsonHeader, bytes.NewReader(body), &labels)
  72. }
  73. func (c *Client) DeleteIssueLabel(owner, repo string, index, label int64) error {
  74. _, err := c.getResponse("DELETE", fmt.Sprintf("/repos/%s/%s/issues/%d/labels/%d", owner, repo, index, label), nil, nil)
  75. return err
  76. }
  77. func (c *Client) ClearIssueLabels(owner, repo string, index int64) error {
  78. _, err := c.getResponse("DELETE", fmt.Sprintf("/repos/%s/%s/issues/%d/labels", owner, repo, index), nil, nil)
  79. return err
  80. }