1//
2// Copyright 2017, Igor Varavko
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//
16
17package gitlab
18
19import (
20 "fmt"
21 "time"
22)
23
24// PipelinesService handles communication with the repositories related
25// methods of the GitLab API.
26//
27// GitLab API docs: https://docs.gitlab.com/ce/api/pipelines.html
28type PipelinesService struct {
29 client *Client
30}
31
32// PipelineVariable represents a pipeline variable.
33//
34// GitLab API docs: https://docs.gitlab.com/ce/api/pipelines.html
35type PipelineVariable struct {
36 Key string `json:"key"`
37 Value string `json:"value"`
38 VariableType string `json:"variable_type"`
39}
40
41// Pipeline represents a GitLab pipeline.
42//
43// GitLab API docs: https://docs.gitlab.com/ce/api/pipelines.html
44type Pipeline struct {
45 ID int `json:"id"`
46 Status string `json:"status"`
47 Ref string `json:"ref"`
48 SHA string `json:"sha"`
49 BeforeSHA string `json:"before_sha"`
50 Tag bool `json:"tag"`
51 YamlErrors string `json:"yaml_errors"`
52 User *BasicUser `json:"user"`
53 UpdatedAt *time.Time `json:"updated_at"`
54 CreatedAt *time.Time `json:"created_at"`
55 StartedAt *time.Time `json:"started_at"`
56 FinishedAt *time.Time `json:"finished_at"`
57 CommittedAt *time.Time `json:"committed_at"`
58 Duration int `json:"duration"`
59 Coverage string `json:"coverage"`
60 WebURL string `json:"web_url"`
61 DetailedStatus *DetailedStatus `json:"detailed_status"`
62}
63
64// DetailedStatus contains detailed information about the status of a pipeline
65type DetailedStatus struct {
66 Icon string `json:"icon"`
67 Text string `json:"text"`
68 Label string `json:"label"`
69 Group string `json:"group"`
70 Tooltip string `json:"tooltip"`
71 HasDetails bool `json:"has_details"`
72 DetailsPath string `json:"details_path"`
73 Illustration struct {
74 Image string `json:"image"`
75 } `json:"illustration"`
76 Favicon string `json:"favicon"`
77}
78
79func (p Pipeline) String() string {
80 return Stringify(p)
81}
82
83// PipelineInfo shows the basic entities of a pipeline, mostly used as fields
84// on other assets, like Commit.
85type PipelineInfo struct {
86 ID int `json:"id"`
87 Status string `json:"status"`
88 Ref string `json:"ref"`
89 SHA string `json:"sha"`
90 WebURL string `json:"web_url"`
91 UpdatedAt *time.Time `json:"updated_at"`
92 CreatedAt *time.Time `json:"created_at"`
93}
94
95func (p PipelineInfo) String() string {
96 return Stringify(p)
97}
98
99// ListProjectPipelinesOptions represents the available ListProjectPipelines() options.
100//
101// GitLab API docs: https://docs.gitlab.com/ce/api/pipelines.html#list-project-pipelines
102type ListProjectPipelinesOptions struct {
103 ListOptions
104 Scope *string `url:"scope,omitempty" json:"scope,omitempty"`
105 Status *BuildStateValue `url:"status,omitempty" json:"status,omitempty"`
106 Ref *string `url:"ref,omitempty" json:"ref,omitempty"`
107 SHA *string `url:"sha,omitempty" json:"sha,omitempty"`
108 YamlErrors *bool `url:"yaml_errors,omitempty" json:"yaml_errors,omitempty"`
109 Name *string `url:"name,omitempty" json:"name,omitempty"`
110 Username *string `url:"username,omitempty" json:"username,omitempty"`
111 OrderBy *string `url:"order_by,omitempty" json:"order_by,omitempty"`
112 Sort *string `url:"sort,omitempty" json:"sort,omitempty"`
113}
114
115// ListProjectPipelines gets a list of project piplines.
116//
117// GitLab API docs: https://docs.gitlab.com/ce/api/pipelines.html#list-project-pipelines
118func (s *PipelinesService) ListProjectPipelines(pid interface{}, opt *ListProjectPipelinesOptions, options ...OptionFunc) ([]*PipelineInfo, *Response, error) {
119 project, err := parseID(pid)
120 if err != nil {
121 return nil, nil, err
122 }
123 u := fmt.Sprintf("projects/%s/pipelines", pathEscape(project))
124
125 req, err := s.client.NewRequest("GET", u, opt, options)
126 if err != nil {
127 return nil, nil, err
128 }
129
130 var p []*PipelineInfo
131 resp, err := s.client.Do(req, &p)
132 if err != nil {
133 return nil, resp, err
134 }
135
136 return p, resp, err
137}
138
139// GetPipeline gets a single project pipeline.
140//
141// GitLab API docs: https://docs.gitlab.com/ce/api/pipelines.html#get-a-single-pipeline
142func (s *PipelinesService) GetPipeline(pid interface{}, pipeline int, options ...OptionFunc) (*Pipeline, *Response, error) {
143 project, err := parseID(pid)
144 if err != nil {
145 return nil, nil, err
146 }
147 u := fmt.Sprintf("projects/%s/pipelines/%d", pathEscape(project), pipeline)
148
149 req, err := s.client.NewRequest("GET", u, nil, options)
150 if err != nil {
151 return nil, nil, err
152 }
153
154 p := new(Pipeline)
155 resp, err := s.client.Do(req, p)
156 if err != nil {
157 return nil, resp, err
158 }
159
160 return p, resp, err
161}
162
163// GetPipelineVariables gets the variables of a single project pipeline.
164//
165// GitLab API docs: https://docs.gitlab.com/ce/api/pipelines.html#get-variables-of-a-pipeline
166func (s *PipelinesService) GetPipelineVariables(pid interface{}, pipeline int, options ...OptionFunc) ([]*PipelineVariable, *Response, error) {
167 project, err := parseID(pid)
168 if err != nil {
169 return nil, nil, err
170 }
171 u := fmt.Sprintf("projects/%s/pipelines/%d/variables", pathEscape(project), pipeline)
172
173 req, err := s.client.NewRequest("GET", u, nil, options)
174 if err != nil {
175 return nil, nil, err
176 }
177
178 var p []*PipelineVariable
179 resp, err := s.client.Do(req, &p)
180 if err != nil {
181 return nil, resp, err
182 }
183
184 return p, resp, err
185}
186
187// CreatePipelineOptions represents the available CreatePipeline() options.
188//
189// GitLab API docs: https://docs.gitlab.com/ce/api/pipelines.html#create-a-new-pipeline
190type CreatePipelineOptions struct {
191 Ref *string `url:"ref" json:"ref"`
192 Variables []*PipelineVariable `url:"variables,omitempty" json:"variables,omitempty"`
193}
194
195// CreatePipeline creates a new project pipeline.
196//
197// GitLab API docs: https://docs.gitlab.com/ce/api/pipelines.html#create-a-new-pipeline
198func (s *PipelinesService) CreatePipeline(pid interface{}, opt *CreatePipelineOptions, options ...OptionFunc) (*Pipeline, *Response, error) {
199 project, err := parseID(pid)
200 if err != nil {
201 return nil, nil, err
202 }
203 u := fmt.Sprintf("projects/%s/pipeline", pathEscape(project))
204
205 req, err := s.client.NewRequest("POST", u, opt, options)
206 if err != nil {
207 return nil, nil, err
208 }
209
210 p := new(Pipeline)
211 resp, err := s.client.Do(req, p)
212 if err != nil {
213 return nil, resp, err
214 }
215
216 return p, resp, err
217}
218
219// RetryPipelineBuild retries failed builds in a pipeline
220//
221// GitLab API docs:
222// https://docs.gitlab.com/ce/api/pipelines.html#retry-failed-builds-in-a-pipeline
223func (s *PipelinesService) RetryPipelineBuild(pid interface{}, pipeline int, options ...OptionFunc) (*Pipeline, *Response, error) {
224 project, err := parseID(pid)
225 if err != nil {
226 return nil, nil, err
227 }
228 u := fmt.Sprintf("projects/%s/pipelines/%d/retry", pathEscape(project), pipeline)
229
230 req, err := s.client.NewRequest("POST", u, nil, options)
231 if err != nil {
232 return nil, nil, err
233 }
234
235 p := new(Pipeline)
236 resp, err := s.client.Do(req, p)
237 if err != nil {
238 return nil, resp, err
239 }
240
241 return p, resp, err
242}
243
244// CancelPipelineBuild cancels a pipeline builds
245//
246// GitLab API docs:
247//https://docs.gitlab.com/ce/api/pipelines.html#cancel-a-pipelines-builds
248func (s *PipelinesService) CancelPipelineBuild(pid interface{}, pipeline int, options ...OptionFunc) (*Pipeline, *Response, error) {
249 project, err := parseID(pid)
250 if err != nil {
251 return nil, nil, err
252 }
253 u := fmt.Sprintf("projects/%s/pipelines/%d/cancel", pathEscape(project), pipeline)
254
255 req, err := s.client.NewRequest("POST", u, nil, options)
256 if err != nil {
257 return nil, nil, err
258 }
259
260 p := new(Pipeline)
261 resp, err := s.client.Do(req, p)
262 if err != nil {
263 return nil, resp, err
264 }
265
266 return p, resp, err
267}
268
269// DeletePipeline deletes an existing pipeline.
270//
271// GitLab API docs:
272// https://docs.gitlab.com/ce/api/pipelines.html#delete-a-pipeline
273func (s *PipelinesService) DeletePipeline(pid interface{}, pipeline int, options ...OptionFunc) (*Response, error) {
274 project, err := parseID(pid)
275 if err != nil {
276 return nil, err
277 }
278 u := fmt.Sprintf("projects/%s/pipelines/%d", pathEscape(project), pipeline)
279
280 req, err := s.client.NewRequest("DELETE", u, nil, options)
281 if err != nil {
282 return nil, err
283 }
284
285 return s.client.Do(req, nil)
286}