forked from drone/go-scm
-
Notifications
You must be signed in to change notification settings - Fork 83
/
deploy.go
225 lines (203 loc) · 8.42 KB
/
deploy.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package github
import (
"context"
"fmt"
"io"
"strconv"
"strings"
"time"
"github.com/pkg/errors"
"github.com/jenkins-x/go-scm/scm"
)
type deploymentService struct {
client *wrapper
}
type deployment struct {
Namespace string
Name string
FullName string
ID int `json:"id"`
Link string `json:"url"`
Sha string `json:"sha"`
Ref string `json:"ref"`
Task string `json:"task"`
Description string `json:"description"`
OriginalEnvironment string `json:"original_environment"`
Environment string `json:"environment"`
EnvironmentURL string `json:"environment_url"`
RepositoryLink string `json:"repository_url"`
StatusLink string `json:"statuses_url"`
Author *user `json:"creator"`
Created time.Time `json:"created_at"`
Updated time.Time `json:"updated_at"`
TransientEnvironment bool `json:"transient_environment"`
ProductionEnvironment bool `json:"production_environment"`
Payload interface{} `json:"payload"`
}
type deploymentInput struct {
Ref string `json:"ref,omitempty"`
Task string `json:"task,omitempty"`
Payload string `json:"payload,omitempty"`
Environment string `json:"environment,omitempty"`
Description string `json:"description,omitempty"`
RequiredContexts []string `json:"required_contexts,omitempty"`
AutoMerge bool `json:"auto_merge"`
TransientEnvironment bool `json:"transient_environment"`
ProductionEnvironment bool `json:"production_environment"`
}
type deploymentStatus struct {
ID int `json:"id"`
State string `json:"state"`
Author *user `json:"creator"`
Description string `json:"description"`
Environment string `json:"environment"`
DeploymentLink string `json:"deployment_url"`
EnvironmentLink string `json:"environment_url"`
LogLink string `json:"log_url"`
RepositoryLink string `json:"repository_url"`
TargetLink string `json:"target_url"`
Created time.Time `json:"created_at"`
Updated time.Time `json:"updated_at"`
}
type deploymentStatusInput struct {
State string `json:"state"`
TargetLink string `json:"target_url"`
LogLink string `json:"log_url"`
Description string `json:"description"`
Environment string `json:"environment"`
EnvironmentLink string `json:"environment_url"`
AutoInactive bool `json:"auto_inactive"`
}
func (s *deploymentService) Find(ctx context.Context, repoFullName, deploymentID string) (*scm.Deployment, *scm.Response, error) {
path := fmt.Sprintf("repos/%s/deployments/%s", repoFullName, deploymentID)
out := new(deployment)
res, err := s.client.do(ctx, "GET", path, nil, out)
return convertDeployment(out, repoFullName), res, wrapError(res, err)
}
func (s *deploymentService) List(ctx context.Context, repoFullName string, opts *scm.ListOptions) ([]*scm.Deployment, *scm.Response, error) {
path := fmt.Sprintf("repos/%s/deployments?%s", repoFullName, encodeListOptions(opts))
out := []*deployment{}
res, err := s.client.do(ctx, "GET", path, nil, &out)
return convertDeploymentList(out, repoFullName), res, wrapError(res, err)
}
func (s *deploymentService) Create(ctx context.Context, repoFullName string, deploymentInput *scm.DeploymentInput) (*scm.Deployment, *scm.Response, error) {
path := fmt.Sprintf("repos/%s/deployments", repoFullName)
in := convertToDeploymentInput(deploymentInput)
out := new(deployment)
res, err := s.client.do(ctx, "POST", path, in, out)
return convertDeployment(out, repoFullName), res, wrapError(res, err)
}
func (s *deploymentService) Delete(ctx context.Context, repoFullName, deploymentID string) (*scm.Response, error) {
path := fmt.Sprintf("repos/%s/deployments/%s", repoFullName, deploymentID)
return s.client.do(ctx, "DELETE", path, nil, nil)
}
func (s *deploymentService) FindStatus(ctx context.Context, repoFullName, deploymentID, statusID string) (*scm.DeploymentStatus, *scm.Response, error) {
path := fmt.Sprintf("repos/%s/deployments/%s/statuses/%s", repoFullName, deploymentID, statusID)
out := new(deploymentStatus)
res, err := s.client.do(ctx, "GET", path, nil, out)
return convertDeploymentStatus(out), res, wrapError(res, err)
}
func (s *deploymentService) ListStatus(ctx context.Context, repoFullName, deploymentID string, opts *scm.ListOptions) ([]*scm.DeploymentStatus, *scm.Response, error) {
path := fmt.Sprintf("repos/%s/deployments/%s/statuses?%s", repoFullName, deploymentID, encodeListOptions(opts))
out := []*deploymentStatus{}
res, err := s.client.do(ctx, "GET", path, nil, &out)
return convertDeploymentStatusList(out), res, wrapError(res, err)
}
func (s *deploymentService) CreateStatus(ctx context.Context, repoFullName, deploymentID string, deploymentStatusInput *scm.DeploymentStatusInput) (*scm.DeploymentStatus, *scm.Response, error) {
path := fmt.Sprintf("repos/%s/deployments/%s/statuses", repoFullName, deploymentID)
in := convertToDeploymentStatusInput(deploymentStatusInput)
out := new(deploymentStatus)
res, err := s.client.do(ctx, "POST", path, in, out)
return convertDeploymentStatus(out), res, wrapError(res, err)
}
func wrapError(res *scm.Response, err error) error {
if res == nil {
return err
}
data, err2 := io.ReadAll(res.Body)
if err2 != nil {
return errors.Wrapf(err, "http status %d", res.Status)
}
return errors.Wrapf(err, "http status %d mesage %s", res.Status, string(data))
}
func convertDeploymentList(out []*deployment, fullName string) []*scm.Deployment {
answer := []*scm.Deployment{}
for _, o := range out {
answer = append(answer, convertDeployment(o, fullName))
}
return answer
}
func convertDeploymentStatusList(out []*deploymentStatus) []*scm.DeploymentStatus {
answer := []*scm.DeploymentStatus{}
for _, o := range out {
answer = append(answer, convertDeploymentStatus(o))
}
return answer
}
func convertToDeploymentInput(from *scm.DeploymentInput) *deploymentInput {
return &deploymentInput{
Ref: from.Ref,
Task: from.Task,
Payload: from.Payload,
Environment: from.Environment,
Description: from.Description,
RequiredContexts: from.RequiredContexts,
AutoMerge: from.AutoMerge,
TransientEnvironment: from.TransientEnvironment,
ProductionEnvironment: from.ProductionEnvironment,
}
}
func convertDeployment(from *deployment, fullName string) *scm.Deployment {
dst := &scm.Deployment{
ID: strconv.Itoa(from.ID),
Link: from.Link,
Sha: from.Sha,
Ref: from.Ref,
FullName: fullName,
Task: from.Task,
Description: from.Description,
OriginalEnvironment: from.OriginalEnvironment,
Environment: from.Environment,
RepositoryLink: from.RepositoryLink,
StatusLink: from.StatusLink,
Author: convertUser(from.Author),
Created: from.Created,
Updated: from.Updated,
TransientEnvironment: from.TransientEnvironment,
ProductionEnvironment: from.ProductionEnvironment,
Payload: from.Payload,
}
names := strings.Split(fullName, "/")
if len(names) > 1 {
dst.Namespace = names[0]
dst.Name = names[1]
}
return dst
}
func convertDeploymentStatus(from *deploymentStatus) *scm.DeploymentStatus {
return &scm.DeploymentStatus{
ID: strconv.Itoa(from.ID),
State: from.State,
Author: convertUser(from.Author),
Description: from.Description,
Environment: from.Environment,
DeploymentLink: from.DeploymentLink,
EnvironmentLink: from.EnvironmentLink,
LogLink: from.LogLink,
RepositoryLink: from.RepositoryLink,
TargetLink: from.TargetLink,
Created: from.Created,
Updated: from.Updated,
}
}
func convertToDeploymentStatusInput(from *scm.DeploymentStatusInput) *deploymentStatusInput {
return &deploymentStatusInput{
State: from.State,
TargetLink: from.TargetLink,
LogLink: from.LogLink,
Description: from.Description,
Environment: from.Environment,
EnvironmentLink: from.EnvironmentLink,
AutoInactive: from.AutoInactive,
}
}