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 "time"
24 )
25
26
27
28
29
30 type CommitsService struct {
31 client *Client
32 }
33
34
35
36
37 type Commit struct {
38 ID string `json:"id"`
39 ShortID string `json:"short_id"`
40 Title string `json:"title"`
41 AuthorName string `json:"author_name"`
42 AuthorEmail string `json:"author_email"`
43 AuthoredDate *time.Time `json:"authored_date"`
44 CommitterName string `json:"committer_name"`
45 CommitterEmail string `json:"committer_email"`
46 CommittedDate *time.Time `json:"committed_date"`
47 CreatedAt *time.Time `json:"created_at"`
48 Message string `json:"message"`
49 ParentIDs []string `json:"parent_ids"`
50 Stats *CommitStats `json:"stats"`
51 Status *BuildStateValue `json:"status"`
52 LastPipeline *PipelineInfo `json:"last_pipeline"`
53 ProjectID int `json:"project_id"`
54 Trailers map[string]string `json:"trailers"`
55 WebURL string `json:"web_url"`
56 }
57
58
59
60
61 type CommitStats struct {
62 Additions int `json:"additions"`
63 Deletions int `json:"deletions"`
64 Total int `json:"total"`
65 }
66
67 func (c Commit) String() string {
68 return Stringify(c)
69 }
70
71
72
73
74 type ListCommitsOptions struct {
75 ListOptions
76 RefName *string `url:"ref_name,omitempty" json:"ref_name,omitempty"`
77 Since *time.Time `url:"since,omitempty" json:"since,omitempty"`
78 Until *time.Time `url:"until,omitempty" json:"until,omitempty"`
79 Path *string `url:"path,omitempty" json:"path,omitempty"`
80 Author *string `url:"author,omitempty" json:"author,omitempty"`
81 All *bool `url:"all,omitempty" json:"all,omitempty"`
82 WithStats *bool `url:"with_stats,omitempty" json:"with_stats,omitempty"`
83 FirstParent *bool `url:"first_parent,omitempty" json:"first_parent,omitempty"`
84 Trailers *bool `url:"trailers,omitempty" json:"trailers,omitempty"`
85 }
86
87
88
89
90 func (s *CommitsService) ListCommits(pid interface{}, opt *ListCommitsOptions, options ...RequestOptionFunc) ([]*Commit, *Response, error) {
91 project, err := parseID(pid)
92 if err != nil {
93 return nil, nil, err
94 }
95 u := fmt.Sprintf("projects/%s/repository/commits", PathEscape(project))
96
97 req, err := s.client.NewRequest(http.MethodGet, u, opt, options)
98 if err != nil {
99 return nil, nil, err
100 }
101
102 var c []*Commit
103 resp, err := s.client.Do(req, &c)
104 if err != nil {
105 return nil, resp, err
106 }
107
108 return c, resp, nil
109 }
110
111
112
113
114
115 type CommitRef struct {
116 Type string `json:"type"`
117 Name string `json:"name"`
118 }
119
120
121
122
123
124 type GetCommitRefsOptions struct {
125 ListOptions
126 Type *string `url:"type,omitempty" json:"type,omitempty"`
127 }
128
129
130
131
132
133 func (s *CommitsService) GetCommitRefs(pid interface{}, sha string, opt *GetCommitRefsOptions, options ...RequestOptionFunc) ([]*CommitRef, *Response, error) {
134 project, err := parseID(pid)
135 if err != nil {
136 return nil, nil, err
137 }
138 u := fmt.Sprintf("projects/%s/repository/commits/%s/refs", PathEscape(project), url.PathEscape(sha))
139
140 req, err := s.client.NewRequest(http.MethodGet, u, opt, options)
141 if err != nil {
142 return nil, nil, err
143 }
144
145 var cs []*CommitRef
146 resp, err := s.client.Do(req, &cs)
147 if err != nil {
148 return nil, resp, err
149 }
150
151 return cs, resp, nil
152 }
153
154
155
156
157
158 func (s *CommitsService) GetCommit(pid interface{}, sha string, options ...RequestOptionFunc) (*Commit, *Response, error) {
159 project, err := parseID(pid)
160 if err != nil {
161 return nil, nil, err
162 }
163 if sha == "" {
164 return nil, nil, fmt.Errorf("SHA must be a non-empty string")
165 }
166 u := fmt.Sprintf("projects/%s/repository/commits/%s", PathEscape(project), url.PathEscape(sha))
167
168 req, err := s.client.NewRequest(http.MethodGet, u, nil, options)
169 if err != nil {
170 return nil, nil, err
171 }
172
173 c := new(Commit)
174 resp, err := s.client.Do(req, c)
175 if err != nil {
176 return nil, resp, err
177 }
178
179 return c, resp, nil
180 }
181
182
183
184
185 type CreateCommitOptions struct {
186 Branch *string `url:"branch,omitempty" json:"branch,omitempty"`
187 CommitMessage *string `url:"commit_message,omitempty" json:"commit_message,omitempty"`
188 StartBranch *string `url:"start_branch,omitempty" json:"start_branch,omitempty"`
189 StartSHA *string `url:"start_sha,omitempty" json:"start_sha,omitempty"`
190 StartProject *string `url:"start_project,omitempty" json:"start_project,omitempty"`
191 Actions []*CommitActionOptions `url:"actions" json:"actions"`
192 AuthorEmail *string `url:"author_email,omitempty" json:"author_email,omitempty"`
193 AuthorName *string `url:"author_name,omitempty" json:"author_name,omitempty"`
194 Stats *bool `url:"stats,omitempty" json:"stats,omitempty"`
195 Force *bool `url:"force,omitempty" json:"force,omitempty"`
196 }
197
198
199
200
201
202 type CommitActionOptions struct {
203 Action *FileActionValue `url:"action,omitempty" json:"action,omitempty"`
204 FilePath *string `url:"file_path,omitempty" json:"file_path,omitempty"`
205 PreviousPath *string `url:"previous_path,omitempty" json:"previous_path,omitempty"`
206 Content *string `url:"content,omitempty" json:"content,omitempty"`
207 Encoding *string `url:"encoding,omitempty" json:"encoding,omitempty"`
208 LastCommitID *string `url:"last_commit_id,omitempty" json:"last_commit_id,omitempty"`
209 ExecuteFilemode *bool `url:"execute_filemode,omitempty" json:"execute_filemode,omitempty"`
210 }
211
212
213
214
215 func (s *CommitsService) CreateCommit(pid interface{}, opt *CreateCommitOptions, options ...RequestOptionFunc) (*Commit, *Response, error) {
216 project, err := parseID(pid)
217 if err != nil {
218 return nil, nil, err
219 }
220 u := fmt.Sprintf("projects/%s/repository/commits", PathEscape(project))
221
222 req, err := s.client.NewRequest(http.MethodPost, u, opt, options)
223 if err != nil {
224 return nil, nil, err
225 }
226
227 c := new(Commit)
228 resp, err := s.client.Do(req, &c)
229 if err != nil {
230 return nil, resp, err
231 }
232
233 return c, resp, nil
234 }
235
236
237
238
239 type Diff struct {
240 Diff string `json:"diff"`
241 NewPath string `json:"new_path"`
242 OldPath string `json:"old_path"`
243 AMode string `json:"a_mode"`
244 BMode string `json:"b_mode"`
245 NewFile bool `json:"new_file"`
246 RenamedFile bool `json:"renamed_file"`
247 DeletedFile bool `json:"deleted_file"`
248 }
249
250 func (d Diff) String() string {
251 return Stringify(d)
252 }
253
254
255
256
257
258 type GetCommitDiffOptions struct {
259 ListOptions
260 Unidiff *bool `url:"unidiff,omitempty" json:"unidiff,omitempty"`
261 }
262
263
264
265
266
267 func (s *CommitsService) GetCommitDiff(pid interface{}, sha string, opt *GetCommitDiffOptions, options ...RequestOptionFunc) ([]*Diff, *Response, error) {
268 project, err := parseID(pid)
269 if err != nil {
270 return nil, nil, err
271 }
272 u := fmt.Sprintf("projects/%s/repository/commits/%s/diff", PathEscape(project), url.PathEscape(sha))
273
274 req, err := s.client.NewRequest(http.MethodGet, u, opt, options)
275 if err != nil {
276 return nil, nil, err
277 }
278
279 var d []*Diff
280 resp, err := s.client.Do(req, &d)
281 if err != nil {
282 return nil, resp, err
283 }
284
285 return d, resp, nil
286 }
287
288
289
290
291 type CommitComment struct {
292 Note string `json:"note"`
293 Path string `json:"path"`
294 Line int `json:"line"`
295 LineType string `json:"line_type"`
296 Author Author `json:"author"`
297 }
298
299
300 type Author struct {
301 ID int `json:"id"`
302 Username string `json:"username"`
303 Email string `json:"email"`
304 Name string `json:"name"`
305 State string `json:"state"`
306 Blocked bool `json:"blocked"`
307 CreatedAt *time.Time `json:"created_at"`
308 }
309
310 func (c CommitComment) String() string {
311 return Stringify(c)
312 }
313
314
315
316
317
318 type GetCommitCommentsOptions ListOptions
319
320
321
322
323
324 func (s *CommitsService) GetCommitComments(pid interface{}, sha string, opt *GetCommitCommentsOptions, options ...RequestOptionFunc) ([]*CommitComment, *Response, error) {
325 project, err := parseID(pid)
326 if err != nil {
327 return nil, nil, err
328 }
329 u := fmt.Sprintf("projects/%s/repository/commits/%s/comments", PathEscape(project), url.PathEscape(sha))
330
331 req, err := s.client.NewRequest(http.MethodGet, u, opt, options)
332 if err != nil {
333 return nil, nil, err
334 }
335
336 var c []*CommitComment
337 resp, err := s.client.Do(req, &c)
338 if err != nil {
339 return nil, resp, err
340 }
341
342 return c, resp, nil
343 }
344
345
346
347
348
349
350 type PostCommitCommentOptions struct {
351 Note *string `url:"note,omitempty" json:"note,omitempty"`
352 Path *string `url:"path" json:"path"`
353 Line *int `url:"line" json:"line"`
354 LineType *string `url:"line_type" json:"line_type"`
355 }
356
357
358
359
360
361
362
363 func (s *CommitsService) PostCommitComment(pid interface{}, sha string, opt *PostCommitCommentOptions, options ...RequestOptionFunc) (*CommitComment, *Response, error) {
364 project, err := parseID(pid)
365 if err != nil {
366 return nil, nil, err
367 }
368 u := fmt.Sprintf("projects/%s/repository/commits/%s/comments", PathEscape(project), url.PathEscape(sha))
369
370 req, err := s.client.NewRequest(http.MethodPost, u, opt, options)
371 if err != nil {
372 return nil, nil, err
373 }
374
375 c := new(CommitComment)
376 resp, err := s.client.Do(req, c)
377 if err != nil {
378 return nil, resp, err
379 }
380
381 return c, resp, nil
382 }
383
384
385
386
387 type GetCommitStatusesOptions struct {
388 ListOptions
389 Ref *string `url:"ref,omitempty" json:"ref,omitempty"`
390 Stage *string `url:"stage,omitempty" json:"stage,omitempty"`
391 Name *string `url:"name,omitempty" json:"name,omitempty"`
392 All *bool `url:"all,omitempty" json:"all,omitempty"`
393 }
394
395
396
397
398 type CommitStatus struct {
399 ID int `json:"id"`
400 SHA string `json:"sha"`
401 Ref string `json:"ref"`
402 Status string `json:"status"`
403 CreatedAt *time.Time `json:"created_at"`
404 StartedAt *time.Time `json:"started_at"`
405 FinishedAt *time.Time `json:"finished_at"`
406 Name string `json:"name"`
407 AllowFailure bool `json:"allow_failure"`
408 Coverage float64 `json:"coverage"`
409 PipelineId int `json:"pipeline_id"`
410 Author Author `json:"author"`
411 Description string `json:"description"`
412 TargetURL string `json:"target_url"`
413 }
414
415
416
417
418 func (s *CommitsService) GetCommitStatuses(pid interface{}, sha string, opt *GetCommitStatusesOptions, options ...RequestOptionFunc) ([]*CommitStatus, *Response, error) {
419 project, err := parseID(pid)
420 if err != nil {
421 return nil, nil, err
422 }
423 u := fmt.Sprintf("projects/%s/repository/commits/%s/statuses", PathEscape(project), url.PathEscape(sha))
424
425 req, err := s.client.NewRequest(http.MethodGet, u, opt, options)
426 if err != nil {
427 return nil, nil, err
428 }
429
430 var cs []*CommitStatus
431 resp, err := s.client.Do(req, &cs)
432 if err != nil {
433 return nil, resp, err
434 }
435
436 return cs, resp, nil
437 }
438
439
440
441
442 type SetCommitStatusOptions struct {
443 State BuildStateValue `url:"state" json:"state"`
444 Ref *string `url:"ref,omitempty" json:"ref,omitempty"`
445 Name *string `url:"name,omitempty" json:"name,omitempty"`
446 Context *string `url:"context,omitempty" json:"context,omitempty"`
447 TargetURL *string `url:"target_url,omitempty" json:"target_url,omitempty"`
448 Description *string `url:"description,omitempty" json:"description,omitempty"`
449 Coverage *float64 `url:"coverage,omitempty" json:"coverage,omitempty"`
450 PipelineID *int `url:"pipeline_id,omitempty" json:"pipeline_id,omitempty"`
451 }
452
453
454
455
456 func (s *CommitsService) SetCommitStatus(pid interface{}, sha string, opt *SetCommitStatusOptions, options ...RequestOptionFunc) (*CommitStatus, *Response, error) {
457 project, err := parseID(pid)
458 if err != nil {
459 return nil, nil, err
460 }
461 u := fmt.Sprintf("projects/%s/statuses/%s", PathEscape(project), url.PathEscape(sha))
462
463 req, err := s.client.NewRequest(http.MethodPost, u, opt, options)
464 if err != nil {
465 return nil, nil, err
466 }
467
468 cs := new(CommitStatus)
469 resp, err := s.client.Do(req, &cs)
470 if err != nil {
471 return nil, resp, err
472 }
473
474 return cs, resp, nil
475 }
476
477
478
479
480
481 func (s *CommitsService) ListMergeRequestsByCommit(pid interface{}, sha string, options ...RequestOptionFunc) ([]*MergeRequest, *Response, error) {
482 project, err := parseID(pid)
483 if err != nil {
484 return nil, nil, err
485 }
486 u := fmt.Sprintf("projects/%s/repository/commits/%s/merge_requests", PathEscape(project), url.PathEscape(sha))
487
488 req, err := s.client.NewRequest(http.MethodGet, u, nil, options)
489 if err != nil {
490 return nil, nil, err
491 }
492
493 var mrs []*MergeRequest
494 resp, err := s.client.Do(req, &mrs)
495 if err != nil {
496 return nil, resp, err
497 }
498
499 return mrs, resp, nil
500 }
501
502
503
504
505 type CherryPickCommitOptions struct {
506 Branch *string `url:"branch,omitempty" json:"branch,omitempty"`
507 DryRun *bool `url:"dry_run,omitempty" json:"dry_run,omitempty"`
508 Message *string `url:"message,omitempty" json:"message,omitempty"`
509 }
510
511
512
513
514 func (s *CommitsService) CherryPickCommit(pid interface{}, sha string, opt *CherryPickCommitOptions, options ...RequestOptionFunc) (*Commit, *Response, error) {
515 project, err := parseID(pid)
516 if err != nil {
517 return nil, nil, err
518 }
519 u := fmt.Sprintf("projects/%s/repository/commits/%s/cherry_pick", PathEscape(project), url.PathEscape(sha))
520
521 req, err := s.client.NewRequest(http.MethodPost, u, opt, options)
522 if err != nil {
523 return nil, nil, err
524 }
525
526 c := new(Commit)
527 resp, err := s.client.Do(req, &c)
528 if err != nil {
529 return nil, resp, err
530 }
531
532 return c, resp, nil
533 }
534
535
536
537
538 type RevertCommitOptions struct {
539 Branch *string `url:"branch,omitempty" json:"branch,omitempty"`
540 }
541
542
543
544
545 func (s *CommitsService) RevertCommit(pid interface{}, sha string, opt *RevertCommitOptions, options ...RequestOptionFunc) (*Commit, *Response, error) {
546 project, err := parseID(pid)
547 if err != nil {
548 return nil, nil, err
549 }
550 u := fmt.Sprintf("projects/%s/repository/commits/%s/revert", PathEscape(project), url.PathEscape(sha))
551
552 req, err := s.client.NewRequest(http.MethodPost, u, opt, options)
553 if err != nil {
554 return nil, nil, err
555 }
556
557 c := new(Commit)
558 resp, err := s.client.Do(req, &c)
559 if err != nil {
560 return nil, resp, err
561 }
562
563 return c, resp, nil
564 }
565
566
567
568
569
570 type GPGSignature struct {
571 KeyID int `json:"gpg_key_id"`
572 KeyPrimaryKeyID string `json:"gpg_key_primary_keyid"`
573 KeyUserName string `json:"gpg_key_user_name"`
574 KeyUserEmail string `json:"gpg_key_user_email"`
575 VerificationStatus string `json:"verification_status"`
576 KeySubkeyID int `json:"gpg_key_subkey_id"`
577 }
578
579
580
581
582 func (s *CommitsService) GetGPGSignature(pid interface{}, sha string, options ...RequestOptionFunc) (*GPGSignature, *Response, error) {
583 project, err := parseID(pid)
584 if err != nil {
585 return nil, nil, err
586 }
587 u := fmt.Sprintf("projects/%s/repository/commits/%s/signature", PathEscape(project), url.PathEscape(sha))
588
589 req, err := s.client.NewRequest(http.MethodGet, u, nil, options)
590 if err != nil {
591 return nil, nil, err
592 }
593
594 sig := new(GPGSignature)
595 resp, err := s.client.Do(req, &sig)
596 if err != nil {
597 return nil, resp, err
598 }
599
600 return sig, resp, nil
601 }
602
View as plain text