project_snippets.go 6.4 KB

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