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