This repository was archived by the owner on Dec 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 953
/
Copy pathmerge_trains.go
170 lines (148 loc) · 5.49 KB
/
merge_trains.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package gitlab
import (
"fmt"
"net/http"
"time"
)
// MergeTrainsService handles communication with the merge trains related
// methods of the GitLab API.
//
// GitLab API docs: https://docs.gitlab.com/ee/api/merge_trains.html
type MergeTrainsService struct {
client *Client
}
// MergeTrain represents a Gitlab merge train.
//
// GitLab API docs: https://docs.gitlab.com/ee/api/merge_trains.html
type MergeTrain struct {
ID int `json:"id"`
MergeRequest *MergeTrainMergeRequest `json:"merge_request"`
User *BasicUser `json:"user"`
Pipeline *Pipeline `json:"pipeline"`
CreatedAt *time.Time `json:"created_at"`
UpdatedAt *time.Time `json:"updated_at"`
TargetBranch string `json:"target_branch"`
Status string `json:"status"`
MergedAt *time.Time `json:"merged_at"`
Duration int `json:"duration"`
}
// MergeTrainMergeRequest represents a Gitlab merge request inside merge train.
//
// GitLab API docs: https://docs.gitlab.com/ee/api/merge_trains.html
type MergeTrainMergeRequest struct {
ID int `json:"id"`
IID int `json:"iid"`
ProjectID int `json:"project_id"`
Title string `json:"title"`
Description string `json:"description"`
State string `json:"state"`
CreatedAt *time.Time `json:"created_at"`
UpdatedAt *time.Time `json:"updated_at"`
WebURL string `json:"web_url"`
}
// ListMergeTrainsOptions represents the available ListMergeTrain() options.
//
// Gitab API docs:
// https://docs.gitlab.com/ee/api/merge_trains.html#list-merge-trains-for-a-project
type ListMergeTrainsOptions struct {
ListOptions
Scope *string `url:"scope,omitempty" json:"scope,omitempty"`
Sort *string `url:"sort,omitempty" json:"sort,omitempty"`
}
// ListProjectMergeTrains get a list of merge trains in a project.
//
// GitLab API docs:
// https://docs.gitlab.com/ee/api/merge_trains.html#list-merge-trains-for-a-project
func (s *MergeTrainsService) ListProjectMergeTrains(pid interface{}, opt *ListMergeTrainsOptions, options ...RequestOptionFunc) ([]*MergeTrain, *Response, error) {
project, err := parseID(pid)
if err != nil {
return nil, nil, err
}
u := fmt.Sprintf("projects/%s/merge_trains", PathEscape(project))
req, err := s.client.NewRequest(http.MethodGet, u, opt, options)
if err != nil {
return nil, nil, err
}
var mts []*MergeTrain
resp, err := s.client.Do(req, &mts)
if err != nil {
return nil, resp, err
}
return mts, resp, nil
}
// ListMergeRequestInMergeTrain gets a list of merge requests added to a merge
// train for the requested target branch.
//
// GitLab API docs:
// https://docs.gitlab.com/ee/api/merge_trains.html#list-merge-requests-in-a-merge-train
func (s *MergeTrainsService) ListMergeRequestInMergeTrain(pid interface{}, targetBranch string, opts *ListMergeTrainsOptions, options ...RequestOptionFunc) ([]*MergeTrain, *Response, error) {
project, err := parseID(pid)
if err != nil {
return nil, nil, err
}
u := fmt.Sprintf("projects/%s/merge_trains/%s", PathEscape(project), targetBranch)
req, err := s.client.NewRequest(http.MethodGet, u, opts, options)
if err != nil {
return nil, nil, err
}
var mts []*MergeTrain
resp, err := s.client.Do(req, &mts)
if err != nil {
return nil, resp, err
}
return mts, resp, nil
}
// GetMergeRequestOnAMergeTrain Get merge train information for the requested
// merge request.
//
// GitLab API docs:
// https://docs.gitlab.com/ee/api/merge_trains.html#get-the-status-of-a-merge-request-on-a-merge-train
func (s *MergeTrainsService) GetMergeRequestOnAMergeTrain(pid interface{}, mergeRequest int, options ...RequestOptionFunc) (*MergeTrain, *Response, error) {
project, err := parseID(pid)
if err != nil {
return nil, nil, err
}
u := fmt.Sprintf("projects/%s/merge_trains/merge_requests/%d", PathEscape(project), mergeRequest)
req, err := s.client.NewRequest(http.MethodGet, u, nil, options)
if err != nil {
return nil, nil, err
}
mt := new(MergeTrain)
resp, err := s.client.Do(req, mt)
if err != nil {
return nil, resp, err
}
return mt, resp, nil
}
// AddMergeRequestToMergeTrainOptions represents the available
// AddMergeRequestToMergeTrain() options.
//
// GitLab API docs:
// https://docs.gitlab.com/ee/api/merge_trains.html#add-a-merge-request-to-a-merge-train
type AddMergeRequestToMergeTrainOptions struct {
WhenPipelineSucceeds *bool `url:"when_pipeline_succeeds,omitempty" json:"when_pipeline_succeeds,omitempty"`
SHA *string `url:"sha,omitempty" json:"sha,omitempty"`
Squash *bool `url:"squash,omitempty" json:"squash,omitempty"`
}
// AddMergeRequestToMergeTrain Add a merge request to the merge train targeting
// the merge request’s target branch.
//
// GitLab API docs:
// https://docs.gitlab.com/ee/api/merge_trains.html#add-a-merge-request-to-a-merge-train
func (s *MergeTrainsService) AddMergeRequestToMergeTrain(pid interface{}, mergeRequest int, opts *AddMergeRequestToMergeTrainOptions, options ...RequestOptionFunc) ([]*MergeTrain, *Response, error) {
project, err := parseID(pid)
if err != nil {
return nil, nil, err
}
u := fmt.Sprintf("projects/%s/merge_trains/merge_requests/%d", PathEscape(project), mergeRequest)
req, err := s.client.NewRequest(http.MethodPost, u, opts, options)
if err != nil {
return nil, nil, err
}
var mts []*MergeTrain
resp, err := s.client.Do(req, &mts)
if err != nil {
return nil, resp, err
}
return mts, resp, nil
}