snippets.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. "bytes"
  19. "fmt"
  20. "time"
  21. )
  22. // SnippetsService handles communication with the snippets
  23. // related methods of the GitLab API.
  24. //
  25. // GitLab API docs: https://docs.gitlab.com/ce/api/snippets.html
  26. type SnippetsService struct {
  27. client *Client
  28. }
  29. // Snippet represents a GitLab snippet.
  30. //
  31. // GitLab API docs: https://docs.gitlab.com/ce/api/snippets.html
  32. type Snippet struct {
  33. ID int `json:"id"`
  34. Title string `json:"title"`
  35. FileName string `json:"file_name"`
  36. Description string `json:"description"`
  37. Author struct {
  38. ID int `json:"id"`
  39. Username string `json:"username"`
  40. Email string `json:"email"`
  41. Name string `json:"name"`
  42. State string `json:"state"`
  43. CreatedAt *time.Time `json:"created_at"`
  44. } `json:"author"`
  45. UpdatedAt *time.Time `json:"updated_at"`
  46. CreatedAt *time.Time `json:"created_at"`
  47. WebURL string `json:"web_url"`
  48. RawURL string `json:"raw_url"`
  49. }
  50. func (s Snippet) String() string {
  51. return Stringify(s)
  52. }
  53. // ListSnippetsOptions represents the available ListSnippets() options.
  54. //
  55. // GitLab API docs: https://docs.gitlab.com/ce/api/snippets.html#list-snippets
  56. type ListSnippetsOptions ListOptions
  57. // ListSnippets gets a list of snippets.
  58. //
  59. // GitLab API docs: https://docs.gitlab.com/ce/api/snippets.html#list-snippets
  60. func (s *SnippetsService) ListSnippets(opt *ListSnippetsOptions, options ...OptionFunc) ([]*Snippet, *Response, error) {
  61. req, err := s.client.NewRequest("GET", "snippets", opt, options)
  62. if err != nil {
  63. return nil, nil, err
  64. }
  65. var ps []*Snippet
  66. resp, err := s.client.Do(req, &ps)
  67. if err != nil {
  68. return nil, resp, err
  69. }
  70. return ps, resp, err
  71. }
  72. // GetSnippet gets a single snippet
  73. //
  74. // GitLab API docs:
  75. // https://docs.gitlab.com/ce/api/snippets.html#single-snippet
  76. func (s *SnippetsService) GetSnippet(snippet int, options ...OptionFunc) (*Snippet, *Response, error) {
  77. u := fmt.Sprintf("snippets/%d", snippet)
  78. req, err := s.client.NewRequest("GET", u, nil, options)
  79. if err != nil {
  80. return nil, nil, err
  81. }
  82. ps := new(Snippet)
  83. resp, err := s.client.Do(req, ps)
  84. if err != nil {
  85. return nil, resp, err
  86. }
  87. return ps, resp, err
  88. }
  89. // CreateSnippetOptions represents the available CreateSnippet() options.
  90. //
  91. // GitLab API docs:
  92. // https://docs.gitlab.com/ce/api/snippets.html#create-new-snippet
  93. type CreateSnippetOptions struct {
  94. Title *string `url:"title,omitempty" json:"title,omitempty"`
  95. FileName *string `url:"file_name,omitempty" json:"file_name,omitempty"`
  96. Description *string `url:"description,omitempty" json:"description,omitempty"`
  97. Content *string `url:"content,omitempty" json:"content,omitempty"`
  98. Visibility *VisibilityValue `url:"visibility,omitempty" json:"visibility,omitempty"`
  99. }
  100. // CreateSnippet creates a new snippet. The user must have permission
  101. // to create new snippets.
  102. //
  103. // GitLab API docs:
  104. // https://docs.gitlab.com/ce/api/snippets.html#create-new-snippet
  105. func (s *SnippetsService) CreateSnippet(opt *CreateSnippetOptions, options ...OptionFunc) (*Snippet, *Response, error) {
  106. req, err := s.client.NewRequest("POST", "snippets", opt, options)
  107. if err != nil {
  108. return nil, nil, err
  109. }
  110. ps := new(Snippet)
  111. resp, err := s.client.Do(req, ps)
  112. if err != nil {
  113. return nil, resp, err
  114. }
  115. return ps, resp, err
  116. }
  117. // UpdateSnippetOptions represents the available UpdateSnippet() options.
  118. //
  119. // GitLab API docs:
  120. // https://docs.gitlab.com/ce/api/snippets.html#update-snippet
  121. type UpdateSnippetOptions struct {
  122. Title *string `url:"title,omitempty" json:"title,omitempty"`
  123. FileName *string `url:"file_name,omitempty" json:"file_name,omitempty"`
  124. Description *string `url:"description,omitempty" json:"description,omitempty"`
  125. Content *string `url:"content,omitempty" json:"content,omitempty"`
  126. Visibility *VisibilityValue `url:"visibility,omitempty" json:"visibility,omitempty"`
  127. }
  128. // UpdateSnippet updates an existing snippet. The user must have
  129. // permission to change an existing snippet.
  130. //
  131. // GitLab API docs:
  132. // https://docs.gitlab.com/ce/api/snippets.html#update-snippet
  133. func (s *SnippetsService) UpdateSnippet(snippet int, opt *UpdateSnippetOptions, options ...OptionFunc) (*Snippet, *Response, error) {
  134. u := fmt.Sprintf("snippets/%d", snippet)
  135. req, err := s.client.NewRequest("PUT", u, opt, options)
  136. if err != nil {
  137. return nil, nil, err
  138. }
  139. ps := new(Snippet)
  140. resp, err := s.client.Do(req, ps)
  141. if err != nil {
  142. return nil, resp, err
  143. }
  144. return ps, resp, err
  145. }
  146. // DeleteSnippet deletes an existing snippet. This is an idempotent
  147. // function and deleting a non-existent snippet still returns a 200 OK status
  148. // code.
  149. //
  150. // GitLab API docs:
  151. // https://docs.gitlab.com/ce/api/snippets.html#delete-snippet
  152. func (s *SnippetsService) DeleteSnippet(snippet int, options ...OptionFunc) (*Response, error) {
  153. u := fmt.Sprintf("snippets/%d", snippet)
  154. req, err := s.client.NewRequest("DELETE", u, nil, options)
  155. if err != nil {
  156. return nil, err
  157. }
  158. return s.client.Do(req, nil)
  159. }
  160. // SnippetContent returns the raw snippet as plain text.
  161. //
  162. // GitLab API docs:
  163. // https://docs.gitlab.com/ce/api/snippets.html#snippet-content
  164. func (s *SnippetsService) SnippetContent(snippet int, options ...OptionFunc) ([]byte, *Response, error) {
  165. u := fmt.Sprintf("snippets/%d/raw", snippet)
  166. req, err := s.client.NewRequest("GET", u, nil, options)
  167. if err != nil {
  168. return nil, nil, err
  169. }
  170. var b bytes.Buffer
  171. resp, err := s.client.Do(req, &b)
  172. if err != nil {
  173. return nil, resp, err
  174. }
  175. return b.Bytes(), resp, err
  176. }
  177. // ExploreSnippetsOptions represents the available ExploreSnippets() options.
  178. //
  179. // GitLab API docs:
  180. // https://docs.gitlab.com/ce/api/snippets.html#explore-all-public-snippets
  181. type ExploreSnippetsOptions ListOptions
  182. // ExploreSnippets gets the list of public snippets.
  183. //
  184. // GitLab API docs:
  185. // https://docs.gitlab.com/ce/api/snippets.html#explore-all-public-snippets
  186. func (s *SnippetsService) ExploreSnippets(opt *ExploreSnippetsOptions, options ...OptionFunc) ([]*Snippet, *Response, error) {
  187. req, err := s.client.NewRequest("GET", "snippets/public", nil, options)
  188. if err != nil {
  189. return nil, nil, err
  190. }
  191. var ps []*Snippet
  192. resp, err := s.client.Do(req, &ps)
  193. if err != nil {
  194. return nil, resp, err
  195. }
  196. return ps, resp, err
  197. }