forked from rcarmstrong/go-bamboo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deploys.go
208 lines (168 loc) · 6.15 KB
/
deploys.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package bamboo
import (
"fmt"
"net/http"
)
// DeployService handles communication with the deploy related methods
type DeployService service
// DeployResponse is the REST response from the server
type DeployResponse struct {
*ResourceMetadata
}
// DeploysResponse is a collection of Deploy elements
type DeploysResponse = []*Deploy
// Deploy is a single Deploy definition
type Deploy struct {
ID int `json:"id"`
PlanKey *PlanKey `json:"planKey,omitempty"`
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
Environments []*DeployEnvironment `json:"environments,omitempty"`
}
// DeployEnvironment is the information for an environment
type DeployEnvironment struct {
ID int `json:"id"`
Name string `json:"name"`
Description string `json:"description,omitempty"`
DeploymentProjectID int `json:"deploymentProjectId,omitempty"`
}
// DeployEnvironmentResults is the information for a single Deploy
type DeployEnvironmentResults struct {
Name string `json:"name"`
ID int `json:"id"`
Results []*DeployStatus `json:"results"`
}
// DeploymentVersion contains version information for a deployment
type DeploymentVersion struct {
Name string `json:"name"`
ID int `json:"id"`
}
// QueueDeployRequest contains information from a queue deploy request
type QueueDeployRequest struct {
DeploymentResultID int `json:"deploymentResultId"`
Link *Link `json:"link"`
}
// DeployStatus contains deploy status information
type DeployStatus struct {
DeploymentVersion *DeploymentVersion `json:"deploymentVersion"`
DeploymentVersionName string `json:"deploymentVersionName"`
DeploymentState string `json:"deploymentState"`
LifeCycleState string `json:"lifeCycleState"`
StartedDate int `json:"startedDate"`
}
type createDeploymentVersion struct {
PlanResultKey string `json:"planResultKey"`
Name string `json:"name"`
NextVersionName string `json:"nextVersionName"`
}
// DeployVersionResult will have the information for creating a
// new release/version for bamboo
type DeployVersionResult struct {
ID int `json:"id"`
Name string `json:"name"`
}
// DeployVersionListResult stores a list of deployment versions
type DeployVersionListResult struct {
Versions []*DeployVersionResult `json:"versions"`
}
// CreateDeployVersion will take a deploy project id, plan result, version name and the next version name and create a release.
func (d *DeployService) CreateDeployVersion(deploymentProjectID int, planResultKey, versionName, nextVersionName string) (*DeployVersionResult, error) {
createDeployment := &createDeploymentVersion{
PlanResultKey: planResultKey,
Name: versionName,
NextVersionName: nextVersionName,
}
request, err := d.client.NewRequest(http.MethodPost, fmt.Sprintf("deploy/project/%d/version", deploymentProjectID), createDeployment)
if err != nil {
return nil, err
}
result := &DeployVersionResult{}
response, err := d.client.Do(request, result)
if err != nil {
return nil, err
}
if response.StatusCode != http.StatusOK {
return nil, newRespErr(response, "Error creating deploy version")
}
return result, nil
}
// ListDeploys lists all deployments
func (d *DeployService) ListDeploys() (DeploysResponse, error) {
request, err := d.client.NewRequest(http.MethodGet, "deploy/project/all", nil)
if err != nil {
return nil, err
}
deployResp := DeploysResponse{}
response, err := d.client.Do(request, &deployResp)
if err != nil {
return nil, err
}
if response.StatusCode != http.StatusOK {
return nil, newRespErr(response, "Error listing deploys")
}
return deployResp, nil
}
// DeployEnvironments returns information on the requested environment
func (d *DeployService) DeployEnvironments(id int) (*DeployEnvironment, error) {
request, err := d.client.NewRequest(http.MethodGet, fmt.Sprintf("deploy/project/%d", id), nil)
if err != nil {
return nil, err
}
deployEnvironmentResp := &DeployEnvironment{}
response, err := d.client.Do(request, &deployEnvironmentResp)
if err != nil {
return nil, err
}
if response.StatusCode != http.StatusOK {
return nil, newRespErr(response, "Error getting environments")
}
return deployEnvironmentResp, nil
}
// DeployEnvironmentResults returns result information for the requested environment
func (d *DeployService) DeployEnvironmentResults(id int) (*DeployEnvironmentResults, error) {
request, err := d.client.NewRequest(http.MethodGet, fmt.Sprintf("deploy/environment/%d/results", id), nil)
if err != nil {
return nil, err
}
deployEnvironmentResultsResp := &DeployEnvironmentResults{}
response, err := d.client.Do(request, &deployEnvironmentResultsResp)
if err != nil {
return nil, err
}
if response.StatusCode != http.StatusOK {
return nil, newRespErr(response, "Error getting deploy results")
}
return deployEnvironmentResultsResp, nil
}
// QueueDeploy adds a deploy of the specified version to the given environment.
func (d *DeployService) QueueDeploy(environmentID, versionID int) (*QueueDeployRequest, error) {
request, err := d.client.NewRequest(http.MethodPost, fmt.Sprintf("queue/deployment/?environmentId=%d&versionId=%d", environmentID, versionID), nil)
if err != nil {
return nil, err
}
queueDeployRequest := &QueueDeployRequest{}
response, err := d.client.Do(request, &queueDeployRequest)
if err != nil {
return nil, err
}
if response.StatusCode != http.StatusOK {
return nil, newRespErr(response, "Error queueing deploy")
}
return queueDeployRequest, nil
}
// DeployStatus returns information on the requested deploy
func (d *DeployService) DeployStatus(id int) (*DeployStatus, error) {
request, err := d.client.NewRequest(http.MethodGet, fmt.Sprintf("deploy/result/%d", id), nil)
if err != nil {
return nil, err
}
deployStatus := &DeployStatus{}
response, err := d.client.Do(request, &deployStatus)
if err != nil {
return nil, err
}
if response.StatusCode != http.StatusOK {
return nil, newRespErr(response, "Error getting deploy status")
}
return deployStatus, nil
}