You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

issue_label.go 3.2KB

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