repositories.go 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. // Blob gets information about blob in repository like size and content. Note
  74. // that blob content is Base64 encoded.
  75. //
  76. // GitLab API docs:
  77. // https://docs.gitlab.com/ce/api/repositories.html#get-a-blob-from-repository
  78. func (s *RepositoriesService) Blob(pid interface{}, sha string, options ...OptionFunc) ([]byte, *Response, error) {
  79. project, err := parseID(pid)
  80. if err != nil {
  81. return nil, nil, err
  82. }
  83. u := fmt.Sprintf("projects/%s/repository/blobs/%s", url.QueryEscape(project), sha)
  84. req, err := s.client.NewRequest("GET", u, nil, options)
  85. if err != nil {
  86. return nil, nil, err
  87. }
  88. var b bytes.Buffer
  89. resp, err := s.client.Do(req, &b)
  90. if err != nil {
  91. return nil, resp, err
  92. }
  93. return b.Bytes(), resp, err
  94. }
  95. // RawBlobContent gets the raw file contents for a blob by blob SHA.
  96. //
  97. // GitLab API docs:
  98. // https://docs.gitlab.com/ce/api/repositories.html#raw-blob-content
  99. func (s *RepositoriesService) RawBlobContent(pid interface{}, sha string, options ...OptionFunc) ([]byte, *Response, error) {
  100. project, err := parseID(pid)
  101. if err != nil {
  102. return nil, nil, err
  103. }
  104. u := fmt.Sprintf("projects/%s/repository/blobs/%s/raw", url.QueryEscape(project), sha)
  105. req, err := s.client.NewRequest("GET", u, nil, options)
  106. if err != nil {
  107. return nil, nil, err
  108. }
  109. var b bytes.Buffer
  110. resp, err := s.client.Do(req, &b)
  111. if err != nil {
  112. return nil, resp, err
  113. }
  114. return b.Bytes(), resp, err
  115. }
  116. // ArchiveOptions represents the available Archive() options.
  117. //
  118. // GitLab API docs:
  119. // https://docs.gitlab.com/ce/api/repositories.html#get-file-archive
  120. type ArchiveOptions struct {
  121. SHA *string `url:"sha,omitempty" json:"sha,omitempty"`
  122. }
  123. // Archive gets an archive of the repository.
  124. //
  125. // GitLab API docs:
  126. // https://docs.gitlab.com/ce/api/repositories.html#get-file-archive
  127. func (s *RepositoriesService) Archive(pid interface{}, opt *ArchiveOptions, options ...OptionFunc) ([]byte, *Response, error) {
  128. project, err := parseID(pid)
  129. if err != nil {
  130. return nil, nil, err
  131. }
  132. u := fmt.Sprintf("projects/%s/repository/archive", url.QueryEscape(project))
  133. req, err := s.client.NewRequest("GET", u, opt, options)
  134. if err != nil {
  135. return nil, nil, err
  136. }
  137. var b bytes.Buffer
  138. resp, err := s.client.Do(req, &b)
  139. if err != nil {
  140. return nil, resp, err
  141. }
  142. return b.Bytes(), resp, err
  143. }
  144. // Compare represents the result of a comparison of branches, tags or commits.
  145. //
  146. // GitLab API docs:
  147. // https://docs.gitlab.com/ce/api/repositories.html#compare-branches-tags-or-commits
  148. type Compare struct {
  149. Commit *Commit `json:"commit"`
  150. Commits []*Commit `json:"commits"`
  151. Diffs []*Diff `json:"diffs"`
  152. CompareTimeout bool `json:"compare_timeout"`
  153. CompareSameRef bool `json:"compare_same_ref"`
  154. }
  155. func (c Compare) String() string {
  156. return Stringify(c)
  157. }
  158. // CompareOptions represents the available Compare() options.
  159. //
  160. // GitLab API docs:
  161. // https://docs.gitlab.com/ce/api/repositories.html#compare-branches-tags-or-commits
  162. type CompareOptions struct {
  163. From *string `url:"from,omitempty" json:"from,omitempty"`
  164. To *string `url:"to,omitempty" json:"to,omitempty"`
  165. Straight *bool `url:"straight,omitempty" json:"straight,omitempty"`
  166. }
  167. // Compare compares branches, tags or commits.
  168. //
  169. // GitLab API docs:
  170. // https://docs.gitlab.com/ce/api/repositories.html#compare-branches-tags-or-commits
  171. func (s *RepositoriesService) Compare(pid interface{}, opt *CompareOptions, options ...OptionFunc) (*Compare, *Response, error) {
  172. project, err := parseID(pid)
  173. if err != nil {
  174. return nil, nil, err
  175. }
  176. u := fmt.Sprintf("projects/%s/repository/compare", url.QueryEscape(project))
  177. req, err := s.client.NewRequest("GET", u, opt, options)
  178. if err != nil {
  179. return nil, nil, err
  180. }
  181. c := new(Compare)
  182. resp, err := s.client.Do(req, c)
  183. if err != nil {
  184. return nil, resp, err
  185. }
  186. return c, resp, err
  187. }
  188. // Contributor represents a GitLap contributor.
  189. //
  190. // GitLab API docs: https://docs.gitlab.com/ce/api/repositories.html#contributors
  191. type Contributor struct {
  192. Name string `json:"name,omitempty"`
  193. Email string `json:"email,omitempty"`
  194. Commits int `json:"commits,omitempty"`
  195. Additions int `json:"additions,omitempty"`
  196. Deletions int `json:"deletions,omitempty"`
  197. }
  198. func (c Contributor) String() string {
  199. return Stringify(c)
  200. }
  201. // ListContributorsOptions represents the available ListContributors() 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. }
  225. // MergeBaseOptions represents the available MergeBase() options.
  226. //
  227. // GitLab API docs:
  228. // https://docs.gitlab.com/ce/api/repositories.html#merge-base
  229. type MergeBaseOptions struct {
  230. Ref []string `url:"refs[],omitempty" json:"refs,omitempty"`
  231. }
  232. // MergeBase gets the common ancestor for 2 refs (commit SHAs, branch
  233. // names or tags).
  234. //
  235. // GitLab API docs:
  236. // https://docs.gitlab.com/ce/api/repositories.html#merge-base
  237. func (s *RepositoriesService) MergeBase(pid interface{}, opt *MergeBaseOptions, options ...OptionFunc) (*Commit, *Response, error) {
  238. project, err := parseID(pid)
  239. if err != nil {
  240. return nil, nil, err
  241. }
  242. u := fmt.Sprintf("projects/%s/repository/merge_base", url.QueryEscape(project))
  243. req, err := s.client.NewRequest("GET", u, opt, options)
  244. if err != nil {
  245. return nil, nil, err
  246. }
  247. c := new(Commit)
  248. resp, err := s.client.Do(req, c)
  249. if err != nil {
  250. return nil, resp, err
  251. }
  252. return c, resp, err
  253. }