repositories.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. // RepositoriesService handles communication with the repositories related
  23. // methods of the GitLab API.
  24. //
  25. // GitLab API docs: https://docs.gitlab.com/ce/api/repositories.html
  26. type RepositoriesService struct {
  27. client *Client
  28. }
  29. // TreeNode represents a GitLab repository file or directory.
  30. //
  31. // GitLab API docs: https://docs.gitlab.com/ce/api/repositories.html
  32. type TreeNode struct {
  33. ID string `json:"id"`
  34. Name string `json:"name"`
  35. Type string `json:"type"`
  36. Path string `json:"path"`
  37. Mode string `json:"mode"`
  38. }
  39. func (t TreeNode) String() string {
  40. return Stringify(t)
  41. }
  42. // ListTreeOptions represents the available ListTree() options.
  43. //
  44. // GitLab API docs:
  45. // https://docs.gitlab.com/ce/api/repositories.html#list-repository-tree
  46. type ListTreeOptions struct {
  47. ListOptions
  48. Path *string `url:"path,omitempty" json:"path,omitempty"`
  49. Ref *string `url:"ref,omitempty" json:"ref,omitempty"`
  50. Recursive *bool `url:"recursive,omitempty" json:"recursive,omitempty"`
  51. }
  52. // ListTree gets a list of repository files and directories in a project.
  53. //
  54. // GitLab API docs:
  55. // https://docs.gitlab.com/ce/api/repositories.html#list-repository-tree
  56. func (s *RepositoriesService) ListTree(pid interface{}, opt *ListTreeOptions, options ...OptionFunc) ([]*TreeNode, *Response, error) {
  57. project, err := parseID(pid)
  58. if err != nil {
  59. return nil, nil, err
  60. }
  61. u := fmt.Sprintf("projects/%s/repository/tree", url.QueryEscape(project))
  62. req, err := s.client.NewRequest("GET", u, opt, options)
  63. if err != nil {
  64. return nil, nil, err
  65. }
  66. var t []*TreeNode
  67. resp, err := s.client.Do(req, &t)
  68. if err != nil {
  69. return nil, resp, err
  70. }
  71. return t, resp, err
  72. }
  73. // RawFileContent gets the raw file contents for a file by commit SHA and path
  74. //
  75. // GitLab API docs:
  76. // https://docs.gitlab.com/ce/api/repositories.html#raw-file-content
  77. func (s *RepositoriesService) RawFileContent(pid interface{}, sha string, options ...OptionFunc) ([]byte, *Response, error) {
  78. project, err := parseID(pid)
  79. if err != nil {
  80. return nil, nil, err
  81. }
  82. u := fmt.Sprintf("projects/%s/repository/blobs/%s", url.QueryEscape(project), sha)
  83. req, err := s.client.NewRequest("GET", u, nil, options)
  84. if err != nil {
  85. return nil, nil, err
  86. }
  87. var b bytes.Buffer
  88. resp, err := s.client.Do(req, &b)
  89. if err != nil {
  90. return nil, resp, err
  91. }
  92. return b.Bytes(), resp, err
  93. }
  94. // RawBlobContent gets the raw file contents for a blob by blob SHA.
  95. //
  96. // GitLab API docs:
  97. // https://docs.gitlab.com/ce/api/repositories.html#raw-blob-content
  98. func (s *RepositoriesService) RawBlobContent(pid interface{}, sha string, options ...OptionFunc) ([]byte, *Response, error) {
  99. project, err := parseID(pid)
  100. if err != nil {
  101. return nil, nil, err
  102. }
  103. u := fmt.Sprintf("projects/%s/repository/blobs/%s/raw", url.QueryEscape(project), sha)
  104. req, err := s.client.NewRequest("GET", u, nil, options)
  105. if err != nil {
  106. return nil, nil, err
  107. }
  108. var b bytes.Buffer
  109. resp, err := s.client.Do(req, &b)
  110. if err != nil {
  111. return nil, resp, err
  112. }
  113. return b.Bytes(), resp, err
  114. }
  115. // ArchiveOptions represents the available Archive() options.
  116. //
  117. // GitLab API docs:
  118. // https://docs.gitlab.com/ce/api/repositories.html#get-file-archive
  119. type ArchiveOptions struct {
  120. SHA *string `url:"sha,omitempty" json:"sha,omitempty"`
  121. }
  122. // Archive gets an archive of the repository.
  123. //
  124. // GitLab API docs:
  125. // https://docs.gitlab.com/ce/api/repositories.html#get-file-archive
  126. func (s *RepositoriesService) Archive(pid interface{}, opt *ArchiveOptions, options ...OptionFunc) ([]byte, *Response, error) {
  127. project, err := parseID(pid)
  128. if err != nil {
  129. return nil, nil, err
  130. }
  131. u := fmt.Sprintf("projects/%s/repository/archive", url.QueryEscape(project))
  132. req, err := s.client.NewRequest("GET", u, opt, options)
  133. if err != nil {
  134. return nil, nil, err
  135. }
  136. var b bytes.Buffer
  137. resp, err := s.client.Do(req, &b)
  138. if err != nil {
  139. return nil, resp, err
  140. }
  141. return b.Bytes(), resp, err
  142. }
  143. // Compare represents the result of a comparison of branches, tags or commits.
  144. //
  145. // GitLab API docs:
  146. // https://docs.gitlab.com/ce/api/repositories.html#compare-branches-tags-or-commits
  147. type Compare struct {
  148. Commit *Commit `json:"commit"`
  149. Commits []*Commit `json:"commits"`
  150. Diffs []*Diff `json:"diffs"`
  151. CompareTimeout bool `json:"compare_timeout"`
  152. CompareSameRef bool `json:"compare_same_ref"`
  153. }
  154. func (c Compare) String() string {
  155. return Stringify(c)
  156. }
  157. // CompareOptions represents the available Compare() options.
  158. //
  159. // GitLab API docs:
  160. // https://docs.gitlab.com/ce/api/repositories.html#compare-branches-tags-or-commits
  161. type CompareOptions struct {
  162. From *string `url:"from,omitempty" json:"from,omitempty"`
  163. To *string `url:"to,omitempty" json:"to,omitempty"`
  164. Straight *bool `url:"straight,omitempty" json:"straight,omitempty"`
  165. }
  166. // Compare compares branches, tags or commits.
  167. //
  168. // GitLab API docs:
  169. // https://docs.gitlab.com/ce/api/repositories.html#compare-branches-tags-or-commits
  170. func (s *RepositoriesService) Compare(pid interface{}, opt *CompareOptions, options ...OptionFunc) (*Compare, *Response, error) {
  171. project, err := parseID(pid)
  172. if err != nil {
  173. return nil, nil, err
  174. }
  175. u := fmt.Sprintf("projects/%s/repository/compare", url.QueryEscape(project))
  176. req, err := s.client.NewRequest("GET", u, opt, options)
  177. if err != nil {
  178. return nil, nil, err
  179. }
  180. c := new(Compare)
  181. resp, err := s.client.Do(req, c)
  182. if err != nil {
  183. return nil, resp, err
  184. }
  185. return c, resp, err
  186. }
  187. // Contributor represents a GitLap contributor.
  188. //
  189. // GitLab API docs: https://docs.gitlab.com/ce/api/repositories.html#contributors
  190. type Contributor struct {
  191. Name string `json:"name,omitempty"`
  192. Email string `json:"email,omitempty"`
  193. Commits int `json:"commits,omitempty"`
  194. Additions int `json:"additions,omitempty"`
  195. Deletions int `json:"deletions,omitempty"`
  196. }
  197. func (c Contributor) String() string {
  198. return Stringify(c)
  199. }
  200. // ListContributorsOptions represents the available ListContributorsOptions()
  201. // options.
  202. //
  203. // GitLab API docs: https://docs.gitlab.com/ce/api/repositories.html#contributors
  204. type ListContributorsOptions ListOptions
  205. // Contributors gets the repository contributors list.
  206. //
  207. // GitLab API docs: https://docs.gitlab.com/ce/api/repositories.html#contributors
  208. func (s *RepositoriesService) Contributors(pid interface{}, opt *ListContributorsOptions, options ...OptionFunc) ([]*Contributor, *Response, error) {
  209. project, err := parseID(pid)
  210. if err != nil {
  211. return nil, nil, err
  212. }
  213. u := fmt.Sprintf("projects/%s/repository/contributors", url.QueryEscape(project))
  214. req, err := s.client.NewRequest("GET", u, opt, options)
  215. if err != nil {
  216. return nil, nil, err
  217. }
  218. var c []*Contributor
  219. resp, err := s.client.Do(req, &c)
  220. if err != nil {
  221. return nil, resp, err
  222. }
  223. return c, resp, err
  224. }