branches.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. "fmt"
  19. "net/url"
  20. )
  21. // BranchesService handles communication with the branch related methods
  22. // of the GitLab API.
  23. //
  24. // GitLab API docs: https://docs.gitlab.com/ce/api/branches.html
  25. type BranchesService struct {
  26. client *Client
  27. }
  28. // Branch represents a GitLab branch.
  29. //
  30. // GitLab API docs: https://docs.gitlab.com/ce/api/branches.html
  31. type Branch struct {
  32. Commit *Commit `json:"commit"`
  33. Name string `json:"name"`
  34. Protected bool `json:"protected"`
  35. Merged bool `json:"merged"`
  36. DevelopersCanPush bool `json:"developers_can_push"`
  37. DevelopersCanMerge bool `json:"developers_can_merge"`
  38. }
  39. func (b Branch) String() string {
  40. return Stringify(b)
  41. }
  42. // ListBranchesOptions represents the available ListBranches() options.
  43. //
  44. // GitLab API docs:
  45. // https://docs.gitlab.com/ce/api/branches.html#list-repository-branches
  46. type ListBranchesOptions ListOptions
  47. // ListBranches gets a list of repository branches from a project, sorted by
  48. // name alphabetically.
  49. //
  50. // GitLab API docs:
  51. // https://docs.gitlab.com/ce/api/branches.html#list-repository-branches
  52. func (s *BranchesService) ListBranches(pid interface{}, opts *ListBranchesOptions, options ...OptionFunc) ([]*Branch, *Response, error) {
  53. project, err := parseID(pid)
  54. if err != nil {
  55. return nil, nil, err
  56. }
  57. u := fmt.Sprintf("projects/%s/repository/branches", url.QueryEscape(project))
  58. req, err := s.client.NewRequest("GET", u, opts, options)
  59. if err != nil {
  60. return nil, nil, err
  61. }
  62. var b []*Branch
  63. resp, err := s.client.Do(req, &b)
  64. if err != nil {
  65. return nil, resp, err
  66. }
  67. return b, resp, err
  68. }
  69. // GetBranch gets a single project repository branch.
  70. //
  71. // GitLab API docs:
  72. // https://docs.gitlab.com/ce/api/branches.html#get-single-repository-branch
  73. func (s *BranchesService) GetBranch(pid interface{}, branch string, options ...OptionFunc) (*Branch, *Response, error) {
  74. project, err := parseID(pid)
  75. if err != nil {
  76. return nil, nil, err
  77. }
  78. u := fmt.Sprintf("projects/%s/repository/branches/%s", url.QueryEscape(project), url.QueryEscape(branch))
  79. req, err := s.client.NewRequest("GET", u, nil, options)
  80. if err != nil {
  81. return nil, nil, err
  82. }
  83. b := new(Branch)
  84. resp, err := s.client.Do(req, b)
  85. if err != nil {
  86. return nil, resp, err
  87. }
  88. return b, resp, err
  89. }
  90. // ProtectBranchOptions represents the available ProtectBranch() options.
  91. //
  92. // GitLab API docs:
  93. // https://docs.gitlab.com/ce/api/branches.html#protect-repository-branch
  94. type ProtectBranchOptions struct {
  95. DevelopersCanPush *bool `url:"developers_can_push,omitempty" json:"developers_can_push,omitempty"`
  96. DevelopersCanMerge *bool `url:"developers_can_merge,omitempty" json:"developers_can_merge,omitempty"`
  97. }
  98. // ProtectBranch protects a single project repository branch. This is an
  99. // idempotent function, protecting an already protected repository branch
  100. // still returns a 200 OK status code.
  101. //
  102. // GitLab API docs:
  103. // https://docs.gitlab.com/ce/api/branches.html#protect-repository-branch
  104. func (s *BranchesService) ProtectBranch(pid interface{}, branch string, opts *ProtectBranchOptions, options ...OptionFunc) (*Branch, *Response, error) {
  105. project, err := parseID(pid)
  106. if err != nil {
  107. return nil, nil, err
  108. }
  109. u := fmt.Sprintf("projects/%s/repository/branches/%s/protect", url.QueryEscape(project), url.QueryEscape(branch))
  110. req, err := s.client.NewRequest("PUT", u, opts, options)
  111. if err != nil {
  112. return nil, nil, err
  113. }
  114. b := new(Branch)
  115. resp, err := s.client.Do(req, b)
  116. if err != nil {
  117. return nil, resp, err
  118. }
  119. return b, resp, err
  120. }
  121. // UnprotectBranch unprotects a single project repository branch. This is an
  122. // idempotent function, unprotecting an already unprotected repository branch
  123. // still returns a 200 OK status code.
  124. //
  125. // GitLab API docs:
  126. // https://docs.gitlab.com/ce/api/branches.html#unprotect-repository-branch
  127. func (s *BranchesService) UnprotectBranch(pid interface{}, branch string, options ...OptionFunc) (*Branch, *Response, error) {
  128. project, err := parseID(pid)
  129. if err != nil {
  130. return nil, nil, err
  131. }
  132. u := fmt.Sprintf("projects/%s/repository/branches/%s/unprotect", url.QueryEscape(project), url.QueryEscape(branch))
  133. req, err := s.client.NewRequest("PUT", u, nil, options)
  134. if err != nil {
  135. return nil, nil, err
  136. }
  137. b := new(Branch)
  138. resp, err := s.client.Do(req, b)
  139. if err != nil {
  140. return nil, resp, err
  141. }
  142. return b, resp, err
  143. }
  144. // CreateBranchOptions represents the available CreateBranch() options.
  145. //
  146. // GitLab API docs:
  147. // https://docs.gitlab.com/ce/api/branches.html#create-repository-branch
  148. type CreateBranchOptions struct {
  149. Branch *string `url:"branch,omitempty" json:"branch,omitempty"`
  150. Ref *string `url:"ref,omitempty" json:"ref,omitempty"`
  151. }
  152. // CreateBranch creates branch from commit SHA or existing branch.
  153. //
  154. // GitLab API docs:
  155. // https://docs.gitlab.com/ce/api/branches.html#create-repository-branch
  156. func (s *BranchesService) CreateBranch(pid interface{}, opt *CreateBranchOptions, options ...OptionFunc) (*Branch, *Response, error) {
  157. project, err := parseID(pid)
  158. if err != nil {
  159. return nil, nil, err
  160. }
  161. u := fmt.Sprintf("projects/%s/repository/branches", url.QueryEscape(project))
  162. req, err := s.client.NewRequest("POST", u, opt, options)
  163. if err != nil {
  164. return nil, nil, err
  165. }
  166. b := new(Branch)
  167. resp, err := s.client.Do(req, b)
  168. if err != nil {
  169. return nil, resp, err
  170. }
  171. return b, resp, err
  172. }
  173. // DeleteBranch deletes an existing branch.
  174. //
  175. // GitLab API docs:
  176. // https://docs.gitlab.com/ce/api/branches.html#delete-repository-branch
  177. func (s *BranchesService) DeleteBranch(pid interface{}, branch string, options ...OptionFunc) (*Response, error) {
  178. project, err := parseID(pid)
  179. if err != nil {
  180. return nil, err
  181. }
  182. u := fmt.Sprintf("projects/%s/repository/branches/%s", url.QueryEscape(project), url.QueryEscape(branch))
  183. req, err := s.client.NewRequest("DELETE", u, nil, options)
  184. if err != nil {
  185. return nil, err
  186. }
  187. return s.client.Do(req, nil)
  188. }
  189. // DeleteMergedBranches deletes all branches that are merged into the project's default branch.
  190. //
  191. // GitLab API docs:
  192. // https://docs.gitlab.com/ce/api/branches.html#delete-merged-branches
  193. func (s *BranchesService) DeleteMergedBranches(pid interface{}, 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/merged_branches", url.QueryEscape(project))
  199. req, err := s.client.NewRequest("DELETE", u, nil, options)
  200. if err != nil {
  201. return nil, err
  202. }
  203. return s.client.Do(req, nil)
  204. }