issues_labels.go 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. // Copyright 2013 The go-github AUTHORS. All rights reserved.
  2. //
  3. // Use of this source code is governed by a BSD-style
  4. // license that can be found in the LICENSE file.
  5. package github
  6. import (
  7. "context"
  8. "fmt"
  9. "strings"
  10. )
  11. // Label represents a GitHub label on an Issue
  12. type Label struct {
  13. ID *int64 `json:"id,omitempty"`
  14. URL *string `json:"url,omitempty"`
  15. Name *string `json:"name,omitempty"`
  16. Color *string `json:"color,omitempty"`
  17. Description *string `json:"description,omitempty"`
  18. Default *bool `json:"default,omitempty"`
  19. NodeID *string `json:"node_id,omitempty"`
  20. }
  21. func (l Label) String() string {
  22. return Stringify(l)
  23. }
  24. // ListLabels lists all labels for a repository.
  25. //
  26. // GitHub API docs: https://developer.github.com/v3/issues/labels/#list-all-labels-for-this-repository
  27. func (s *IssuesService) ListLabels(ctx context.Context, owner string, repo string, opt *ListOptions) ([]*Label, *Response, error) {
  28. u := fmt.Sprintf("repos/%v/%v/labels", owner, repo)
  29. u, err := addOptions(u, opt)
  30. if err != nil {
  31. return nil, nil, err
  32. }
  33. req, err := s.client.NewRequest("GET", u, nil)
  34. if err != nil {
  35. return nil, nil, err
  36. }
  37. // TODO: remove custom Accept header when this API fully launches.
  38. acceptHeaders := []string{mediaTypeGraphQLNodeIDPreview, mediaTypeLabelDescriptionSearchPreview}
  39. req.Header.Set("Accept", strings.Join(acceptHeaders, ", "))
  40. var labels []*Label
  41. resp, err := s.client.Do(ctx, req, &labels)
  42. if err != nil {
  43. return nil, resp, err
  44. }
  45. return labels, resp, nil
  46. }
  47. // GetLabel gets a single label.
  48. //
  49. // GitHub API docs: https://developer.github.com/v3/issues/labels/#get-a-single-label
  50. func (s *IssuesService) GetLabel(ctx context.Context, owner string, repo string, name string) (*Label, *Response, error) {
  51. u := fmt.Sprintf("repos/%v/%v/labels/%v", owner, repo, name)
  52. req, err := s.client.NewRequest("GET", u, nil)
  53. if err != nil {
  54. return nil, nil, err
  55. }
  56. // TODO: remove custom Accept header when this API fully launches.
  57. acceptHeaders := []string{mediaTypeGraphQLNodeIDPreview, mediaTypeLabelDescriptionSearchPreview}
  58. req.Header.Set("Accept", strings.Join(acceptHeaders, ", "))
  59. label := new(Label)
  60. resp, err := s.client.Do(ctx, req, label)
  61. if err != nil {
  62. return nil, resp, err
  63. }
  64. return label, resp, nil
  65. }
  66. // CreateLabel creates a new label on the specified repository.
  67. //
  68. // GitHub API docs: https://developer.github.com/v3/issues/labels/#create-a-label
  69. func (s *IssuesService) CreateLabel(ctx context.Context, owner string, repo string, label *Label) (*Label, *Response, error) {
  70. u := fmt.Sprintf("repos/%v/%v/labels", owner, repo)
  71. req, err := s.client.NewRequest("POST", u, label)
  72. if err != nil {
  73. return nil, nil, err
  74. }
  75. // TODO: remove custom Accept header when this API fully launches.
  76. acceptHeaders := []string{mediaTypeGraphQLNodeIDPreview, mediaTypeLabelDescriptionSearchPreview}
  77. req.Header.Set("Accept", strings.Join(acceptHeaders, ", "))
  78. l := new(Label)
  79. resp, err := s.client.Do(ctx, req, l)
  80. if err != nil {
  81. return nil, resp, err
  82. }
  83. return l, resp, nil
  84. }
  85. // EditLabel edits a label.
  86. //
  87. // GitHub API docs: https://developer.github.com/v3/issues/labels/#update-a-label
  88. func (s *IssuesService) EditLabel(ctx context.Context, owner string, repo string, name string, label *Label) (*Label, *Response, error) {
  89. u := fmt.Sprintf("repos/%v/%v/labels/%v", owner, repo, name)
  90. req, err := s.client.NewRequest("PATCH", u, label)
  91. if err != nil {
  92. return nil, nil, err
  93. }
  94. // TODO: remove custom Accept header when this API fully launches.
  95. acceptHeaders := []string{mediaTypeGraphQLNodeIDPreview, mediaTypeLabelDescriptionSearchPreview}
  96. req.Header.Set("Accept", strings.Join(acceptHeaders, ", "))
  97. l := new(Label)
  98. resp, err := s.client.Do(ctx, req, l)
  99. if err != nil {
  100. return nil, resp, err
  101. }
  102. return l, resp, nil
  103. }
  104. // DeleteLabel deletes a label.
  105. //
  106. // GitHub API docs: https://developer.github.com/v3/issues/labels/#delete-a-label
  107. func (s *IssuesService) DeleteLabel(ctx context.Context, owner string, repo string, name string) (*Response, error) {
  108. u := fmt.Sprintf("repos/%v/%v/labels/%v", owner, repo, name)
  109. req, err := s.client.NewRequest("DELETE", u, nil)
  110. if err != nil {
  111. return nil, err
  112. }
  113. return s.client.Do(ctx, req, nil)
  114. }
  115. // ListLabelsByIssue lists all labels for an issue.
  116. //
  117. // GitHub API docs: https://developer.github.com/v3/issues/labels/#list-labels-on-an-issue
  118. func (s *IssuesService) ListLabelsByIssue(ctx context.Context, owner string, repo string, number int, opt *ListOptions) ([]*Label, *Response, error) {
  119. u := fmt.Sprintf("repos/%v/%v/issues/%d/labels", owner, repo, number)
  120. u, err := addOptions(u, opt)
  121. if err != nil {
  122. return nil, nil, err
  123. }
  124. req, err := s.client.NewRequest("GET", u, nil)
  125. if err != nil {
  126. return nil, nil, err
  127. }
  128. // TODO: remove custom Accept header when this API fully launches.
  129. acceptHeaders := []string{mediaTypeGraphQLNodeIDPreview, mediaTypeLabelDescriptionSearchPreview}
  130. req.Header.Set("Accept", strings.Join(acceptHeaders, ", "))
  131. var labels []*Label
  132. resp, err := s.client.Do(ctx, req, &labels)
  133. if err != nil {
  134. return nil, resp, err
  135. }
  136. return labels, resp, nil
  137. }
  138. // AddLabelsToIssue adds labels to an issue.
  139. //
  140. // GitHub API docs: https://developer.github.com/v3/issues/labels/#add-labels-to-an-issue
  141. func (s *IssuesService) AddLabelsToIssue(ctx context.Context, owner string, repo string, number int, labels []string) ([]*Label, *Response, error) {
  142. u := fmt.Sprintf("repos/%v/%v/issues/%d/labels", owner, repo, number)
  143. req, err := s.client.NewRequest("POST", u, labels)
  144. if err != nil {
  145. return nil, nil, err
  146. }
  147. // TODO: remove custom Accept header when this API fully launches.
  148. acceptHeaders := []string{mediaTypeGraphQLNodeIDPreview, mediaTypeLabelDescriptionSearchPreview}
  149. req.Header.Set("Accept", strings.Join(acceptHeaders, ", "))
  150. var l []*Label
  151. resp, err := s.client.Do(ctx, req, &l)
  152. if err != nil {
  153. return nil, resp, err
  154. }
  155. return l, resp, nil
  156. }
  157. // RemoveLabelForIssue removes a label for an issue.
  158. //
  159. // GitHub API docs: https://developer.github.com/v3/issues/labels/#remove-a-label-from-an-issue
  160. func (s *IssuesService) RemoveLabelForIssue(ctx context.Context, owner string, repo string, number int, label string) (*Response, error) {
  161. u := fmt.Sprintf("repos/%v/%v/issues/%d/labels/%v", owner, repo, number, label)
  162. req, err := s.client.NewRequest("DELETE", u, nil)
  163. if err != nil {
  164. return nil, err
  165. }
  166. // TODO: remove custom Accept header when this API fully launches.
  167. req.Header.Set("Accept", mediaTypeLabelDescriptionSearchPreview)
  168. return s.client.Do(ctx, req, nil)
  169. }
  170. // ReplaceLabelsForIssue replaces all labels for an issue.
  171. //
  172. // GitHub API docs: https://developer.github.com/v3/issues/labels/#replace-all-labels-for-an-issue
  173. func (s *IssuesService) ReplaceLabelsForIssue(ctx context.Context, owner string, repo string, number int, labels []string) ([]*Label, *Response, error) {
  174. u := fmt.Sprintf("repos/%v/%v/issues/%d/labels", owner, repo, number)
  175. req, err := s.client.NewRequest("PUT", u, labels)
  176. if err != nil {
  177. return nil, nil, err
  178. }
  179. // TODO: remove custom Accept header when this API fully launches.
  180. acceptHeaders := []string{mediaTypeGraphQLNodeIDPreview, mediaTypeLabelDescriptionSearchPreview}
  181. req.Header.Set("Accept", strings.Join(acceptHeaders, ", "))
  182. var l []*Label
  183. resp, err := s.client.Do(ctx, req, &l)
  184. if err != nil {
  185. return nil, resp, err
  186. }
  187. return l, resp, nil
  188. }
  189. // RemoveLabelsForIssue removes all labels for an issue.
  190. //
  191. // GitHub API docs: https://developer.github.com/v3/issues/labels/#remove-all-labels-from-an-issue
  192. func (s *IssuesService) RemoveLabelsForIssue(ctx context.Context, owner string, repo string, number int) (*Response, error) {
  193. u := fmt.Sprintf("repos/%v/%v/issues/%d/labels", owner, repo, number)
  194. req, err := s.client.NewRequest("DELETE", u, nil)
  195. if err != nil {
  196. return nil, err
  197. }
  198. // TODO: remove custom Accept header when this API fully launches.
  199. req.Header.Set("Accept", mediaTypeLabelDescriptionSearchPreview)
  200. return s.client.Do(ctx, req, nil)
  201. }
  202. // ListLabelsForMilestone lists labels for every issue in a milestone.
  203. //
  204. // GitHub API docs: https://developer.github.com/v3/issues/labels/#get-labels-for-every-issue-in-a-milestone
  205. func (s *IssuesService) ListLabelsForMilestone(ctx context.Context, owner string, repo string, number int, opt *ListOptions) ([]*Label, *Response, error) {
  206. u := fmt.Sprintf("repos/%v/%v/milestones/%d/labels", owner, repo, number)
  207. u, err := addOptions(u, opt)
  208. if err != nil {
  209. return nil, nil, err
  210. }
  211. req, err := s.client.NewRequest("GET", u, nil)
  212. if err != nil {
  213. return nil, nil, err
  214. }
  215. // TODO: remove custom Accept header when this API fully launches.
  216. acceptHeaders := []string{mediaTypeGraphQLNodeIDPreview, mediaTypeLabelDescriptionSearchPreview}
  217. req.Header.Set("Accept", strings.Join(acceptHeaders, ", "))
  218. var labels []*Label
  219. resp, err := s.client.Do(ctx, req, &labels)
  220. if err != nil {
  221. return nil, resp, err
  222. }
  223. return labels, resp, nil
  224. }