deployments.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. //
  2. // Copyright 2018, Sander van Harmelen
  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. package gitlab
  16. import (
  17. "fmt"
  18. "net/url"
  19. "time"
  20. )
  21. // DeploymentsService handles communication with the deployment related methods
  22. // of the GitLab API.
  23. //
  24. // GitLab API docs: https://docs.gitlab.com/ce/api/deployments.html
  25. type DeploymentsService struct {
  26. client *Client
  27. }
  28. // Deployment represents the Gitlab deployment
  29. type Deployment struct {
  30. ID int `json:"id"`
  31. IID int `json:"iid"`
  32. Ref string `json:"ref"`
  33. Sha string `json:"sha"`
  34. CreatedAt *time.Time `json:"created_at"`
  35. User *ProjectUser `json:"user"`
  36. Environment *Environment `json:"environment"`
  37. Deployable struct {
  38. ID int `json:"id"`
  39. Status string `json:"status"`
  40. Stage string `json:"stage"`
  41. Name string `json:"name"`
  42. Ref string `json:"ref"`
  43. Tag bool `json:"tag"`
  44. Coverage float64 `json:"coverage"`
  45. CreatedAt *time.Time `json:"created_at"`
  46. StartedAt *time.Time `json:"started_at"`
  47. FinishedAt *time.Time `json:"finished_at"`
  48. Duration float64 `json:"duration"`
  49. User *User `json:"user"`
  50. Commit *Commit `json:"commit"`
  51. Pipeline struct {
  52. ID int `json:"id"`
  53. Sha string `json:"sha"`
  54. Ref string `json:"ref"`
  55. Status string `json:"status"`
  56. } `json:"pipeline"`
  57. Runner *Runner `json:"runner"`
  58. } `json:"deployable"`
  59. }
  60. // ListProjectDeploymentsOptions represents the available ListProjectDeployments() options.
  61. //
  62. // GitLab API docs:
  63. // https://docs.gitlab.com/ce/api/deployments.html#list-project-deployments
  64. type ListProjectDeploymentsOptions struct {
  65. ListOptions
  66. OrderBy *string `url:"order_by,omitempty" json:"order_by,omitempty"`
  67. Sort *string `url:"sort,omitempty" json:"sort,omitempty"`
  68. }
  69. // ListProjectDeployments gets a list of deployments in a project.
  70. //
  71. // GitLab API docs: https://docs.gitlab.com/ce/api/deployments.html#list-project-deployments
  72. func (s *DeploymentsService) ListProjectDeployments(pid interface{}, opts *ListProjectDeploymentsOptions, options ...OptionFunc) ([]*Deployment, *Response, error) {
  73. project, err := parseID(pid)
  74. if err != nil {
  75. return nil, nil, err
  76. }
  77. u := fmt.Sprintf("projects/%s/deployments", url.QueryEscape(project))
  78. req, err := s.client.NewRequest("GET", u, opts, options)
  79. if err != nil {
  80. return nil, nil, err
  81. }
  82. var ds []*Deployment
  83. resp, err := s.client.Do(req, &ds)
  84. if err != nil {
  85. return nil, resp, err
  86. }
  87. return ds, resp, err
  88. }
  89. // GetProjectDeployment get a deployment for a project.
  90. //
  91. // GitLab API docs: https://docs.gitlab.com/ce/api/deployments.html#get-a-specific-deployment
  92. func (s *DeploymentsService) GetProjectDeployment(pid interface{}, deployment int, options ...OptionFunc) (*Deployment, *Response, error) {
  93. project, err := parseID(pid)
  94. if err != nil {
  95. return nil, nil, err
  96. }
  97. u := fmt.Sprintf("projects/%s/deployments/%d", url.QueryEscape(project), deployment)
  98. req, err := s.client.NewRequest("GET", u, nil, options)
  99. if err != nil {
  100. return nil, nil, err
  101. }
  102. d := new(Deployment)
  103. resp, err := s.client.Do(req, d)
  104. if err != nil {
  105. return nil, resp, err
  106. }
  107. return d, resp, err
  108. }