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 type BranchesService struct {
30 client *Client
31 }
32
33
34
35
36 type Branch struct {
37 Commit *Commit `json:"commit"`
38 Name string `json:"name"`
39 Protected bool `json:"protected"`
40 Merged bool `json:"merged"`
41 Default bool `json:"default"`
42 CanPush bool `json:"can_push"`
43 DevelopersCanPush bool `json:"developers_can_push"`
44 DevelopersCanMerge bool `json:"developers_can_merge"`
45 WebURL string `json:"web_url"`
46 }
47
48 func (b Branch) String() string {
49 return Stringify(b)
50 }
51
52
53
54
55
56 type ListBranchesOptions struct {
57 ListOptions
58 Search *string `url:"search,omitempty" json:"search,omitempty"`
59 Regex *string `url:"regex,omitempty" json:"regex,omitempty"`
60 }
61
62
63
64
65
66
67 func (s *BranchesService) ListBranches(pid interface{}, opts *ListBranchesOptions, options ...RequestOptionFunc) ([]*Branch, *Response, error) {
68 project, err := parseID(pid)
69 if err != nil {
70 return nil, nil, err
71 }
72 u := fmt.Sprintf("projects/%s/repository/branches", PathEscape(project))
73
74 req, err := s.client.NewRequest(http.MethodGet, u, opts, options)
75 if err != nil {
76 return nil, nil, err
77 }
78
79 var b []*Branch
80 resp, err := s.client.Do(req, &b)
81 if err != nil {
82 return nil, resp, err
83 }
84
85 return b, resp, nil
86 }
87
88
89
90
91
92 func (s *BranchesService) GetBranch(pid interface{}, branch string, options ...RequestOptionFunc) (*Branch, *Response, error) {
93 project, err := parseID(pid)
94 if err != nil {
95 return nil, nil, err
96 }
97 u := fmt.Sprintf("projects/%s/repository/branches/%s", PathEscape(project), url.PathEscape(branch))
98
99 req, err := s.client.NewRequest(http.MethodGet, u, nil, options)
100 if err != nil {
101 return nil, nil, err
102 }
103
104 b := new(Branch)
105 resp, err := s.client.Do(req, b)
106 if err != nil {
107 return nil, resp, err
108 }
109
110 return b, resp, nil
111 }
112
113
114
115
116
117 type ProtectBranchOptions struct {
118 DevelopersCanPush *bool `url:"developers_can_push,omitempty" json:"developers_can_push,omitempty"`
119 DevelopersCanMerge *bool `url:"developers_can_merge,omitempty" json:"developers_can_merge,omitempty"`
120 }
121
122
123
124
125
126
127
128
129
130
131 func (s *BranchesService) ProtectBranch(pid interface{}, branch string, opts *ProtectBranchOptions, options ...RequestOptionFunc) (*Branch, *Response, error) {
132 project, err := parseID(pid)
133 if err != nil {
134 return nil, nil, err
135 }
136 u := fmt.Sprintf("projects/%s/repository/branches/%s/protect", PathEscape(project), url.PathEscape(branch))
137
138 req, err := s.client.NewRequest(http.MethodPut, u, opts, options)
139 if err != nil {
140 return nil, nil, err
141 }
142
143 b := new(Branch)
144 resp, err := s.client.Do(req, b)
145 if err != nil {
146 return nil, resp, err
147 }
148
149 return b, resp, nil
150 }
151
152
153
154
155
156
157
158
159
160
161 func (s *BranchesService) UnprotectBranch(pid interface{}, branch string, options ...RequestOptionFunc) (*Branch, *Response, error) {
162 project, err := parseID(pid)
163 if err != nil {
164 return nil, nil, err
165 }
166 u := fmt.Sprintf("projects/%s/repository/branches/%s/unprotect", PathEscape(project), url.PathEscape(branch))
167
168 req, err := s.client.NewRequest(http.MethodPut, u, nil, options)
169 if err != nil {
170 return nil, nil, err
171 }
172
173 b := new(Branch)
174 resp, err := s.client.Do(req, b)
175 if err != nil {
176 return nil, resp, err
177 }
178
179 return b, resp, nil
180 }
181
182
183
184
185
186 type CreateBranchOptions struct {
187 Branch *string `url:"branch,omitempty" json:"branch,omitempty"`
188 Ref *string `url:"ref,omitempty" json:"ref,omitempty"`
189 }
190
191
192
193
194
195 func (s *BranchesService) CreateBranch(pid interface{}, opt *CreateBranchOptions, options ...RequestOptionFunc) (*Branch, *Response, error) {
196 project, err := parseID(pid)
197 if err != nil {
198 return nil, nil, err
199 }
200 u := fmt.Sprintf("projects/%s/repository/branches", PathEscape(project))
201
202 req, err := s.client.NewRequest(http.MethodPost, u, opt, options)
203 if err != nil {
204 return nil, nil, err
205 }
206
207 b := new(Branch)
208 resp, err := s.client.Do(req, b)
209 if err != nil {
210 return nil, resp, err
211 }
212
213 return b, resp, nil
214 }
215
216
217
218
219
220 func (s *BranchesService) DeleteBranch(pid interface{}, branch string, options ...RequestOptionFunc) (*Response, error) {
221 project, err := parseID(pid)
222 if err != nil {
223 return nil, err
224 }
225 u := fmt.Sprintf("projects/%s/repository/branches/%s", PathEscape(project), url.PathEscape(branch))
226
227 req, err := s.client.NewRequest(http.MethodDelete, u, nil, options)
228 if err != nil {
229 return nil, err
230 }
231
232 return s.client.Do(req, nil)
233 }
234
235
236
237
238
239 func (s *BranchesService) DeleteMergedBranches(pid interface{}, options ...RequestOptionFunc) (*Response, error) {
240 project, err := parseID(pid)
241 if err != nil {
242 return nil, err
243 }
244 u := fmt.Sprintf("projects/%s/repository/merged_branches", PathEscape(project))
245
246 req, err := s.client.NewRequest(http.MethodDelete, u, nil, options)
247 if err != nil {
248 return nil, err
249 }
250
251 return s.client.Do(req, nil)
252 }
253
View as plain text