pipeline_triggers.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. package gitlab
  2. import (
  3. "fmt"
  4. "net/url"
  5. "time"
  6. )
  7. // PipelineTriggersService handles Project pipeline triggers.
  8. //
  9. // GitLab API docs:
  10. // https://docs.gitlab.com/ce/api/pipeline_triggers.html
  11. type PipelineTriggersService struct {
  12. client *Client
  13. }
  14. // PipelineTrigger represents a project pipeline trigger.
  15. //
  16. // GitLab API docs:
  17. // https://docs.gitlab.com/ce/api/pipeline_triggers.html#pipeline-triggers
  18. type PipelineTrigger struct {
  19. ID int `json:"id"`
  20. Description string `json:"description"`
  21. CreatedAt *time.Time `json:"created_at"`
  22. DeletedAt *time.Time `json:"deleted_at"`
  23. LastUsed *time.Time `json:"last_used"`
  24. Token string `json:"token"`
  25. UpdatedAt *time.Time `json:"updated_at"`
  26. Owner *User `json:"owner"`
  27. }
  28. // ListPipelineTriggersOptions represents the available ListPipelineTriggers() options.
  29. //
  30. // GitLab API docs:
  31. // https://docs.gitlab.com/ce/api/pipeline_triggers.html#list-project-triggers
  32. type ListPipelineTriggersOptions ListOptions
  33. // ListPipelineTriggers gets a list of project triggers.
  34. //
  35. // GitLab API docs:
  36. // https://docs.gitlab.com/ce/api/pipeline_triggers.html#list-project-triggers
  37. func (s *PipelineTriggersService) ListPipelineTriggers(pid interface{}, opt *ListPipelineTriggersOptions, options ...OptionFunc) ([]*PipelineTrigger, *Response, error) {
  38. project, err := parseID(pid)
  39. if err != nil {
  40. return nil, nil, err
  41. }
  42. u := fmt.Sprintf("projects/%s/triggers", url.QueryEscape(project))
  43. req, err := s.client.NewRequest("GET", u, opt, options)
  44. if err != nil {
  45. return nil, nil, err
  46. }
  47. var pt []*PipelineTrigger
  48. resp, err := s.client.Do(req, &pt)
  49. if err != nil {
  50. return nil, resp, err
  51. }
  52. return pt, resp, err
  53. }
  54. // GetPipelineTrigger gets a specific pipeline trigger for a project.
  55. //
  56. // GitLab API docs:
  57. // https://docs.gitlab.com/ce/api/pipeline_triggers.html#get-trigger-details
  58. func (s *PipelineTriggersService) GetPipelineTrigger(pid interface{}, trigger int, options ...OptionFunc) (*PipelineTrigger, *Response, error) {
  59. project, err := parseID(pid)
  60. if err != nil {
  61. return nil, nil, err
  62. }
  63. u := fmt.Sprintf("projects/%s/triggers/%d", url.QueryEscape(project), trigger)
  64. req, err := s.client.NewRequest("GET", u, nil, options)
  65. if err != nil {
  66. return nil, nil, err
  67. }
  68. pt := new(PipelineTrigger)
  69. resp, err := s.client.Do(req, pt)
  70. if err != nil {
  71. return nil, resp, err
  72. }
  73. return pt, resp, err
  74. }
  75. // AddPipelineTriggerOptions represents the available AddPipelineTrigger() options.
  76. //
  77. // GitLab API docs:
  78. // https://docs.gitlab.com/ce/api/pipeline_triggers.html#create-a-project-trigger
  79. type AddPipelineTriggerOptions struct {
  80. Description *string `url:"description,omitempty" json:"description,omitempty"`
  81. }
  82. // AddPipelineTrigger adds a pipeline trigger to a specified project.
  83. //
  84. // GitLab API docs:
  85. // https://docs.gitlab.com/ce/api/pipeline_triggers.html#create-a-project-trigger
  86. func (s *PipelineTriggersService) AddPipelineTrigger(pid interface{}, opt *AddPipelineTriggerOptions, options ...OptionFunc) (*PipelineTrigger, *Response, error) {
  87. project, err := parseID(pid)
  88. if err != nil {
  89. return nil, nil, err
  90. }
  91. u := fmt.Sprintf("projects/%s/triggers", url.QueryEscape(project))
  92. req, err := s.client.NewRequest("POST", u, opt, options)
  93. if err != nil {
  94. return nil, nil, err
  95. }
  96. pt := new(PipelineTrigger)
  97. resp, err := s.client.Do(req, pt)
  98. if err != nil {
  99. return nil, resp, err
  100. }
  101. return pt, resp, err
  102. }
  103. // EditPipelineTriggerOptions represents the available EditPipelineTrigger() options.
  104. //
  105. // GitLab API docs:
  106. // https://docs.gitlab.com/ce/api/pipeline_triggers.html#update-a-project-trigger
  107. type EditPipelineTriggerOptions struct {
  108. Description *string `url:"description,omitempty" json:"description,omitempty"`
  109. }
  110. // EditPipelineTrigger edits a trigger for a specified project.
  111. //
  112. // GitLab API docs:
  113. // https://docs.gitlab.com/ce/api/pipeline_triggers.html#update-a-project-trigger
  114. func (s *PipelineTriggersService) EditPipelineTrigger(pid interface{}, trigger int, opt *EditPipelineTriggerOptions, options ...OptionFunc) (*PipelineTrigger, *Response, error) {
  115. project, err := parseID(pid)
  116. if err != nil {
  117. return nil, nil, err
  118. }
  119. u := fmt.Sprintf("projects/%s/triggers/%d", url.QueryEscape(project), trigger)
  120. req, err := s.client.NewRequest("PUT", u, opt, options)
  121. if err != nil {
  122. return nil, nil, err
  123. }
  124. pt := new(PipelineTrigger)
  125. resp, err := s.client.Do(req, pt)
  126. if err != nil {
  127. return nil, resp, err
  128. }
  129. return pt, resp, err
  130. }
  131. // TakeOwnershipOfPipelineTrigger sets the owner of the specified
  132. // pipeline trigger to the user issuing the request.
  133. //
  134. // GitLab API docs:
  135. // https://docs.gitlab.com/ce/api/pipeline_triggers.html#take-ownership-of-a-project-trigger
  136. func (s *PipelineTriggersService) TakeOwnershipOfPipelineTrigger(pid interface{}, trigger int, options ...OptionFunc) (*PipelineTrigger, *Response, error) {
  137. project, err := parseID(pid)
  138. if err != nil {
  139. return nil, nil, err
  140. }
  141. u := fmt.Sprintf("projects/%s/triggers/%d/take_ownership", url.QueryEscape(project), trigger)
  142. req, err := s.client.NewRequest("POST", u, nil, options)
  143. if err != nil {
  144. return nil, nil, err
  145. }
  146. pt := new(PipelineTrigger)
  147. resp, err := s.client.Do(req, pt)
  148. if err != nil {
  149. return nil, resp, err
  150. }
  151. return pt, resp, err
  152. }
  153. // DeletePipelineTrigger removes a trigger from a project.
  154. //
  155. // GitLab API docs:
  156. // https://docs.gitlab.com/ce/api/pipeline_triggers.html#remove-a-project-trigger
  157. func (s *PipelineTriggersService) DeletePipelineTrigger(pid interface{}, trigger int, options ...OptionFunc) (*Response, error) {
  158. project, err := parseID(pid)
  159. if err != nil {
  160. return nil, err
  161. }
  162. u := fmt.Sprintf("projects/%s/triggers/%d", url.QueryEscape(project), trigger)
  163. req, err := s.client.NewRequest("DELETE", u, nil, options)
  164. if err != nil {
  165. return nil, err
  166. }
  167. return s.client.Do(req, nil)
  168. }
  169. // RunPipelineTriggerOptions represents the available RunPipelineTrigger() options.
  170. //
  171. // GitLab API docs:
  172. // https://docs.gitlab.com/ce/ci/triggers/README.html#triggering-a-pipeline
  173. type RunPipelineTriggerOptions struct {
  174. Ref *string `url:"ref" json:"ref"`
  175. Token *string `url:"token" json:"token"`
  176. Variables map[string]string `url:"variables,omitempty" json:"variables,omitempty"`
  177. }
  178. // RunPipelineTrigger starts a trigger from a project.
  179. //
  180. // GitLab API docs:
  181. // https://docs.gitlab.com/ce/ci/triggers/README.html#triggering-a-pipeline
  182. func (s *PipelineTriggersService) RunPipelineTrigger(pid interface{}, opt *RunPipelineTriggerOptions, options ...OptionFunc) (*Pipeline, *Response, error) {
  183. project, err := parseID(pid)
  184. if err != nil {
  185. return nil, nil, err
  186. }
  187. u := fmt.Sprintf("projects/%s/trigger/pipeline", url.QueryEscape(project))
  188. req, err := s.client.NewRequest("POST", u, opt, options)
  189. if err != nil {
  190. return nil, nil, err
  191. }
  192. pt := new(Pipeline)
  193. resp, err := s.client.Do(req, pt)
  194. if err != nil {
  195. return nil, resp, err
  196. }
  197. return pt, resp, err
  198. }