repository_files.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. // RepositoryFilesService handles communication with the repository files
  23. // related methods of the GitLab API.
  24. //
  25. // GitLab API docs: https://docs.gitlab.com/ce/api/repository_files.html
  26. type RepositoryFilesService struct {
  27. client *Client
  28. }
  29. // File represents a GitLab repository file.
  30. //
  31. // GitLab API docs: https://docs.gitlab.com/ce/api/repository_files.html
  32. type File struct {
  33. FileName string `json:"file_name"`
  34. FilePath string `json:"file_path"`
  35. Size int `json:"size"`
  36. Encoding string `json:"encoding"`
  37. Content string `json:"content"`
  38. Ref string `json:"ref"`
  39. BlobID string `json:"blob_id"`
  40. CommitID string `json:"commit_id"`
  41. }
  42. func (r File) String() string {
  43. return Stringify(r)
  44. }
  45. // GetFileOptions represents the available GetFile() options.
  46. //
  47. // GitLab API docs:
  48. // https://docs.gitlab.com/ce/api/repository_files.html#get-file-from-repository
  49. type GetFileOptions struct {
  50. Ref *string `url:"ref,omitempty" json:"ref,omitempty"`
  51. }
  52. // GetFile allows you to receive information about a file in repository like
  53. // name, size, content. Note that file content is Base64 encoded.
  54. //
  55. // GitLab API docs:
  56. // https://docs.gitlab.com/ce/api/repository_files.html#get-file-from-repository
  57. func (s *RepositoryFilesService) GetFile(pid interface{}, fileName string, opt *GetFileOptions, options ...OptionFunc) (*File, *Response, error) {
  58. project, err := parseID(pid)
  59. if err != nil {
  60. return nil, nil, err
  61. }
  62. u := fmt.Sprintf("projects/%s/repository/files/%s", url.QueryEscape(project), url.PathEscape(fileName))
  63. req, err := s.client.NewRequest("GET", u, opt, options)
  64. if err != nil {
  65. return nil, nil, err
  66. }
  67. f := new(File)
  68. resp, err := s.client.Do(req, f)
  69. if err != nil {
  70. return nil, resp, err
  71. }
  72. return f, resp, err
  73. }
  74. // GetRawFileOptions represents the available GetRawFile() options.
  75. //
  76. // GitLab API docs:
  77. // https://docs.gitlab.com/ce/api/repository_files.html#get-raw-file-from-repository
  78. type GetRawFileOptions struct {
  79. Ref *string `url:"ref,omitempty" json:"ref,omitempty"`
  80. }
  81. // GetRawFile allows you to receive the raw file in repository.
  82. //
  83. // GitLab API docs:
  84. // https://docs.gitlab.com/ce/api/repository_files.html#get-raw-file-from-repository
  85. func (s *RepositoryFilesService) GetRawFile(pid interface{}, fileName string, opt *GetRawFileOptions, options ...OptionFunc) ([]byte, *Response, error) {
  86. project, err := parseID(pid)
  87. if err != nil {
  88. return nil, nil, err
  89. }
  90. u := fmt.Sprintf("projects/%s/repository/files/%s/raw", url.QueryEscape(project), url.PathEscape(fileName))
  91. req, err := s.client.NewRequest("GET", u, opt, options)
  92. if err != nil {
  93. return nil, nil, err
  94. }
  95. var f bytes.Buffer
  96. resp, err := s.client.Do(req, &f)
  97. if err != nil {
  98. return nil, resp, err
  99. }
  100. return f.Bytes(), resp, err
  101. }
  102. // FileInfo represents file details of a GitLab repository file.
  103. //
  104. // GitLab API docs: https://docs.gitlab.com/ce/api/repository_files.html
  105. type FileInfo struct {
  106. FilePath string `json:"file_path"`
  107. Branch string `json:"branch"`
  108. }
  109. func (r FileInfo) String() string {
  110. return Stringify(r)
  111. }
  112. // CreateFileOptions represents the available CreateFile() options.
  113. //
  114. // GitLab API docs:
  115. // https://docs.gitlab.com/ce/api/repository_files.html#create-new-file-in-repository
  116. type CreateFileOptions struct {
  117. Branch *string `url:"branch,omitempty" json:"branch,omitempty"`
  118. Encoding *string `url:"encoding,omitempty" json:"encoding,omitempty"`
  119. AuthorEmail *string `url:"author_email,omitempty" json:"author_email,omitempty"`
  120. AuthorName *string `url:"author_name,omitempty" json:"author_name,omitempty"`
  121. Content *string `url:"content,omitempty" json:"content,omitempty"`
  122. CommitMessage *string `url:"commit_message,omitempty" json:"commit_message,omitempty"`
  123. }
  124. // CreateFile creates a new file in a repository.
  125. //
  126. // GitLab API docs:
  127. // https://docs.gitlab.com/ce/api/repository_files.html#create-new-file-in-repository
  128. func (s *RepositoryFilesService) CreateFile(pid interface{}, fileName string, opt *CreateFileOptions, options ...OptionFunc) (*FileInfo, *Response, error) {
  129. project, err := parseID(pid)
  130. if err != nil {
  131. return nil, nil, err
  132. }
  133. u := fmt.Sprintf("projects/%s/repository/files/%s", url.QueryEscape(project), url.PathEscape(fileName))
  134. req, err := s.client.NewRequest("POST", u, opt, options)
  135. if err != nil {
  136. return nil, nil, err
  137. }
  138. f := new(FileInfo)
  139. resp, err := s.client.Do(req, f)
  140. if err != nil {
  141. return nil, resp, err
  142. }
  143. return f, resp, err
  144. }
  145. // UpdateFileOptions represents the available UpdateFile() options.
  146. //
  147. // GitLab API docs:
  148. // https://docs.gitlab.com/ce/api/repository_files.html#update-existing-file-in-repository
  149. type UpdateFileOptions struct {
  150. Branch *string `url:"branch,omitempty" json:"branch,omitempty"`
  151. Encoding *string `url:"encoding,omitempty" json:"encoding,omitempty"`
  152. AuthorEmail *string `url:"author_email,omitempty" json:"author_email,omitempty"`
  153. AuthorName *string `url:"author_name,omitempty" json:"author_name,omitempty"`
  154. Content *string `url:"content,omitempty" json:"content,omitempty"`
  155. CommitMessage *string `url:"commit_message,omitempty" json:"commit_message,omitempty"`
  156. LastCommitID *string `url:"last_commit_id,omitempty" json:"last_commit_id,omitempty"`
  157. }
  158. // UpdateFile updates an existing file in a repository
  159. //
  160. // GitLab API docs:
  161. // https://docs.gitlab.com/ce/api/repository_files.html#update-existing-file-in-repository
  162. func (s *RepositoryFilesService) UpdateFile(pid interface{}, fileName string, opt *UpdateFileOptions, options ...OptionFunc) (*FileInfo, *Response, error) {
  163. project, err := parseID(pid)
  164. if err != nil {
  165. return nil, nil, err
  166. }
  167. u := fmt.Sprintf("projects/%s/repository/files/%s", url.QueryEscape(project), url.PathEscape(fileName))
  168. req, err := s.client.NewRequest("PUT", u, opt, options)
  169. if err != nil {
  170. return nil, nil, err
  171. }
  172. f := new(FileInfo)
  173. resp, err := s.client.Do(req, f)
  174. if err != nil {
  175. return nil, resp, err
  176. }
  177. return f, resp, err
  178. }
  179. // DeleteFileOptions represents the available DeleteFile() options.
  180. //
  181. // GitLab API docs:
  182. // https://docs.gitlab.com/ce/api/repository_files.html#delete-existing-file-in-repository
  183. type DeleteFileOptions struct {
  184. Branch *string `url:"branch,omitempty" json:"branch,omitempty"`
  185. AuthorEmail *string `url:"author_email,omitempty" json:"author_email,omitempty"`
  186. AuthorName *string `url:"author_name,omitempty" json:"author_name,omitempty"`
  187. CommitMessage *string `url:"commit_message,omitempty" json:"commit_message,omitempty"`
  188. }
  189. // DeleteFile deletes an existing file in a repository
  190. //
  191. // GitLab API docs:
  192. // https://docs.gitlab.com/ce/api/repository_files.html#delete-existing-file-in-repository
  193. func (s *RepositoryFilesService) DeleteFile(pid interface{}, fileName string, opt *DeleteFileOptions, options ...OptionFunc) (*Response, error) {
  194. project, err := parseID(pid)
  195. if err != nil {
  196. return nil, err
  197. }
  198. u := fmt.Sprintf("projects/%s/repository/files/%s", url.QueryEscape(project), url.PathEscape(fileName))
  199. req, err := s.client.NewRequest("DELETE", u, opt, options)
  200. if err != nil {
  201. return nil, err
  202. }
  203. return s.client.Do(req, nil)
  204. }