pipelines.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. SHA *string `url:"sha,omitempty" json:"sha,omitempty"`
  87. YamlErrors *bool `url:"yaml_errors,omitempty" json:"yaml_errors,omitempty"`
  88. Name *string `url:"name,omitempty" json:"name,omitempty"`
  89. Username *string `url:"username,omitempty" json:"username,omitempty"`
  90. OrderBy *string `url:"order_by,omitempty" json:"order_by,omitempty"`
  91. Sort *string `url:"sort,omitempty" json:"sort,omitempty"`
  92. }
  93. // ListProjectPipelines gets a list of project piplines.
  94. //
  95. // GitLab API docs: https://docs.gitlab.com/ce/api/pipelines.html#list-project-pipelines
  96. func (s *PipelinesService) ListProjectPipelines(pid interface{}, opt *ListProjectPipelinesOptions, options ...OptionFunc) (PipelineList, *Response, error) {
  97. project, err := parseID(pid)
  98. if err != nil {
  99. return nil, nil, err
  100. }
  101. u := fmt.Sprintf("projects/%s/pipelines", url.QueryEscape(project))
  102. req, err := s.client.NewRequest("GET", u, opt, options)
  103. if err != nil {
  104. return nil, nil, err
  105. }
  106. var p PipelineList
  107. resp, err := s.client.Do(req, &p)
  108. if err != nil {
  109. return nil, resp, err
  110. }
  111. return p, resp, err
  112. }
  113. // GetPipeline gets a single project pipeline.
  114. //
  115. // GitLab API docs: https://docs.gitlab.com/ce/api/pipelines.html#get-a-single-pipeline
  116. func (s *PipelinesService) GetPipeline(pid interface{}, pipeline int, options ...OptionFunc) (*Pipeline, *Response, error) {
  117. project, err := parseID(pid)
  118. if err != nil {
  119. return nil, nil, err
  120. }
  121. u := fmt.Sprintf("projects/%s/pipelines/%d", url.QueryEscape(project), pipeline)
  122. req, err := s.client.NewRequest("GET", u, nil, options)
  123. if err != nil {
  124. return nil, nil, err
  125. }
  126. p := new(Pipeline)
  127. resp, err := s.client.Do(req, p)
  128. if err != nil {
  129. return nil, resp, err
  130. }
  131. return p, resp, err
  132. }
  133. // CreatePipelineOptions represents the available CreatePipeline() options.
  134. //
  135. // GitLab API docs: https://docs.gitlab.com/ce/api/pipelines.html#create-a-new-pipeline
  136. type CreatePipelineOptions struct {
  137. Ref *string `url:"ref" json:"ref"`
  138. Variables []*PipelineVariable `url:"variables,omitempty" json:"variables,omitempty"`
  139. }
  140. // CreatePipeline creates a new project pipeline.
  141. //
  142. // GitLab API docs: https://docs.gitlab.com/ce/api/pipelines.html#create-a-new-pipeline
  143. func (s *PipelinesService) CreatePipeline(pid interface{}, opt *CreatePipelineOptions, options ...OptionFunc) (*Pipeline, *Response, error) {
  144. project, err := parseID(pid)
  145. if err != nil {
  146. return nil, nil, err
  147. }
  148. u := fmt.Sprintf("projects/%s/pipeline", url.QueryEscape(project))
  149. req, err := s.client.NewRequest("POST", u, opt, options)
  150. if err != nil {
  151. return nil, nil, err
  152. }
  153. p := new(Pipeline)
  154. resp, err := s.client.Do(req, p)
  155. if err != nil {
  156. return nil, resp, err
  157. }
  158. return p, resp, err
  159. }
  160. // RetryPipelineBuild retries failed builds in a pipeline
  161. //
  162. // GitLab API docs:
  163. // https://docs.gitlab.com/ce/api/pipelines.html#retry-failed-builds-in-a-pipeline
  164. func (s *PipelinesService) RetryPipelineBuild(pid interface{}, pipelineID int, options ...OptionFunc) (*Pipeline, *Response, error) {
  165. project, err := parseID(pid)
  166. if err != nil {
  167. return nil, nil, err
  168. }
  169. u := fmt.Sprintf("projects/%s/pipelines/%d/retry", project, pipelineID)
  170. req, err := s.client.NewRequest("POST", u, nil, options)
  171. if err != nil {
  172. return nil, nil, err
  173. }
  174. p := new(Pipeline)
  175. resp, err := s.client.Do(req, p)
  176. if err != nil {
  177. return nil, resp, err
  178. }
  179. return p, resp, err
  180. }
  181. // CancelPipelineBuild cancels a pipeline builds
  182. //
  183. // GitLab API docs:
  184. //https://docs.gitlab.com/ce/api/pipelines.html#cancel-a-pipelines-builds
  185. func (s *PipelinesService) CancelPipelineBuild(pid interface{}, pipelineID int, options ...OptionFunc) (*Pipeline, *Response, error) {
  186. project, err := parseID(pid)
  187. if err != nil {
  188. return nil, nil, err
  189. }
  190. u := fmt.Sprintf("projects/%s/pipelines/%d/cancel", project, pipelineID)
  191. req, err := s.client.NewRequest("POST", u, nil, options)
  192. if err != nil {
  193. return nil, nil, err
  194. }
  195. p := new(Pipeline)
  196. resp, err := s.client.Do(req, p)
  197. if err != nil {
  198. return nil, resp, err
  199. }
  200. return p, resp, err
  201. }