本站源代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

230 lines
8.0KB

  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. var deployments []*Deployment
  68. resp, err := s.client.Do(ctx, req, &deployments)
  69. if err != nil {
  70. return nil, resp, err
  71. }
  72. return deployments, resp, nil
  73. }
  74. // GetDeployment returns a single deployment of a repository.
  75. //
  76. // GitHub API docs: https://developer.github.com/v3/repos/deployments/#get-a-single-deployment
  77. func (s *RepositoriesService) GetDeployment(ctx context.Context, owner, repo string, deploymentID int64) (*Deployment, *Response, error) {
  78. u := fmt.Sprintf("repos/%v/%v/deployments/%v", owner, repo, deploymentID)
  79. req, err := s.client.NewRequest("GET", u, nil)
  80. if err != nil {
  81. return nil, nil, err
  82. }
  83. deployment := new(Deployment)
  84. resp, err := s.client.Do(ctx, req, deployment)
  85. if err != nil {
  86. return nil, resp, err
  87. }
  88. return deployment, resp, nil
  89. }
  90. // CreateDeployment creates a new deployment for a repository.
  91. //
  92. // GitHub API docs: https://developer.github.com/v3/repos/deployments/#create-a-deployment
  93. func (s *RepositoriesService) CreateDeployment(ctx context.Context, owner, repo string, request *DeploymentRequest) (*Deployment, *Response, error) {
  94. u := fmt.Sprintf("repos/%v/%v/deployments", owner, repo)
  95. req, err := s.client.NewRequest("POST", u, request)
  96. if err != nil {
  97. return nil, nil, err
  98. }
  99. // TODO: remove custom Accept headers when APIs fully launch.
  100. acceptHeaders := []string{mediaTypeDeploymentStatusPreview, mediaTypeExpandDeploymentStatusPreview}
  101. req.Header.Set("Accept", strings.Join(acceptHeaders, ", "))
  102. d := new(Deployment)
  103. resp, err := s.client.Do(ctx, req, d)
  104. if err != nil {
  105. return nil, resp, err
  106. }
  107. return d, resp, nil
  108. }
  109. // DeploymentStatus represents the status of a
  110. // particular deployment.
  111. type DeploymentStatus struct {
  112. ID *int64 `json:"id,omitempty"`
  113. // State is the deployment state.
  114. // Possible values are: "pending", "success", "failure", "error", "inactive".
  115. State *string `json:"state,omitempty"`
  116. Creator *User `json:"creator,omitempty"`
  117. Description *string `json:"description,omitempty"`
  118. TargetURL *string `json:"target_url,omitempty"`
  119. CreatedAt *Timestamp `json:"created_at,omitempty"`
  120. UpdatedAt *Timestamp `json:"updated_at,omitempty"`
  121. DeploymentURL *string `json:"deployment_url,omitempty"`
  122. RepositoryURL *string `json:"repository_url,omitempty"`
  123. NodeID *string `json:"node_id,omitempty"`
  124. }
  125. // DeploymentStatusRequest represents a deployment request
  126. type DeploymentStatusRequest struct {
  127. State *string `json:"state,omitempty"`
  128. LogURL *string `json:"log_url,omitempty"`
  129. Description *string `json:"description,omitempty"`
  130. Environment *string `json:"environment,omitempty"`
  131. EnvironmentURL *string `json:"environment_url,omitempty"`
  132. AutoInactive *bool `json:"auto_inactive,omitempty"`
  133. }
  134. // ListDeploymentStatuses lists the statuses of a given deployment of a repository.
  135. //
  136. // GitHub API docs: https://developer.github.com/v3/repos/deployments/#list-deployment-statuses
  137. func (s *RepositoriesService) ListDeploymentStatuses(ctx context.Context, owner, repo string, deployment int64, opt *ListOptions) ([]*DeploymentStatus, *Response, error) {
  138. u := fmt.Sprintf("repos/%v/%v/deployments/%v/statuses", owner, repo, deployment)
  139. u, err := addOptions(u, opt)
  140. if err != nil {
  141. return nil, nil, err
  142. }
  143. req, err := s.client.NewRequest("GET", u, nil)
  144. if err != nil {
  145. return nil, nil, err
  146. }
  147. var statuses []*DeploymentStatus
  148. resp, err := s.client.Do(ctx, req, &statuses)
  149. if err != nil {
  150. return nil, resp, err
  151. }
  152. return statuses, resp, nil
  153. }
  154. // GetDeploymentStatus returns a single deployment status of a repository.
  155. //
  156. // GitHub API docs: https://developer.github.com/v3/repos/deployments/#get-a-single-deployment-status
  157. func (s *RepositoriesService) GetDeploymentStatus(ctx context.Context, owner, repo string, deploymentID, deploymentStatusID int64) (*DeploymentStatus, *Response, error) {
  158. u := fmt.Sprintf("repos/%v/%v/deployments/%v/statuses/%v", owner, repo, deploymentID, deploymentStatusID)
  159. req, err := s.client.NewRequest("GET", u, nil)
  160. if err != nil {
  161. return nil, nil, err
  162. }
  163. // TODO: remove custom Accept headers when APIs fully launch.
  164. acceptHeaders := []string{mediaTypeDeploymentStatusPreview, mediaTypeExpandDeploymentStatusPreview}
  165. req.Header.Set("Accept", strings.Join(acceptHeaders, ", "))
  166. d := new(DeploymentStatus)
  167. resp, err := s.client.Do(ctx, req, d)
  168. if err != nil {
  169. return nil, resp, err
  170. }
  171. return d, resp, nil
  172. }
  173. // CreateDeploymentStatus creates a new status for a deployment.
  174. //
  175. // GitHub API docs: https://developer.github.com/v3/repos/deployments/#create-a-deployment-status
  176. func (s *RepositoriesService) CreateDeploymentStatus(ctx context.Context, owner, repo string, deployment int64, request *DeploymentStatusRequest) (*DeploymentStatus, *Response, error) {
  177. u := fmt.Sprintf("repos/%v/%v/deployments/%v/statuses", owner, repo, deployment)
  178. req, err := s.client.NewRequest("POST", u, request)
  179. if err != nil {
  180. return nil, nil, err
  181. }
  182. // TODO: remove custom Accept headers when APIs fully launch.
  183. acceptHeaders := []string{mediaTypeDeploymentStatusPreview, mediaTypeExpandDeploymentStatusPreview}
  184. req.Header.Set("Accept", strings.Join(acceptHeaders, ", "))
  185. d := new(DeploymentStatus)
  186. resp, err := s.client.Do(ctx, req, d)
  187. if err != nil {
  188. return nil, resp, err
  189. }
  190. return d, resp, nil
  191. }
上海开阖软件有限公司 沪ICP备12045867号-1