@@ -14,11 +14,15 @@ import (
14
14
"go.uber.org/zap"
15
15
)
16
16
17
+ type (
18
+ DeploymentsInteractor service
19
+ )
20
+
17
21
// IsApproved verifies that the request is approved or not.
18
22
// It is approved if there is an approval of reviews at least, but
19
23
// it is rejected if there is a reject of reviews.
20
- func (i * Interactor ) IsApproved (ctx context.Context , d * ent.Deployment ) bool {
21
- rvs , _ := i .Store .ListReviews (ctx , d )
24
+ func (i * DeploymentsInteractor ) IsApproved (ctx context.Context , d * ent.Deployment ) bool {
25
+ rvs , _ := i .store .ListReviews (ctx , d )
22
26
23
27
for _ , r := range rvs {
24
28
if r .Status == review .StatusRejected {
@@ -39,12 +43,12 @@ func (i *Interactor) IsApproved(ctx context.Context, d *ent.Deployment) bool {
39
43
// But if it requires a review, it saves the payload on the DB
40
44
// and waits until reviewed.
41
45
// It returns an error for a undeployable payload.
42
- func (i * Interactor ) Deploy (ctx context.Context , u * ent.User , r * ent.Repo , d * ent.Deployment , env * extent.Env ) (* ent.Deployment , error ) {
46
+ func (i * DeploymentsInteractor ) Deploy (ctx context.Context , u * ent.User , r * ent.Repo , d * ent.Deployment , env * extent.Env ) (* ent.Deployment , error ) {
43
47
if err := i .isDeployable (ctx , u , r , d , env ); err != nil {
44
48
return nil , err
45
49
}
46
50
47
- number , err := i .Store .GetNextDeploymentNumberOfRepo (ctx , r )
51
+ number , err := i .store .GetNextDeploymentNumberOfRepo (ctx , r )
48
52
if err != nil {
49
53
return nil , e .NewError (
50
54
e .ErrorCodeInternalError ,
@@ -66,7 +70,7 @@ func (i *Interactor) Deploy(ctx context.Context, u *ent.User, r *ent.Repo, d *en
66
70
}
67
71
68
72
i .log .Debug ("Save the deployment to wait reviews." )
69
- d , err = i .Store .CreateDeployment (ctx , d )
73
+ d , err = i .store .CreateDeployment (ctx , d )
70
74
if err != nil {
71
75
return nil , err
72
76
}
@@ -107,12 +111,12 @@ func (i *Interactor) Deploy(ctx context.Context, u *ent.User, r *ent.Repo, d *en
107
111
}
108
112
109
113
i .log .Debug ("Create a new deployment with the payload." , zap .Any ("deployment" , d ))
110
- d , err = i .Store .CreateDeployment (ctx , d )
114
+ d , err = i .store .CreateDeployment (ctx , d )
111
115
if err != nil {
112
116
return nil , fmt .Errorf ("It failed to save a new deployment.: %w" , err )
113
117
}
114
118
115
- i .CreateDeploymentStatus (ctx , & ent.DeploymentStatus {
119
+ i .store . CreateDeploymentStatus (ctx , & ent.DeploymentStatus {
116
120
Status : string (deployment .StatusCreated ),
117
121
Description : "Gitploy starts to deploy." ,
118
122
DeploymentID : d .ID ,
@@ -124,7 +128,7 @@ func (i *Interactor) Deploy(ctx context.Context, u *ent.User, r *ent.Repo, d *en
124
128
// DeployToRemote posts a new deployment to SCM with the saved payload
125
129
// after review has finished.
126
130
// It returns an error for a undeployable payload.
127
- func (i * Interactor ) DeployToRemote (ctx context.Context , u * ent.User , r * ent.Repo , d * ent.Deployment , env * extent.Env ) (* ent.Deployment , error ) {
131
+ func (i * DeploymentsInteractor ) DeployToRemote (ctx context.Context , u * ent.User , r * ent.Repo , d * ent.Deployment , env * extent.Env ) (* ent.Deployment , error ) {
128
132
if d .Status != deployment .StatusWaiting {
129
133
return nil , e .NewErrorWithMessage (
130
134
e .ErrorCodeDeploymentStatusInvalid ,
@@ -155,11 +159,11 @@ func (i *Interactor) DeployToRemote(ctx context.Context, u *ent.User, r *ent.Rep
155
159
d .HTMLURL = rd .HTLMURL
156
160
d .Status = deployment .StatusCreated
157
161
158
- if d , err = i .UpdateDeployment (ctx , d ); err != nil {
162
+ if d , err = i .store . UpdateDeployment (ctx , d ); err != nil {
159
163
return nil , err
160
164
}
161
165
162
- i .CreateDeploymentStatus (ctx , & ent.DeploymentStatus {
166
+ i .store . CreateDeploymentStatus (ctx , & ent.DeploymentStatus {
163
167
Status : string (deployment .StatusCreated ),
164
168
Description : "Gitploy creates a new deployment." ,
165
169
DeploymentID : d .ID ,
@@ -168,7 +172,7 @@ func (i *Interactor) DeployToRemote(ctx context.Context, u *ent.User, r *ent.Rep
168
172
return d , nil
169
173
}
170
174
171
- func (i * Interactor ) createRemoteDeployment (ctx context.Context , u * ent.User , r * ent.Repo , d * ent.Deployment , env * extent.Env ) (* extent.RemoteDeployment , error ) {
175
+ func (i * DeploymentsInteractor ) createRemoteDeployment (ctx context.Context , u * ent.User , r * ent.Repo , d * ent.Deployment , env * extent.Env ) (* extent.RemoteDeployment , error ) {
172
176
// Rollback configures it can deploy the ref without any constraints.
173
177
// 1) Set auto_merge false to avoid the merge conflict.
174
178
// 2) Set required_contexts empty to skip the verfication.
@@ -177,10 +181,10 @@ func (i *Interactor) createRemoteDeployment(ctx context.Context, u *ent.User, r
177
181
env .RequiredContexts = & []string {}
178
182
}
179
183
180
- return i .SCM .CreateRemoteDeployment (ctx , u , r , d , env )
184
+ return i .scm .CreateRemoteDeployment (ctx , u , r , d , env )
181
185
}
182
186
183
- func (i * Interactor ) isDeployable (ctx context.Context , u * ent.User , r * ent.Repo , d * ent.Deployment , env * extent.Env ) error {
187
+ func (i * DeploymentsInteractor ) isDeployable (ctx context.Context , u * ent.User , r * ent.Repo , d * ent.Deployment , env * extent.Env ) error {
184
188
// Skip verifications for roll back.
185
189
if ! d .IsRollback {
186
190
if ok , err := env .IsDeployableRef (d .Ref ); ! ok {
@@ -191,7 +195,7 @@ func (i *Interactor) isDeployable(ctx context.Context, u *ent.User, r *ent.Repo,
191
195
}
192
196
193
197
// Check that the environment is locked.
194
- if locked , err := i .Store .HasLockOfRepoForEnv (ctx , r , d .Env ); locked {
198
+ if locked , err := i .store .HasLockOfRepoForEnv (ctx , r , d .Env ); locked {
195
199
return e .NewError (e .ErrorCodeDeploymentLocked , err )
196
200
} else if err != nil {
197
201
return err
@@ -206,7 +210,7 @@ func (i *Interactor) isDeployable(ctx context.Context, u *ent.User, r *ent.Repo,
206
210
return nil
207
211
}
208
212
209
- func (i * Interactor ) runClosingInactiveDeployment (stop <- chan struct {}) {
213
+ func (i * DeploymentsInteractor ) runClosingInactiveDeployment (stop <- chan struct {}) {
210
214
ctx := context .Background ()
211
215
212
216
ticker := time .NewTicker (time .Minute )
219
223
break L
220
224
}
221
225
case t := <- ticker .C :
222
- ds , err := i .ListInactiveDeploymentsLessThanTime (ctx , t .Add (- 30 * time .Minute ).UTC (), 1 , 30 )
226
+ ds , err := i .store . ListInactiveDeploymentsLessThanTime (ctx , t .Add (- 30 * time .Minute ).UTC (), 1 , 30 )
223
227
if err != nil {
224
228
i .log .Error ("It has failed to read inactive deployments." , zap .Error (err ))
225
229
continue
235
239
Description : "Gitploy cancels the inactive deployment." ,
236
240
DeploymentID : d .ID ,
237
241
}
238
- if err := i .SCM .CancelDeployment (ctx , d .Edges .User , d .Edges .Repo , d , s ); err != nil {
242
+ if err := i .scm .CancelDeployment (ctx , d .Edges .User , d .Edges .Repo , d , s ); err != nil {
239
243
i .log .Error ("It has failed to cancel the remote deployment." , zap .Error (err ))
240
244
continue
241
245
}
242
246
243
- if _ , err := i .Store .CreateDeploymentStatus (ctx , s ); err != nil {
247
+ if _ , err := i .store .CreateDeploymentStatus (ctx , s ); err != nil {
244
248
i .log .Error ("It has failed to create a new deployment status." , zap .Error (err ))
245
249
continue
246
250
}
247
251
}
248
252
}
249
253
250
254
d .Status = deployment .StatusCanceled
251
- if _ , err := i .UpdateDeployment (ctx , d ); err != nil {
255
+ if _ , err := i .store . UpdateDeployment (ctx , d ); err != nil {
252
256
i .log .Error ("It has failed to update the deployment canceled." , zap .Error (err ))
253
257
}
254
258
0 commit comments