pipelines.go

  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}
 39
 40// Pipeline represents a GitLab pipeline.
 41//
 42// GitLab API docs: https://docs.gitlab.com/ce/api/pipelines.html
 43type Pipeline struct {
 44	ID             int             `json:"id"`
 45	Status         string          `json:"status"`
 46	Ref            string          `json:"ref"`
 47	SHA            string          `json:"sha"`
 48	BeforeSHA      string          `json:"before_sha"`
 49	Tag            bool            `json:"tag"`
 50	YamlErrors     string          `json:"yaml_errors"`
 51	User           *BasicUser      `json:"user"`
 52	UpdatedAt      *time.Time      `json:"updated_at"`
 53	CreatedAt      *time.Time      `json:"created_at"`
 54	StartedAt      *time.Time      `json:"started_at"`
 55	FinishedAt     *time.Time      `json:"finished_at"`
 56	CommittedAt    *time.Time      `json:"committed_at"`
 57	Duration       int             `json:"duration"`
 58	Coverage       string          `json:"coverage"`
 59	WebURL         string          `json:"web_url"`
 60	DetailedStatus *DetailedStatus `json:"detailed_status"`
 61}
 62
 63// DetailedStatus contains detailed information about the status of a pipeline
 64type DetailedStatus struct {
 65	Icon         string `json:"icon"`
 66	Text         string `json:"text"`
 67	Label        string `json:"label"`
 68	Group        string `json:"group"`
 69	Tooltip      string `json:"tooltip"`
 70	HasDetails   bool   `json:"has_details"`
 71	DetailsPath  string `json:"details_path"`
 72	Illustration struct {
 73		Image string `json:"image"`
 74	} `json:"illustration"`
 75	Favicon string `json:"favicon"`
 76}
 77
 78func (p Pipeline) String() string {
 79	return Stringify(p)
 80}
 81
 82// PipelineInfo shows the basic entities of a pipeline, mostly used as fields
 83// on other assets, like Commit.
 84type PipelineInfo struct {
 85	ID     int    `json:"id"`
 86	Status string `json:"status"`
 87	Ref    string `json:"ref"`
 88	SHA    string `json:"sha"`
 89	WebURL string `json:"web_url"`
 90}
 91
 92func (p PipelineInfo) String() string {
 93	return Stringify(p)
 94}
 95
 96// ListProjectPipelinesOptions represents the available ListProjectPipelines() options.
 97//
 98// GitLab API docs: https://docs.gitlab.com/ce/api/pipelines.html#list-project-pipelines
 99type ListProjectPipelinesOptions struct {
100	ListOptions
101	Scope      *string          `url:"scope,omitempty" json:"scope,omitempty"`
102	Status     *BuildStateValue `url:"status,omitempty" json:"status,omitempty"`
103	Ref        *string          `url:"ref,omitempty" json:"ref,omitempty"`
104	SHA        *string          `url:"sha,omitempty" json:"sha,omitempty"`
105	YamlErrors *bool            `url:"yaml_errors,omitempty" json:"yaml_errors,omitempty"`
106	Name       *string          `url:"name,omitempty" json:"name,omitempty"`
107	Username   *string          `url:"username,omitempty" json:"username,omitempty"`
108	OrderBy    *string          `url:"order_by,omitempty" json:"order_by,omitempty"`
109	Sort       *string          `url:"sort,omitempty" json:"sort,omitempty"`
110}
111
112// ListProjectPipelines gets a list of project piplines.
113//
114// GitLab API docs: https://docs.gitlab.com/ce/api/pipelines.html#list-project-pipelines
115func (s *PipelinesService) ListProjectPipelines(pid interface{}, opt *ListProjectPipelinesOptions, options ...OptionFunc) ([]*PipelineInfo, *Response, error) {
116	project, err := parseID(pid)
117	if err != nil {
118		return nil, nil, err
119	}
120	u := fmt.Sprintf("projects/%s/pipelines", pathEscape(project))
121
122	req, err := s.client.NewRequest("GET", u, opt, options)
123	if err != nil {
124		return nil, nil, err
125	}
126
127	var p []*PipelineInfo
128	resp, err := s.client.Do(req, &p)
129	if err != nil {
130		return nil, resp, err
131	}
132
133	return p, resp, err
134}
135
136// GetPipeline gets a single project pipeline.
137//
138// GitLab API docs: https://docs.gitlab.com/ce/api/pipelines.html#get-a-single-pipeline
139func (s *PipelinesService) GetPipeline(pid interface{}, pipeline int, options ...OptionFunc) (*Pipeline, *Response, error) {
140	project, err := parseID(pid)
141	if err != nil {
142		return nil, nil, err
143	}
144	u := fmt.Sprintf("projects/%s/pipelines/%d", pathEscape(project), pipeline)
145
146	req, err := s.client.NewRequest("GET", u, nil, options)
147	if err != nil {
148		return nil, nil, err
149	}
150
151	p := new(Pipeline)
152	resp, err := s.client.Do(req, p)
153	if err != nil {
154		return nil, resp, err
155	}
156
157	return p, resp, err
158}
159
160// GetPipelineVariables gets the variables of a single project pipeline.
161//
162// GitLab API docs: https://docs.gitlab.com/ce/api/pipelines.html#get-variables-of-a-pipeline
163func (s *PipelinesService) GetPipelineVariables(pid interface{}, pipeline int, options ...OptionFunc) ([]*PipelineVariable, *Response, error) {
164	project, err := parseID(pid)
165	if err != nil {
166		return nil, nil, err
167	}
168	u := fmt.Sprintf("projects/%s/pipelines/%d/variables", pathEscape(project), pipeline)
169
170	req, err := s.client.NewRequest("GET", u, nil, options)
171	if err != nil {
172		return nil, nil, err
173	}
174
175	var p []*PipelineVariable
176	resp, err := s.client.Do(req, &p)
177	if err != nil {
178		return nil, resp, err
179	}
180
181	return p, resp, err
182}
183
184// CreatePipelineOptions represents the available CreatePipeline() options.
185//
186// GitLab API docs: https://docs.gitlab.com/ce/api/pipelines.html#create-a-new-pipeline
187type CreatePipelineOptions struct {
188	Ref       *string             `url:"ref" json:"ref"`
189	Variables []*PipelineVariable `url:"variables,omitempty" json:"variables,omitempty"`
190}
191
192// CreatePipeline creates a new project pipeline.
193//
194// GitLab API docs: https://docs.gitlab.com/ce/api/pipelines.html#create-a-new-pipeline
195func (s *PipelinesService) CreatePipeline(pid interface{}, opt *CreatePipelineOptions, options ...OptionFunc) (*Pipeline, *Response, error) {
196	project, err := parseID(pid)
197	if err != nil {
198		return nil, nil, err
199	}
200	u := fmt.Sprintf("projects/%s/pipeline", pathEscape(project))
201
202	req, err := s.client.NewRequest("POST", u, opt, options)
203	if err != nil {
204		return nil, nil, err
205	}
206
207	p := new(Pipeline)
208	resp, err := s.client.Do(req, p)
209	if err != nil {
210		return nil, resp, err
211	}
212
213	return p, resp, err
214}
215
216// RetryPipelineBuild retries failed builds in a pipeline
217//
218// GitLab API docs:
219// https://docs.gitlab.com/ce/api/pipelines.html#retry-failed-builds-in-a-pipeline
220func (s *PipelinesService) RetryPipelineBuild(pid interface{}, pipeline int, options ...OptionFunc) (*Pipeline, *Response, error) {
221	project, err := parseID(pid)
222	if err != nil {
223		return nil, nil, err
224	}
225	u := fmt.Sprintf("projects/%s/pipelines/%d/retry", pathEscape(project), pipeline)
226
227	req, err := s.client.NewRequest("POST", u, nil, options)
228	if err != nil {
229		return nil, nil, err
230	}
231
232	p := new(Pipeline)
233	resp, err := s.client.Do(req, p)
234	if err != nil {
235		return nil, resp, err
236	}
237
238	return p, resp, err
239}
240
241// CancelPipelineBuild cancels a pipeline builds
242//
243// GitLab API docs:
244//https://docs.gitlab.com/ce/api/pipelines.html#cancel-a-pipelines-builds
245func (s *PipelinesService) CancelPipelineBuild(pid interface{}, pipeline int, options ...OptionFunc) (*Pipeline, *Response, error) {
246	project, err := parseID(pid)
247	if err != nil {
248		return nil, nil, err
249	}
250	u := fmt.Sprintf("projects/%s/pipelines/%d/cancel", pathEscape(project), pipeline)
251
252	req, err := s.client.NewRequest("POST", u, nil, options)
253	if err != nil {
254		return nil, nil, err
255	}
256
257	p := new(Pipeline)
258	resp, err := s.client.Do(req, p)
259	if err != nil {
260		return nil, resp, err
261	}
262
263	return p, resp, err
264}
265
266// DeletePipeline deletes an existing pipeline.
267//
268// GitLab API docs:
269// https://docs.gitlab.com/ce/api/pipelines.html#delete-a-pipeline
270func (s *PipelinesService) DeletePipeline(pid interface{}, pipeline int, options ...OptionFunc) (*Response, error) {
271	project, err := parseID(pid)
272	if err != nil {
273		return nil, err
274	}
275	u := fmt.Sprintf("projects/%s/pipelines/%d", pathEscape(project), pipeline)
276
277	req, err := s.client.NewRequest("DELETE", u, nil, options)
278	if err != nil {
279		return nil, err
280	}
281
282	return s.client.Do(req, nil)
283}