issue_label.go 3.2 KB

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