pipelines.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. package gitlab
  17. import (
  18. "fmt"
  19. "net/url"
  20. "time"
  21. )
  22. // PipelinesService handles communication with the repositories related
  23. // methods of the GitLab API.
  24. //
  25. // GitLab API docs: https://docs.gitlab.com/ce/api/pipelines.html
  26. type PipelinesService struct {
  27. client *Client
  28. }
  29. // PipelineVariable represents a pipeline variable.
  30. //
  31. // GitLab API docs: https://docs.gitlab.com/ce/api/pipelines.html
  32. type PipelineVariable struct {
  33. Key string `json:"key"`
  34. Value string `json:"value"`
  35. }
  36. // Pipeline represents a GitLab pipeline.
  37. //
  38. // GitLab API docs: https://docs.gitlab.com/ce/api/pipelines.html
  39. type Pipeline struct {
  40. ID int `json:"id"`
  41. Status string `json:"status"`
  42. Ref string `json:"ref"`
  43. Sha string `json:"sha"`
  44. BeforeSha string `json:"before_sha"`
  45. Tag bool `json:"tag"`
  46. YamlErrors string `json:"yaml_errors"`
  47. User struct {
  48. Name string `json:"name"`
  49. Username string `json:"username"`
  50. ID int `json:"id"`
  51. State string `json:"state"`
  52. AvatarURL string `json:"avatar_url"`
  53. WebURL string `json:"web_url"`
  54. }
  55. UpdatedAt *time.Time `json:"updated_at"`
  56. CreatedAt *time.Time `json:"created_at"`
  57. StartedAt *time.Time `json:"started_at"`
  58. FinishedAt *time.Time `json:"finished_at"`
  59. CommittedAt *time.Time `json:"committed_at"`
  60. Duration int `json:"duration"`
  61. Coverage string `json:"coverage"`
  62. }
  63. func (i Pipeline) String() string {
  64. return Stringify(i)
  65. }
  66. // PipelineList represents a GitLab list project pipelines
  67. //
  68. // GitLab API docs: https://docs.gitlab.com/ce/api/pipelines.html#list-project-pipelines
  69. type PipelineList []struct {
  70. ID int `json:"id"`
  71. Status string `json:"status"`
  72. Ref string `json:"ref"`
  73. Sha string `json:"sha"`
  74. }
  75. func (i PipelineList) String() string {
  76. return Stringify(i)
  77. }
  78. // ListProjectPipelinesOptions represents the available ListProjectPipelines() options.
  79. //
  80. // GitLab API docs: https://docs.gitlab.com/ce/api/pipelines.html#list-project-pipelines
  81. type ListProjectPipelinesOptions struct {
  82. ListOptions
  83. Scope *string `url:"scope,omitempty" json:"scope,omitempty"`
  84. Status *BuildStateValue `url:"status,omitempty" json:"status,omitempty"`
  85. Ref *string `url:"ref,omitempty" json:"ref,omitempty"`
  86. YamlErrors *bool `url:"yaml_errors,omitempty" json:"yaml_errors,omitempty"`
  87. Name *string `url:"name,omitempty" json:"name,omitempty"`
  88. Username *string `url:"username,omitempty" json:"username,omitempty"`
  89. OrderBy *string `url:"order_by,omitempty" json:"order_by,omitempty"`
  90. Sort *string `url:"sort,omitempty" json:"sort,omitempty"`
  91. }
  92. // ListProjectPipelines gets a list of project piplines.
  93. //
  94. // GitLab API docs: https://docs.gitlab.com/ce/api/pipelines.html#list-project-pipelines
  95. func (s *PipelinesService) ListProjectPipelines(pid interface{}, opt *ListProjectPipelinesOptions, options ...OptionFunc) (PipelineList, *Response, error) {
  96. project, err := parseID(pid)
  97. if err != nil {
  98. return nil, nil, err
  99. }
  100. u := fmt.Sprintf("projects/%s/pipelines", url.QueryEscape(project))
  101. req, err := s.client.NewRequest("GET", u, opt, options)
  102. if err != nil {
  103. return nil, nil, err
  104. }
  105. var p PipelineList
  106. resp, err := s.client.Do(req, &p)
  107. if err != nil {
  108. return nil, resp, err
  109. }
  110. return p, resp, err
  111. }
  112. // GetPipeline gets a single project pipeline.
  113. //
  114. // GitLab API docs: https://docs.gitlab.com/ce/api/pipelines.html#get-a-single-pipeline
  115. func (s *PipelinesService) GetPipeline(pid interface{}, pipeline int, options ...OptionFunc) (*Pipeline, *Response, error) {
  116. project, err := parseID(pid)
  117. if err != nil {
  118. return nil, nil, err
  119. }
  120. u := fmt.Sprintf("projects/%s/pipelines/%d", url.QueryEscape(project), pipeline)
  121. req, err := s.client.NewRequest("GET", u, nil, options)
  122. if err != nil {
  123. return nil, nil, err
  124. }
  125. p := new(Pipeline)
  126. resp, err := s.client.Do(req, p)
  127. if err != nil {
  128. return nil, resp, err
  129. }
  130. return p, resp, err
  131. }
  132. // CreatePipelineOptions represents the available CreatePipeline() options.
  133. //
  134. // GitLab API docs: https://docs.gitlab.com/ce/api/pipelines.html#create-a-new-pipeline
  135. type CreatePipelineOptions struct {
  136. Ref *string `url:"ref" json:"ref"`
  137. Variables []*PipelineVariable `url:"variables,omitempty" json:"variables,omitempty"`
  138. }
  139. // CreatePipeline creates a new project pipeline.
  140. //
  141. // GitLab API docs: https://docs.gitlab.com/ce/api/pipelines.html#create-a-new-pipeline
  142. func (s *PipelinesService) CreatePipeline(pid interface{}, opt *CreatePipelineOptions, 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/pipeline", url.QueryEscape(project))
  148. req, err := s.client.NewRequest("POST", u, opt, options)
  149. if err != nil {
  150. return nil, nil, err
  151. }
  152. p := new(Pipeline)
  153. resp, err := s.client.Do(req, p)
  154. if err != nil {
  155. return nil, resp, err
  156. }
  157. return p, resp, err
  158. }
  159. // RetryPipelineBuild retries failed builds in a pipeline
  160. //
  161. // GitLab API docs:
  162. // https://docs.gitlab.com/ce/api/pipelines.html#retry-failed-builds-in-a-pipeline
  163. func (s *PipelinesService) RetryPipelineBuild(pid interface{}, pipelineID int, options ...OptionFunc) (*Pipeline, *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/retry", project, pipelineID)
  169. req, err := s.client.NewRequest("POST", u, nil, options)
  170. if err != nil {
  171. return nil, nil, err
  172. }
  173. p := new(Pipeline)
  174. resp, err := s.client.Do(req, p)
  175. if err != nil {
  176. return nil, resp, err
  177. }
  178. return p, resp, err
  179. }
  180. // CancelPipelineBuild cancels a pipeline builds
  181. //
  182. // GitLab API docs:
  183. //https://docs.gitlab.com/ce/api/pipelines.html#cancel-a-pipelines-builds
  184. func (s *PipelinesService) CancelPipelineBuild(pid interface{}, pipelineID int, options ...OptionFunc) (*Pipeline, *Response, error) {
  185. project, err := parseID(pid)
  186. if err != nil {
  187. return nil, nil, err
  188. }
  189. u := fmt.Sprintf("projects/%s/pipelines/%d/cancel", project, pipelineID)
  190. req, err := s.client.NewRequest("POST", u, nil, options)
  191. if err != nil {
  192. return nil, nil, err
  193. }
  194. p := new(Pipeline)
  195. resp, err := s.client.Do(req, p)
  196. if err != nil {
  197. return nil, resp, err
  198. }
  199. return p, resp, err
  200. }