1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package gitlab
18
19 import (
20 "fmt"
21 "net/http"
22 "net/url"
23 )
24
25
26
27
28
29
30 type ProtectedBranchesService struct {
31 client *Client
32 }
33
34
35
36
37
38 type ProtectedBranch struct {
39 ID int `json:"id"`
40 Name string `json:"name"`
41 PushAccessLevels []*BranchAccessDescription `json:"push_access_levels"`
42 MergeAccessLevels []*BranchAccessDescription `json:"merge_access_levels"`
43 UnprotectAccessLevels []*BranchAccessDescription `json:"unprotect_access_levels"`
44 AllowForcePush bool `json:"allow_force_push"`
45 CodeOwnerApprovalRequired bool `json:"code_owner_approval_required"`
46 }
47
48
49
50
51
52
53 type BranchAccessDescription struct {
54 ID int `json:"id"`
55 AccessLevel AccessLevelValue `json:"access_level"`
56 AccessLevelDescription string `json:"access_level_description"`
57 UserID int `json:"user_id"`
58 GroupID int `json:"group_id"`
59 }
60
61
62
63
64
65
66 type ListProtectedBranchesOptions struct {
67 ListOptions
68 Search *string `url:"search,omitempty" json:"search,omitempty"`
69 }
70
71
72
73
74
75 func (s *ProtectedBranchesService) ListProtectedBranches(pid interface{}, opt *ListProtectedBranchesOptions, options ...RequestOptionFunc) ([]*ProtectedBranch, *Response, error) {
76 project, err := parseID(pid)
77 if err != nil {
78 return nil, nil, err
79 }
80 u := fmt.Sprintf("projects/%s/protected_branches", PathEscape(project))
81
82 req, err := s.client.NewRequest(http.MethodGet, u, opt, options)
83 if err != nil {
84 return nil, nil, err
85 }
86
87 var p []*ProtectedBranch
88 resp, err := s.client.Do(req, &p)
89 if err != nil {
90 return nil, resp, err
91 }
92
93 return p, resp, nil
94 }
95
96
97
98
99
100 func (s *ProtectedBranchesService) GetProtectedBranch(pid interface{}, branch string, options ...RequestOptionFunc) (*ProtectedBranch, *Response, error) {
101 project, err := parseID(pid)
102 if err != nil {
103 return nil, nil, err
104 }
105 u := fmt.Sprintf("projects/%s/protected_branches/%s", PathEscape(project), url.PathEscape(branch))
106
107 req, err := s.client.NewRequest(http.MethodGet, u, nil, options)
108 if err != nil {
109 return nil, nil, err
110 }
111
112 p := new(ProtectedBranch)
113 resp, err := s.client.Do(req, p)
114 if err != nil {
115 return nil, resp, err
116 }
117
118 return p, resp, nil
119 }
120
121
122
123
124
125
126 type ProtectRepositoryBranchesOptions struct {
127 Name *string `url:"name,omitempty" json:"name,omitempty"`
128 PushAccessLevel *AccessLevelValue `url:"push_access_level,omitempty" json:"push_access_level,omitempty"`
129 MergeAccessLevel *AccessLevelValue `url:"merge_access_level,omitempty" json:"merge_access_level,omitempty"`
130 UnprotectAccessLevel *AccessLevelValue `url:"unprotect_access_level,omitempty" json:"unprotect_access_level,omitempty"`
131 AllowForcePush *bool `url:"allow_force_push,omitempty" json:"allow_force_push,omitempty"`
132 AllowedToPush *[]*BranchPermissionOptions `url:"allowed_to_push,omitempty" json:"allowed_to_push,omitempty"`
133 AllowedToMerge *[]*BranchPermissionOptions `url:"allowed_to_merge,omitempty" json:"allowed_to_merge,omitempty"`
134 AllowedToUnprotect *[]*BranchPermissionOptions `url:"allowed_to_unprotect,omitempty" json:"allowed_to_unprotect,omitempty"`
135 CodeOwnerApprovalRequired *bool `url:"code_owner_approval_required,omitempty" json:"code_owner_approval_required,omitempty"`
136 }
137
138
139
140
141
142 type BranchPermissionOptions struct {
143 ID *int `url:"id,omitempty" json:"id,omitempty"`
144 UserID *int `url:"user_id,omitempty" json:"user_id,omitempty"`
145 GroupID *int `url:"group_id,omitempty" json:"group_id,omitempty"`
146 DeployKeyID *int `url:"deploy_key_id,omitempty" json:"deploy_key_id,omitempty"`
147 AccessLevel *AccessLevelValue `url:"access_level,omitempty" json:"access_level,omitempty"`
148 Destroy *bool `url:"_destroy,omitempty" json:"_destroy,omitempty"`
149 }
150
151
152
153
154
155
156 func (s *ProtectedBranchesService) ProtectRepositoryBranches(pid interface{}, opt *ProtectRepositoryBranchesOptions, options ...RequestOptionFunc) (*ProtectedBranch, *Response, error) {
157 project, err := parseID(pid)
158 if err != nil {
159 return nil, nil, err
160 }
161 u := fmt.Sprintf("projects/%s/protected_branches", PathEscape(project))
162
163 req, err := s.client.NewRequest(http.MethodPost, u, opt, options)
164 if err != nil {
165 return nil, nil, err
166 }
167
168 p := new(ProtectedBranch)
169 resp, err := s.client.Do(req, p)
170 if err != nil {
171 return nil, resp, err
172 }
173
174 return p, resp, nil
175 }
176
177
178
179
180
181
182 func (s *ProtectedBranchesService) UnprotectRepositoryBranches(pid interface{}, branch string, options ...RequestOptionFunc) (*Response, error) {
183 project, err := parseID(pid)
184 if err != nil {
185 return nil, err
186 }
187 u := fmt.Sprintf("projects/%s/protected_branches/%s", PathEscape(project), url.PathEscape(branch))
188
189 req, err := s.client.NewRequest(http.MethodDelete, u, nil, options)
190 if err != nil {
191 return nil, err
192 }
193
194 return s.client.Do(req, nil)
195 }
196
197
198
199
200
201
202 type UpdateProtectedBranchOptions struct {
203 Name *string `url:"name,omitempty" json:"name,omitempty"`
204 AllowForcePush *bool `url:"allow_force_push,omitempty" json:"allow_force_push,omitempty"`
205 CodeOwnerApprovalRequired *bool `url:"code_owner_approval_required,omitempty" json:"code_owner_approval_required,omitempty"`
206 AllowedToPush *[]*BranchPermissionOptions `url:"allowed_to_push,omitempty" json:"allowed_to_push,omitempty"`
207 AllowedToMerge *[]*BranchPermissionOptions `url:"allowed_to_merge,omitempty" json:"allowed_to_merge,omitempty"`
208 AllowedToUnprotect *[]*BranchPermissionOptions `url:"allowed_to_unprotect,omitempty" json:"allowed_to_unprotect,omitempty"`
209 }
210
211
212
213
214
215 func (s *ProtectedBranchesService) UpdateProtectedBranch(pid interface{}, branch string, opt *UpdateProtectedBranchOptions, options ...RequestOptionFunc) (*ProtectedBranch, *Response, error) {
216 project, err := parseID(pid)
217 if err != nil {
218 return nil, nil, err
219 }
220 u := fmt.Sprintf("projects/%s/protected_branches/%s", PathEscape(project), url.PathEscape(branch))
221
222 req, err := s.client.NewRequest(http.MethodPatch, u, opt, options)
223 if err != nil {
224 return nil, nil, err
225 }
226
227 p := new(ProtectedBranch)
228 resp, err := s.client.Do(req, p)
229 if err != nil {
230 return nil, resp, err
231 }
232
233 return p, resp, nil
234 }
235
236
237
238
239
240
241 type RequireCodeOwnerApprovalsOptions struct {
242 CodeOwnerApprovalRequired *bool `url:"code_owner_approval_required,omitempty" json:"code_owner_approval_required,omitempty"`
243 }
244
245
246
247
248
249
250
251 func (s *ProtectedBranchesService) RequireCodeOwnerApprovals(pid interface{}, branch string, opt *RequireCodeOwnerApprovalsOptions, options ...RequestOptionFunc) (*Response, error) {
252 updateOptions := &UpdateProtectedBranchOptions{
253 CodeOwnerApprovalRequired: opt.CodeOwnerApprovalRequired,
254 }
255 _, req, err := s.UpdateProtectedBranch(pid, branch, updateOptions, options...)
256 return req, err
257 }
258
View as plain text