本站源代码
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.

262 lines
8.1KB

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