protected_branches.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. //
  2. // Copyright 2017, Sander van Harmelen, Michael Lihs
  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. // ProtectedBranchesService handles communication with the protected branch
  22. // related methods of the GitLab API.
  23. //
  24. // GitLab API docs:
  25. // https://docs.gitlab.com/ce/api/protected_branches.html#protected-branches-api
  26. type ProtectedBranchesService struct {
  27. client *Client
  28. }
  29. // BranchAccessDescription represents the access description for a protected
  30. // branch.
  31. //
  32. // GitLab API docs:
  33. // https://docs.gitlab.com/ce/api/protected_branches.html#protected-branches-api
  34. type BranchAccessDescription struct {
  35. AccessLevel AccessLevelValue `json:"access_level"`
  36. AccessLevelDescription string `json:"access_level_description"`
  37. }
  38. // ProtectedBranch represents a protected branch.
  39. //
  40. // GitLab API docs:
  41. // https://docs.gitlab.com/ce/api/protected_branches.html#list-protected-branches
  42. type ProtectedBranch struct {
  43. Name string `json:"name"`
  44. PushAccessLevels []*BranchAccessDescription `json:"push_access_levels"`
  45. MergeAccessLevels []*BranchAccessDescription `json:"merge_access_levels"`
  46. }
  47. // ListProtectedBranchesOptions represents the available ListProtectedBranches()
  48. // options.
  49. //
  50. // GitLab API docs:
  51. // https://docs.gitlab.com/ce/api/protected_branches.html#list-protected-branches
  52. type ListProtectedBranchesOptions ListOptions
  53. // ListProtectedBranches gets a list of protected branches from a project.
  54. //
  55. // GitLab API docs:
  56. // https://docs.gitlab.com/ce/api/protected_branches.html#list-protected-branches
  57. func (s *ProtectedBranchesService) ListProtectedBranches(pid interface{}, opt *ListProtectedBranchesOptions, options ...OptionFunc) ([]*ProtectedBranch, *Response, error) {
  58. project, err := parseID(pid)
  59. if err != nil {
  60. return nil, nil, err
  61. }
  62. u := fmt.Sprintf("projects/%s/protected_branches", url.QueryEscape(project))
  63. req, err := s.client.NewRequest("GET", u, opt, options)
  64. if err != nil {
  65. return nil, nil, err
  66. }
  67. var p []*ProtectedBranch
  68. resp, err := s.client.Do(req, &p)
  69. if err != nil {
  70. return nil, resp, err
  71. }
  72. return p, resp, err
  73. }
  74. // GetProtectedBranch gets a single protected branch or wildcard protected branch.
  75. //
  76. // GitLab API docs:
  77. // https://docs.gitlab.com/ce/api/protected_branches.html#get-a-single-protected-branch-or-wildcard-protected-branch
  78. func (s *ProtectedBranchesService) GetProtectedBranch(pid interface{}, branch string, options ...OptionFunc) (*ProtectedBranch, *Response, error) {
  79. project, err := parseID(pid)
  80. if err != nil {
  81. return nil, nil, err
  82. }
  83. u := fmt.Sprintf("projects/%s/protected_branches/%s", url.QueryEscape(project), branch)
  84. req, err := s.client.NewRequest("GET", u, nil, options)
  85. if err != nil {
  86. return nil, nil, err
  87. }
  88. p := new(ProtectedBranch)
  89. resp, err := s.client.Do(req, p)
  90. if err != nil {
  91. return nil, resp, err
  92. }
  93. return p, resp, err
  94. }
  95. // ProtectRepositoryBranchesOptions represents the available
  96. // ProtectRepositoryBranches() options.
  97. //
  98. // GitLab API docs:
  99. // https://docs.gitlab.com/ce/api/protected_branches.html#protect-repository-branches
  100. type ProtectRepositoryBranchesOptions struct {
  101. Name *string `url:"name,omitempty" json:"name,omitempty"`
  102. PushAccessLevel *AccessLevelValue `url:"push_access_level,omitempty" json:"push_access_level,omitempty"`
  103. MergeAccessLevel *AccessLevelValue `url:"merge_access_level,omitempty" json:"merge_access_level,omitempty"`
  104. }
  105. // ProtectRepositoryBranches protects a single repository branch or several
  106. // project repository branches using a wildcard protected branch.
  107. //
  108. // GitLab API docs:
  109. // https://docs.gitlab.com/ce/api/protected_branches.html#protect-repository-branches
  110. func (s *ProtectedBranchesService) ProtectRepositoryBranches(pid interface{}, opt *ProtectRepositoryBranchesOptions, options ...OptionFunc) (*ProtectedBranch, *Response, error) {
  111. project, err := parseID(pid)
  112. if err != nil {
  113. return nil, nil, err
  114. }
  115. u := fmt.Sprintf("projects/%s/protected_branches", url.QueryEscape(project))
  116. req, err := s.client.NewRequest("POST", u, opt, options)
  117. if err != nil {
  118. return nil, nil, err
  119. }
  120. p := new(ProtectedBranch)
  121. resp, err := s.client.Do(req, p)
  122. if err != nil {
  123. return nil, resp, err
  124. }
  125. return p, resp, err
  126. }
  127. // UnprotectRepositoryBranches unprotects the given protected branch or wildcard
  128. // protected branch.
  129. //
  130. // GitLab API docs:
  131. // https://docs.gitlab.com/ce/api/protected_branches.html#unprotect-repository-branches
  132. func (s *ProtectedBranchesService) UnprotectRepositoryBranches(pid interface{}, branch string, options ...OptionFunc) (*Response, error) {
  133. project, err := parseID(pid)
  134. if err != nil {
  135. return nil, err
  136. }
  137. u := fmt.Sprintf("projects/%s/protected_branches/%s", url.QueryEscape(project), branch)
  138. req, err := s.client.NewRequest("DELETE", u, nil, options)
  139. if err != nil {
  140. return nil, err
  141. }
  142. return s.client.Do(req, nil)
  143. }