labels.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. //
  2. // Copyright 2017, Sander van Harmelen
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. //
  16. package gitlab
  17. import (
  18. "encoding/json"
  19. "fmt"
  20. "net/url"
  21. )
  22. // LabelsService handles communication with the label related methods of the
  23. // GitLab API.
  24. //
  25. // GitLab API docs: https://docs.gitlab.com/ce/api/labels.html
  26. type LabelsService struct {
  27. client *Client
  28. }
  29. // Label represents a GitLab label.
  30. //
  31. // GitLab API docs: https://docs.gitlab.com/ce/api/labels.html
  32. type Label struct {
  33. ID int `json:"id"`
  34. Name string `json:"name"`
  35. Color string `json:"color"`
  36. Description string `json:"description"`
  37. OpenIssuesCount int `json:"open_issues_count"`
  38. ClosedIssuesCount int `json:"closed_issues_count"`
  39. OpenMergeRequestsCount int `json:"open_merge_requests_count"`
  40. Subscribed bool `json:"subscribed"`
  41. Priority int `json:"priority"`
  42. }
  43. // UnmarshalJSON implements the json.Unmarshaler interface.
  44. func (l *Label) UnmarshalJSON(data []byte) error {
  45. type alias Label
  46. if err := json.Unmarshal(data, (*alias)(l)); err != nil {
  47. return err
  48. }
  49. if l.Name == "" {
  50. var raw map[string]interface{}
  51. if err := json.Unmarshal(data, &raw); err != nil {
  52. return err
  53. }
  54. if title, ok := raw["title"].(string); ok {
  55. l.Name = title
  56. }
  57. }
  58. return nil
  59. }
  60. func (l Label) String() string {
  61. return Stringify(l)
  62. }
  63. // ListLabelsOptions represents the available ListLabels() options.
  64. //
  65. // GitLab API docs: https://docs.gitlab.com/ce/api/labels.html#list-labels
  66. type ListLabelsOptions ListOptions
  67. // ListLabels gets all labels for given project.
  68. //
  69. // GitLab API docs: https://docs.gitlab.com/ce/api/labels.html#list-labels
  70. func (s *LabelsService) ListLabels(pid interface{}, opt *ListLabelsOptions, options ...OptionFunc) ([]*Label, *Response, error) {
  71. project, err := parseID(pid)
  72. if err != nil {
  73. return nil, nil, err
  74. }
  75. u := fmt.Sprintf("projects/%s/labels", url.QueryEscape(project))
  76. req, err := s.client.NewRequest("GET", u, opt, options)
  77. if err != nil {
  78. return nil, nil, err
  79. }
  80. var l []*Label
  81. resp, err := s.client.Do(req, &l)
  82. if err != nil {
  83. return nil, resp, err
  84. }
  85. return l, resp, err
  86. }
  87. // CreateLabelOptions represents the available CreateLabel() options.
  88. //
  89. // GitLab API docs: https://docs.gitlab.com/ce/api/labels.html#create-a-new-label
  90. type CreateLabelOptions struct {
  91. Name *string `url:"name,omitempty" json:"name,omitempty"`
  92. Color *string `url:"color,omitempty" json:"color,omitempty"`
  93. Description *string `url:"description,omitempty" json:"description,omitempty"`
  94. }
  95. // CreateLabel creates a new label for given repository with given name and
  96. // color.
  97. //
  98. // GitLab API docs: https://docs.gitlab.com/ce/api/labels.html#create-a-new-label
  99. func (s *LabelsService) CreateLabel(pid interface{}, opt *CreateLabelOptions, options ...OptionFunc) (*Label, *Response, error) {
  100. project, err := parseID(pid)
  101. if err != nil {
  102. return nil, nil, err
  103. }
  104. u := fmt.Sprintf("projects/%s/labels", url.QueryEscape(project))
  105. req, err := s.client.NewRequest("POST", u, opt, options)
  106. if err != nil {
  107. return nil, nil, err
  108. }
  109. l := new(Label)
  110. resp, err := s.client.Do(req, l)
  111. if err != nil {
  112. return nil, resp, err
  113. }
  114. return l, resp, err
  115. }
  116. // DeleteLabelOptions represents the available DeleteLabel() options.
  117. //
  118. // GitLab API docs: https://docs.gitlab.com/ce/api/labels.html#delete-a-label
  119. type DeleteLabelOptions struct {
  120. Name *string `url:"name,omitempty" json:"name,omitempty"`
  121. }
  122. // DeleteLabel deletes a label given by its name.
  123. //
  124. // GitLab API docs: https://docs.gitlab.com/ce/api/labels.html#delete-a-label
  125. func (s *LabelsService) DeleteLabel(pid interface{}, opt *DeleteLabelOptions, options ...OptionFunc) (*Response, error) {
  126. project, err := parseID(pid)
  127. if err != nil {
  128. return nil, err
  129. }
  130. u := fmt.Sprintf("projects/%s/labels", url.QueryEscape(project))
  131. req, err := s.client.NewRequest("DELETE", u, opt, options)
  132. if err != nil {
  133. return nil, err
  134. }
  135. return s.client.Do(req, nil)
  136. }
  137. // UpdateLabelOptions represents the available UpdateLabel() options.
  138. //
  139. // GitLab API docs: https://docs.gitlab.com/ce/api/labels.html#delete-a-label
  140. type UpdateLabelOptions struct {
  141. Name *string `url:"name,omitempty" json:"name,omitempty"`
  142. NewName *string `url:"new_name,omitempty" json:"new_name,omitempty"`
  143. Color *string `url:"color,omitempty" json:"color,omitempty"`
  144. Description *string `url:"description,omitempty" json:"description,omitempty"`
  145. }
  146. // UpdateLabel updates an existing label with new name or now color. At least
  147. // one parameter is required, to update the label.
  148. //
  149. // GitLab API docs: https://docs.gitlab.com/ce/api/labels.html#edit-an-existing-label
  150. func (s *LabelsService) UpdateLabel(pid interface{}, opt *UpdateLabelOptions, options ...OptionFunc) (*Label, *Response, error) {
  151. project, err := parseID(pid)
  152. if err != nil {
  153. return nil, nil, err
  154. }
  155. u := fmt.Sprintf("projects/%s/labels", url.QueryEscape(project))
  156. req, err := s.client.NewRequest("PUT", u, opt, options)
  157. if err != nil {
  158. return nil, nil, err
  159. }
  160. l := new(Label)
  161. resp, err := s.client.Do(req, l)
  162. if err != nil {
  163. return nil, resp, err
  164. }
  165. return l, resp, err
  166. }
  167. // SubscribeToLabel subscribes the authenticated user to a label to receive
  168. // notifications. If the user is already subscribed to the label, the status
  169. // code 304 is returned.
  170. //
  171. // GitLab API docs:
  172. // https://docs.gitlab.com/ce/api/labels.html#subscribe-to-a-label
  173. func (s *LabelsService) SubscribeToLabel(pid interface{}, labelID interface{}, options ...OptionFunc) (*Label, *Response, error) {
  174. project, err := parseID(pid)
  175. if err != nil {
  176. return nil, nil, err
  177. }
  178. label, err := parseID(labelID)
  179. if err != nil {
  180. return nil, nil, err
  181. }
  182. u := fmt.Sprintf("projects/%s/labels/%s/subscribe", url.QueryEscape(project), label)
  183. req, err := s.client.NewRequest("POST", u, nil, options)
  184. if err != nil {
  185. return nil, nil, err
  186. }
  187. l := new(Label)
  188. resp, err := s.client.Do(req, l)
  189. if err != nil {
  190. return nil, resp, err
  191. }
  192. return l, resp, err
  193. }
  194. // UnsubscribeFromLabel unsubscribes the authenticated user from a label to not
  195. // receive notifications from it. If the user is not subscribed to the label, the
  196. // status code 304 is returned.
  197. //
  198. // GitLab API docs:
  199. // https://docs.gitlab.com/ce/api/labels.html#unsubscribe-from-a-label
  200. func (s *LabelsService) UnsubscribeFromLabel(pid interface{}, labelID interface{}, options ...OptionFunc) (*Response, error) {
  201. project, err := parseID(pid)
  202. if err != nil {
  203. return nil, err
  204. }
  205. label, err := parseID(labelID)
  206. if err != nil {
  207. return nil, err
  208. }
  209. u := fmt.Sprintf("projects/%s/labels/%s/unsubscribe", url.QueryEscape(project), label)
  210. req, err := s.client.NewRequest("POST", u, nil, options)
  211. if err != nil {
  212. return nil, err
  213. }
  214. return s.client.Do(req, nil)
  215. }