1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package gitlab
18
19 import (
20 "bytes"
21 "fmt"
22 "io"
23 "net/http"
24 "net/url"
25 )
26
27
28
29
30
31 type RepositoriesService struct {
32 client *Client
33 }
34
35
36
37
38 type TreeNode struct {
39 ID string `json:"id"`
40 Name string `json:"name"`
41 Type string `json:"type"`
42 Path string `json:"path"`
43 Mode string `json:"mode"`
44 }
45
46 func (t TreeNode) String() string {
47 return Stringify(t)
48 }
49
50
51
52
53
54 type ListTreeOptions struct {
55 ListOptions
56 Path *string `url:"path,omitempty" json:"path,omitempty"`
57 Ref *string `url:"ref,omitempty" json:"ref,omitempty"`
58 Recursive *bool `url:"recursive,omitempty" json:"recursive,omitempty"`
59 }
60
61
62
63
64
65 func (s *RepositoriesService) ListTree(pid interface{}, opt *ListTreeOptions, options ...RequestOptionFunc) ([]*TreeNode, *Response, error) {
66 project, err := parseID(pid)
67 if err != nil {
68 return nil, nil, err
69 }
70 u := fmt.Sprintf("projects/%s/repository/tree", PathEscape(project))
71
72 req, err := s.client.NewRequest(http.MethodGet, u, opt, options)
73 if err != nil {
74 return nil, nil, err
75 }
76
77 var t []*TreeNode
78 resp, err := s.client.Do(req, &t)
79 if err != nil {
80 return nil, resp, err
81 }
82
83 return t, resp, nil
84 }
85
86
87
88
89
90
91 func (s *RepositoriesService) Blob(pid interface{}, sha string, options ...RequestOptionFunc) ([]byte, *Response, error) {
92 project, err := parseID(pid)
93 if err != nil {
94 return nil, nil, err
95 }
96 u := fmt.Sprintf("projects/%s/repository/blobs/%s", PathEscape(project), url.PathEscape(sha))
97
98 req, err := s.client.NewRequest(http.MethodGet, u, nil, options)
99 if err != nil {
100 return nil, nil, err
101 }
102
103 var b bytes.Buffer
104 resp, err := s.client.Do(req, &b)
105 if err != nil {
106 return nil, resp, err
107 }
108
109 return b.Bytes(), resp, err
110 }
111
112
113
114
115
116 func (s *RepositoriesService) RawBlobContent(pid interface{}, sha string, options ...RequestOptionFunc) ([]byte, *Response, error) {
117 project, err := parseID(pid)
118 if err != nil {
119 return nil, nil, err
120 }
121 u := fmt.Sprintf("projects/%s/repository/blobs/%s/raw", PathEscape(project), url.PathEscape(sha))
122
123 req, err := s.client.NewRequest(http.MethodGet, u, nil, options)
124 if err != nil {
125 return nil, nil, err
126 }
127
128 var b bytes.Buffer
129 resp, err := s.client.Do(req, &b)
130 if err != nil {
131 return nil, resp, err
132 }
133
134 return b.Bytes(), resp, err
135 }
136
137
138
139
140
141 type ArchiveOptions struct {
142 Format *string `url:"-" json:"-"`
143 Path *string `url:"path,omitempty" json:"path,omitempty"`
144 SHA *string `url:"sha,omitempty" json:"sha,omitempty"`
145 }
146
147
148
149
150
151 func (s *RepositoriesService) Archive(pid interface{}, opt *ArchiveOptions, options ...RequestOptionFunc) ([]byte, *Response, error) {
152 project, err := parseID(pid)
153 if err != nil {
154 return nil, nil, err
155 }
156 u := fmt.Sprintf("projects/%s/repository/archive", PathEscape(project))
157
158
159 if opt != nil && opt.Format != nil {
160 u = fmt.Sprintf("%s.%s", u, *opt.Format)
161 }
162
163 req, err := s.client.NewRequest(http.MethodGet, u, opt, options)
164 if err != nil {
165 return nil, nil, err
166 }
167
168 var b bytes.Buffer
169 resp, err := s.client.Do(req, &b)
170 if err != nil {
171 return nil, resp, err
172 }
173
174 return b.Bytes(), resp, err
175 }
176
177
178
179
180
181
182 func (s *RepositoriesService) StreamArchive(pid interface{}, w io.Writer, opt *ArchiveOptions, options ...RequestOptionFunc) (*Response, error) {
183 project, err := parseID(pid)
184 if err != nil {
185 return nil, err
186 }
187 u := fmt.Sprintf("projects/%s/repository/archive", PathEscape(project))
188
189
190 if opt != nil && opt.Format != nil {
191 u = fmt.Sprintf("%s.%s", u, *opt.Format)
192 }
193
194 req, err := s.client.NewRequest(http.MethodGet, u, opt, options)
195 if err != nil {
196 return nil, err
197 }
198
199 return s.client.Do(req, w)
200 }
201
202
203
204
205
206 type Compare struct {
207 Commit *Commit `json:"commit"`
208 Commits []*Commit `json:"commits"`
209 Diffs []*Diff `json:"diffs"`
210 CompareTimeout bool `json:"compare_timeout"`
211 CompareSameRef bool `json:"compare_same_ref"`
212 }
213
214 func (c Compare) String() string {
215 return Stringify(c)
216 }
217
218
219
220
221
222 type CompareOptions struct {
223 From *string `url:"from,omitempty" json:"from,omitempty"`
224 To *string `url:"to,omitempty" json:"to,omitempty"`
225 Straight *bool `url:"straight,omitempty" json:"straight,omitempty"`
226 Unidiff *bool `url:"unidiff,omitempty" json:"unidiff,omitempty"`
227 }
228
229
230
231
232
233 func (s *RepositoriesService) Compare(pid interface{}, opt *CompareOptions, options ...RequestOptionFunc) (*Compare, *Response, error) {
234 project, err := parseID(pid)
235 if err != nil {
236 return nil, nil, err
237 }
238 u := fmt.Sprintf("projects/%s/repository/compare", PathEscape(project))
239
240 req, err := s.client.NewRequest(http.MethodGet, u, opt, options)
241 if err != nil {
242 return nil, nil, err
243 }
244
245 c := new(Compare)
246 resp, err := s.client.Do(req, c)
247 if err != nil {
248 return nil, resp, err
249 }
250
251 return c, resp, nil
252 }
253
254
255
256
257 type Contributor struct {
258 Name string `json:"name"`
259 Email string `json:"email"`
260 Commits int `json:"commits"`
261 Additions int `json:"additions"`
262 Deletions int `json:"deletions"`
263 }
264
265 func (c Contributor) String() string {
266 return Stringify(c)
267 }
268
269
270
271
272 type ListContributorsOptions struct {
273 ListOptions
274 OrderBy *string `url:"order_by,omitempty" json:"order_by,omitempty"`
275 Sort *string `url:"sort,omitempty" json:"sort,omitempty"`
276 }
277
278
279
280
281 func (s *RepositoriesService) Contributors(pid interface{}, opt *ListContributorsOptions, options ...RequestOptionFunc) ([]*Contributor, *Response, error) {
282 project, err := parseID(pid)
283 if err != nil {
284 return nil, nil, err
285 }
286 u := fmt.Sprintf("projects/%s/repository/contributors", PathEscape(project))
287
288 req, err := s.client.NewRequest(http.MethodGet, u, opt, options)
289 if err != nil {
290 return nil, nil, err
291 }
292
293 var c []*Contributor
294 resp, err := s.client.Do(req, &c)
295 if err != nil {
296 return nil, resp, err
297 }
298
299 return c, resp, nil
300 }
301
302
303
304
305
306 type MergeBaseOptions struct {
307 Ref *[]string `url:"refs[],omitempty" json:"refs,omitempty"`
308 }
309
310
311
312
313
314
315 func (s *RepositoriesService) MergeBase(pid interface{}, opt *MergeBaseOptions, options ...RequestOptionFunc) (*Commit, *Response, error) {
316 project, err := parseID(pid)
317 if err != nil {
318 return nil, nil, err
319 }
320 u := fmt.Sprintf("projects/%s/repository/merge_base", PathEscape(project))
321
322 req, err := s.client.NewRequest(http.MethodGet, u, opt, options)
323 if err != nil {
324 return nil, nil, err
325 }
326
327 c := new(Commit)
328 resp, err := s.client.Do(req, c)
329 if err != nil {
330 return nil, resp, err
331 }
332
333 return c, resp, nil
334 }
335
336
337
338
339
340 type AddChangelogOptions struct {
341 Version *string `url:"version,omitempty" json:"version,omitempty"`
342 Branch *string `url:"branch,omitempty" json:"branch,omitempty"`
343 ConfigFile *string `url:"config_file,omitempty" json:"config_file,omitempty"`
344 Date *ISOTime `url:"date,omitempty" json:"date,omitempty"`
345 File *string `url:"file,omitempty" json:"file,omitempty"`
346 From *string `url:"from,omitempty" json:"from,omitempty"`
347 Message *string `url:"message,omitempty" json:"message,omitempty"`
348 To *string `url:"to,omitempty" json:"to,omitempty"`
349 Trailer *string `url:"trailer,omitempty" json:"trailer,omitempty"`
350 }
351
352
353
354
355
356 func (s *RepositoriesService) AddChangelog(pid interface{}, opt *AddChangelogOptions, options ...RequestOptionFunc) (*Response, error) {
357 project, err := parseID(pid)
358 if err != nil {
359 return nil, err
360 }
361 u := fmt.Sprintf("projects/%s/repository/changelog", PathEscape(project))
362
363 req, err := s.client.NewRequest(http.MethodPost, u, opt, options)
364 if err != nil {
365 return nil, err
366 }
367
368 return s.client.Do(req, nil)
369 }
370
371
372
373
374
375 type ChangelogData struct {
376 Notes string `json:"notes"`
377 }
378
379 func (c ChangelogData) String() string {
380 return Stringify(c)
381 }
382
383
384
385
386
387
388 type GenerateChangelogDataOptions struct {
389 Version *string `url:"version,omitempty" json:"version,omitempty"`
390 ConfigFile *string `url:"config_file,omitempty" json:"config_file,omitempty"`
391 Date *ISOTime `url:"date,omitempty" json:"date,omitempty"`
392 From *string `url:"from,omitempty" json:"from,omitempty"`
393 To *string `url:"to,omitempty" json:"to,omitempty"`
394 Trailer *string `url:"trailer,omitempty" json:"trailer,omitempty"`
395 }
396
397
398
399
400
401
402 func (s *RepositoriesService) GenerateChangelogData(pid interface{}, opt GenerateChangelogDataOptions, options ...RequestOptionFunc) (*ChangelogData, *Response, error) {
403 project, err := parseID(pid)
404 if err != nil {
405 return nil, nil, err
406 }
407 u := fmt.Sprintf("projects/%s/repository/changelog", project)
408
409 req, err := s.client.NewRequest(http.MethodGet, u, opt, options)
410 if err != nil {
411 return nil, nil, err
412 }
413
414 cd := new(ChangelogData)
415 resp, err := s.client.Do(req, cd)
416 if err != nil {
417 return nil, resp, err
418 }
419
420 return cd, resp, nil
421 }
422
View as plain text