repos_deployments.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. // Copyright 2014 The go-github AUTHORS. All rights reserved.
  2. //
  3. // Use of this source code is governed by a BSD-style
  4. // license that can be found in the LICENSE file.
  5. package github
  6. import (
  7. "context"
  8. "encoding/json"
  9. "fmt"
  10. "strings"
  11. )
  12. // Deployment represents a deployment in a repo
  13. type Deployment struct {
  14. URL *string `json:"url,omitempty"`
  15. ID *int64 `json:"id,omitempty"`
  16. SHA *string `json:"sha,omitempty"`
  17. Ref *string `json:"ref,omitempty"`
  18. Task *string `json:"task,omitempty"`
  19. Payload json.RawMessage `json:"payload,omitempty"`
  20. Environment *string `json:"environment,omitempty"`
  21. Description *string `json:"description,omitempty"`
  22. Creator *User `json:"creator,omitempty"`
  23. CreatedAt *Timestamp `json:"created_at,omitempty"`
  24. UpdatedAt *Timestamp `json:"updated_at,omitempty"`
  25. StatusesURL *string `json:"statuses_url,omitempty"`
  26. RepositoryURL *string `json:"repository_url,omitempty"`
  27. NodeID *string `json:"node_id,omitempty"`
  28. }
  29. // DeploymentRequest represents a deployment request
  30. type DeploymentRequest struct {
  31. Ref *string `json:"ref,omitempty"`
  32. Task *string `json:"task,omitempty"`
  33. AutoMerge *bool `json:"auto_merge,omitempty"`
  34. RequiredContexts *[]string `json:"required_contexts,omitempty"`
  35. Payload *string `json:"payload,omitempty"`
  36. Environment *string `json:"environment,omitempty"`
  37. Description *string `json:"description,omitempty"`
  38. TransientEnvironment *bool `json:"transient_environment,omitempty"`
  39. ProductionEnvironment *bool `json:"production_environment,omitempty"`
  40. }
  41. // DeploymentsListOptions specifies the optional parameters to the
  42. // RepositoriesService.ListDeployments method.
  43. type DeploymentsListOptions struct {
  44. // SHA of the Deployment.
  45. SHA string `url:"sha,omitempty"`
  46. // List deployments for a given ref.
  47. Ref string `url:"ref,omitempty"`
  48. // List deployments for a given task.
  49. Task string `url:"task,omitempty"`
  50. // List deployments for a given environment.
  51. Environment string `url:"environment,omitempty"`
  52. ListOptions
  53. }
  54. // ListDeployments lists the deployments of a repository.
  55. //
  56. // GitHub API docs: https://developer.github.com/v3/repos/deployments/#list-deployments
  57. func (s *RepositoriesService) ListDeployments(ctx context.Context, owner, repo string, opt *DeploymentsListOptions) ([]*Deployment, *Response, error) {
  58. u := fmt.Sprintf("repos/%v/%v/deployments", owner, repo)
  59. u, err := addOptions(u, opt)
  60. if err != nil {
  61. return nil, nil, err
  62. }
  63. req, err := s.client.NewRequest("GET", u, nil)
  64. if err != nil {
  65. return nil, nil, err
  66. }
  67. // TODO: remove custom Accept header when this API fully launches.
  68. req.Header.Set("Accept", mediaTypeGraphQLNodeIDPreview)
  69. var deployments []*Deployment
  70. resp, err := s.client.Do(ctx, req, &deployments)
  71. if err != nil {
  72. return nil, resp, err
  73. }
  74. return deployments, resp, nil
  75. }
  76. // GetDeployment returns a single deployment of a repository.
  77. //
  78. // GitHub API docs: https://developer.github.com/v3/repos/deployments/#get-a-single-deployment
  79. func (s *RepositoriesService) GetDeployment(ctx context.Context, owner, repo string, deploymentID int64) (*Deployment, *Response, error) {
  80. u := fmt.Sprintf("repos/%v/%v/deployments/%v", owner, repo, deploymentID)
  81. req, err := s.client.NewRequest("GET", u, nil)
  82. if err != nil {
  83. return nil, nil, err
  84. }
  85. // TODO: remove custom Accept header when this API fully launches.
  86. req.Header.Set("Accept", mediaTypeGraphQLNodeIDPreview)
  87. deployment := new(Deployment)
  88. resp, err := s.client.Do(ctx, req, deployment)
  89. if err != nil {
  90. return nil, resp, err
  91. }
  92. return deployment, resp, nil
  93. }
  94. // CreateDeployment creates a new deployment for a repository.
  95. //
  96. // GitHub API docs: https://developer.github.com/v3/repos/deployments/#create-a-deployment
  97. func (s *RepositoriesService) CreateDeployment(ctx context.Context, owner, repo string, request *DeploymentRequest) (*Deployment, *Response, error) {
  98. u := fmt.Sprintf("repos/%v/%v/deployments", owner, repo)
  99. req, err := s.client.NewRequest("POST", u, request)
  100. if err != nil {
  101. return nil, nil, err
  102. }
  103. // TODO: remove custom Accept headers when APIs fully launch.
  104. acceptHeaders := []string{mediaTypeDeploymentStatusPreview, mediaTypeGraphQLNodeIDPreview}
  105. req.Header.Set("Accept", strings.Join(acceptHeaders, ", "))
  106. d := new(Deployment)
  107. resp, err := s.client.Do(ctx, req, d)
  108. if err != nil {
  109. return nil, resp, err
  110. }
  111. return d, resp, nil
  112. }
  113. // DeploymentStatus represents the status of a
  114. // particular deployment.
  115. type DeploymentStatus struct {
  116. ID *int64 `json:"id,omitempty"`
  117. // State is the deployment state.
  118. // Possible values are: "pending", "success", "failure", "error", "inactive".
  119. State *string `json:"state,omitempty"`
  120. Creator *User `json:"creator,omitempty"`
  121. Description *string `json:"description,omitempty"`
  122. TargetURL *string `json:"target_url,omitempty"`
  123. CreatedAt *Timestamp `json:"created_at,omitempty"`
  124. UpdatedAt *Timestamp `json:"updated_at,omitempty"`
  125. DeploymentURL *string `json:"deployment_url,omitempty"`
  126. RepositoryURL *string `json:"repository_url,omitempty"`
  127. NodeID *string `json:"node_id,omitempty"`
  128. }
  129. // DeploymentStatusRequest represents a deployment request
  130. type DeploymentStatusRequest struct {
  131. State *string `json:"state,omitempty"`
  132. LogURL *string `json:"log_url,omitempty"`
  133. Description *string `json:"description,omitempty"`
  134. EnvironmentURL *string `json:"environment_url,omitempty"`
  135. AutoInactive *bool `json:"auto_inactive,omitempty"`
  136. }
  137. // ListDeploymentStatuses lists the statuses of a given deployment of a repository.
  138. //
  139. // GitHub API docs: https://developer.github.com/v3/repos/deployments/#list-deployment-statuses
  140. func (s *RepositoriesService) ListDeploymentStatuses(ctx context.Context, owner, repo string, deployment int64, opt *ListOptions) ([]*DeploymentStatus, *Response, error) {
  141. u := fmt.Sprintf("repos/%v/%v/deployments/%v/statuses", owner, repo, deployment)
  142. u, err := addOptions(u, opt)
  143. if err != nil {
  144. return nil, nil, err
  145. }
  146. req, err := s.client.NewRequest("GET", u, nil)
  147. if err != nil {
  148. return nil, nil, err
  149. }
  150. // TODO: remove custom Accept header when this API fully launches.
  151. req.Header.Set("Accept", mediaTypeGraphQLNodeIDPreview)
  152. var statuses []*DeploymentStatus
  153. resp, err := s.client.Do(ctx, req, &statuses)
  154. if err != nil {
  155. return nil, resp, err
  156. }
  157. return statuses, resp, nil
  158. }
  159. // GetDeploymentStatus returns a single deployment status of a repository.
  160. //
  161. // GitHub API docs: https://developer.github.com/v3/repos/deployments/#get-a-single-deployment-status
  162. func (s *RepositoriesService) GetDeploymentStatus(ctx context.Context, owner, repo string, deploymentID, deploymentStatusID int64) (*DeploymentStatus, *Response, error) {
  163. u := fmt.Sprintf("repos/%v/%v/deployments/%v/statuses/%v", owner, repo, deploymentID, deploymentStatusID)
  164. req, err := s.client.NewRequest("GET", u, nil)
  165. if err != nil {
  166. return nil, nil, err
  167. }
  168. // TODO: remove custom Accept headers when APIs fully launch.
  169. acceptHeaders := []string{mediaTypeDeploymentStatusPreview, mediaTypeGraphQLNodeIDPreview}
  170. req.Header.Set("Accept", strings.Join(acceptHeaders, ", "))
  171. d := new(DeploymentStatus)
  172. resp, err := s.client.Do(ctx, req, d)
  173. if err != nil {
  174. return nil, resp, err
  175. }
  176. return d, resp, nil
  177. }
  178. // CreateDeploymentStatus creates a new status for a deployment.
  179. //
  180. // GitHub API docs: https://developer.github.com/v3/repos/deployments/#create-a-deployment-status
  181. func (s *RepositoriesService) CreateDeploymentStatus(ctx context.Context, owner, repo string, deployment int64, request *DeploymentStatusRequest) (*DeploymentStatus, *Response, error) {
  182. u := fmt.Sprintf("repos/%v/%v/deployments/%v/statuses", owner, repo, deployment)
  183. req, err := s.client.NewRequest("POST", u, request)
  184. if err != nil {
  185. return nil, nil, err
  186. }
  187. // TODO: remove custom Accept headers when APIs fully launch.
  188. acceptHeaders := []string{mediaTypeDeploymentStatusPreview, mediaTypeGraphQLNodeIDPreview}
  189. req.Header.Set("Accept", strings.Join(acceptHeaders, ", "))
  190. d := new(DeploymentStatus)
  191. resp, err := s.client.Do(ctx, req, d)
  192. if err != nil {
  193. return nil, resp, err
  194. }
  195. return d, resp, nil
  196. }