|
| 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 | + |
| 6 | +package github |
| 7 | + |
| 8 | +import ( |
| 9 | + "encoding/json" |
| 10 | + "fmt" |
| 11 | +) |
| 12 | + |
| 13 | +// Deployment represents a deployment in a repo |
| 14 | +type Deployment struct { |
| 15 | + Url *string `json:"url,omitempty"` |
| 16 | + ID *int `json:"id,omitempty"` |
| 17 | + SHA *string `json:"sha,omitempty"` |
| 18 | + Ref *string `json:"ref,omitempty"` |
| 19 | + Task *string `json:"task,omitempty"` |
| 20 | + Payload json.RawMessage `json:"payload,omitempty"` |
| 21 | + Environment *string `json:"environment,omitempty"` |
| 22 | + Description *string `json:"description,omitempty"` |
| 23 | + Creator *User `json:"creator,omitempty"` |
| 24 | + CreatedAt *Timestamp `json:"created_at,omitempty"` |
| 25 | + UpdatedAt *Timestamp `json:"pushed_at,omitempty"` |
| 26 | +} |
| 27 | + |
| 28 | +// DeploymentRequest represents a deployment request |
| 29 | +type DeploymentRequest struct { |
| 30 | + Ref *string `json:"ref,omitempty"` |
| 31 | + Task *string `json:"task,omitempty"` |
| 32 | + AutoMerge *bool `json:"auto_merge,omitempty"` |
| 33 | + RequiredContexts []string `json:"required_contexts,omitempty"` |
| 34 | + Payload *string `json:"payload,omitempty"` |
| 35 | + Environment *string `json:"environment,omitempty"` |
| 36 | + Description *string `json:"description,omitempty"` |
| 37 | +} |
| 38 | + |
| 39 | +// DeploymentsListOptions specifies the optional parameters to the |
| 40 | +// RepositoriesService.ListDeployments method. |
| 41 | +type DeploymentsListOptions struct { |
| 42 | + // SHA of the Deployment. |
| 43 | + SHA string `url:"sha,omitempty"` |
| 44 | + |
| 45 | + // List deployments for a given ref. |
| 46 | + Ref string `url:"ref,omitempty"` |
| 47 | + |
| 48 | + // List deployments for a given task. |
| 49 | + Task string `url:"task,omitempty"` |
| 50 | + |
| 51 | + // List deployments for a given environment. |
| 52 | + Environment string `url:"environment,omitempty"` |
| 53 | + |
| 54 | + ListOptions |
| 55 | +} |
| 56 | + |
| 57 | +// ListDeployments lists the deployments of a repository. |
| 58 | +// |
| 59 | +// GitHub API docs: https://developer.github.com/v3/repos/deployments/#list-deployments |
| 60 | +func (s *RepositoriesService) ListDeployments(owner, repo string, opt *DeploymentsListOptions) ([]Deployment, *Response, error) { |
| 61 | + u := fmt.Sprintf("repos/%v/%v/deployments", owner, repo) |
| 62 | + u, err := addOptions(u, opt) |
| 63 | + if err != nil { |
| 64 | + return nil, nil, err |
| 65 | + } |
| 66 | + |
| 67 | + req, err := s.client.NewRequest("GET", u, nil) |
| 68 | + if err != nil { |
| 69 | + return nil, nil, err |
| 70 | + } |
| 71 | + |
| 72 | + // TODO: remove custom Accept header when this API fully launches |
| 73 | + req.Header.Set("Accept", mediaTypeDeploymentPreview) |
| 74 | + |
| 75 | + deployments := new([]Deployment) |
| 76 | + resp, err := s.client.Do(req, deployments) |
| 77 | + if err != nil { |
| 78 | + return nil, resp, err |
| 79 | + } |
| 80 | + |
| 81 | + return *deployments, resp, err |
| 82 | +} |
| 83 | + |
| 84 | +// CreateDeployment creates a new deployment for a repository. |
| 85 | +// |
| 86 | +// GitHub API docs: https://developer.github.com/v3/repos/deployments/#create-a-deployment |
| 87 | +func (s *RepositoriesService) CreateDeployment(owner, repo string, request *DeploymentRequest) (*Deployment, *Response, error) { |
| 88 | + u := fmt.Sprintf("repos/%v/%v/deployments", owner, repo) |
| 89 | + |
| 90 | + req, err := s.client.NewRequest("POST", u, request) |
| 91 | + if err != nil { |
| 92 | + return nil, nil, err |
| 93 | + } |
| 94 | + |
| 95 | + // TODO: remove custom Accept header when this API fully launches |
| 96 | + req.Header.Set("Accept", mediaTypeDeploymentPreview) |
| 97 | + |
| 98 | + d := new(Deployment) |
| 99 | + resp, err := s.client.Do(req, d) |
| 100 | + if err != nil { |
| 101 | + return nil, resp, err |
| 102 | + } |
| 103 | + |
| 104 | + return d, resp, err |
| 105 | +} |
| 106 | + |
| 107 | +// DeploymentStatus represents the status of a |
| 108 | +// particular deployment. |
| 109 | +type DeploymentStatus struct { |
| 110 | + ID *int `json:"id,omitempty"` |
| 111 | + State *string `json:"state,omitempty"` |
| 112 | + Creator *User `json:"creator,omitempty"` |
| 113 | + Description *string `json:"description,omitempty"` |
| 114 | + TargetUrl *string `json:"target_url,omitempty"` |
| 115 | + CreatedAt *Timestamp `json:"created_at,omitempty"` |
| 116 | + UpdatedAt *Timestamp `json:"pushed_at,omitempty"` |
| 117 | +} |
| 118 | + |
| 119 | +// DeploymentRequest represents a deployment request |
| 120 | +type DeploymentStatusRequest struct { |
| 121 | + State *string `json:"state,omitempty"` |
| 122 | + TargetUrl *string `json:"target_url,omitempty"` |
| 123 | + Description *string `json:"description,omitempty"` |
| 124 | +} |
| 125 | + |
| 126 | +// ListDeploymentStatuses lists the statuses of a given deployment of a repository. |
| 127 | +// |
| 128 | +// GitHub API docs: https://developer.github.com/v3/repos/deployments/#list-deployment-statuses |
| 129 | +func (s *RepositoriesService) ListDeploymentStatuses(owner, repo string, deployment int, opt *ListOptions) ([]DeploymentStatus, *Response, error) { |
| 130 | + u := fmt.Sprintf("repos/%v/%v/deployments/%v/statuses", owner, repo, deployment) |
| 131 | + u, err := addOptions(u, opt) |
| 132 | + if err != nil { |
| 133 | + return nil, nil, err |
| 134 | + } |
| 135 | + |
| 136 | + req, err := s.client.NewRequest("GET", u, nil) |
| 137 | + if err != nil { |
| 138 | + return nil, nil, err |
| 139 | + } |
| 140 | + |
| 141 | + // TODO: remove custom Accept header when this API fully launches |
| 142 | + req.Header.Set("Accept", mediaTypeDeploymentPreview) |
| 143 | + |
| 144 | + statuses := new([]DeploymentStatus) |
| 145 | + resp, err := s.client.Do(req, statuses) |
| 146 | + if err != nil { |
| 147 | + return nil, resp, err |
| 148 | + } |
| 149 | + |
| 150 | + return *statuses, resp, err |
| 151 | +} |
| 152 | + |
| 153 | +// CreateDeploymentStatus creates a new status for a deployment. |
| 154 | +// |
| 155 | +// GitHub API docs: https://developer.github.com/v3/repos/deployments/#create-a-deployment-status |
| 156 | +func (s *RepositoriesService) CreateDeploymentStatus(owner, repo string, deployment int, request *DeploymentStatusRequest) (*DeploymentStatus, *Response, error) { |
| 157 | + u := fmt.Sprintf("repos/%v/%v/deployments/%v/statuses", owner, repo, deployment) |
| 158 | + |
| 159 | + req, err := s.client.NewRequest("POST", u, request) |
| 160 | + if err != nil { |
| 161 | + return nil, nil, err |
| 162 | + } |
| 163 | + |
| 164 | + // TODO: remove custom Accept header when this API fully launches |
| 165 | + req.Header.Set("Accept", mediaTypeDeploymentPreview) |
| 166 | + |
| 167 | + d := new(DeploymentStatus) |
| 168 | + resp, err := s.client.Do(req, d) |
| 169 | + if err != nil { |
| 170 | + return nil, resp, err |
| 171 | + } |
| 172 | + |
| 173 | + return d, resp, err |
| 174 | +} |
0 commit comments