@@ -14,11 +14,15 @@ import (
1414 "go.uber.org/zap"
1515)
1616
17+ type (
18+ DeploymentsInteractor service
19+ )
20+
1721// IsApproved verifies that the request is approved or not.
1822// It is approved if there is an approval of reviews at least, but
1923// 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 )
2226
2327 for _ , r := range rvs {
2428 if r .Status == review .StatusRejected {
@@ -39,12 +43,12 @@ func (i *Interactor) IsApproved(ctx context.Context, d *ent.Deployment) bool {
3943// But if it requires a review, it saves the payload on the DB
4044// and waits until reviewed.
4145// 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 ) {
4347 if err := i .isDeployable (ctx , u , r , d , env ); err != nil {
4448 return nil , err
4549 }
4650
47- number , err := i .Store .GetNextDeploymentNumberOfRepo (ctx , r )
51+ number , err := i .store .GetNextDeploymentNumberOfRepo (ctx , r )
4852 if err != nil {
4953 return nil , e .NewError (
5054 e .ErrorCodeInternalError ,
@@ -66,7 +70,7 @@ func (i *Interactor) Deploy(ctx context.Context, u *ent.User, r *ent.Repo, d *en
6670 }
6771
6872 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 )
7074 if err != nil {
7175 return nil , err
7276 }
@@ -107,12 +111,12 @@ func (i *Interactor) Deploy(ctx context.Context, u *ent.User, r *ent.Repo, d *en
107111 }
108112
109113 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 )
111115 if err != nil {
112116 return nil , fmt .Errorf ("It failed to save a new deployment.: %w" , err )
113117 }
114118
115- i .CreateDeploymentStatus (ctx , & ent.DeploymentStatus {
119+ i .store . CreateDeploymentStatus (ctx , & ent.DeploymentStatus {
116120 Status : string (deployment .StatusCreated ),
117121 Description : "Gitploy starts to deploy." ,
118122 DeploymentID : d .ID ,
@@ -124,7 +128,7 @@ func (i *Interactor) Deploy(ctx context.Context, u *ent.User, r *ent.Repo, d *en
124128// DeployToRemote posts a new deployment to SCM with the saved payload
125129// after review has finished.
126130// 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 ) {
128132 if d .Status != deployment .StatusWaiting {
129133 return nil , e .NewErrorWithMessage (
130134 e .ErrorCodeDeploymentStatusInvalid ,
@@ -155,11 +159,11 @@ func (i *Interactor) DeployToRemote(ctx context.Context, u *ent.User, r *ent.Rep
155159 d .HTMLURL = rd .HTLMURL
156160 d .Status = deployment .StatusCreated
157161
158- if d , err = i .UpdateDeployment (ctx , d ); err != nil {
162+ if d , err = i .store . UpdateDeployment (ctx , d ); err != nil {
159163 return nil , err
160164 }
161165
162- i .CreateDeploymentStatus (ctx , & ent.DeploymentStatus {
166+ i .store . CreateDeploymentStatus (ctx , & ent.DeploymentStatus {
163167 Status : string (deployment .StatusCreated ),
164168 Description : "Gitploy creates a new deployment." ,
165169 DeploymentID : d .ID ,
@@ -168,7 +172,7 @@ func (i *Interactor) DeployToRemote(ctx context.Context, u *ent.User, r *ent.Rep
168172 return d , nil
169173}
170174
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 ) {
172176 // Rollback configures it can deploy the ref without any constraints.
173177 // 1) Set auto_merge false to avoid the merge conflict.
174178 // 2) Set required_contexts empty to skip the verfication.
@@ -177,10 +181,10 @@ func (i *Interactor) createRemoteDeployment(ctx context.Context, u *ent.User, r
177181 env .RequiredContexts = & []string {}
178182 }
179183
180- return i .SCM .CreateRemoteDeployment (ctx , u , r , d , env )
184+ return i .scm .CreateRemoteDeployment (ctx , u , r , d , env )
181185}
182186
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 {
184188 // Skip verifications for roll back.
185189 if ! d .IsRollback {
186190 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,
191195 }
192196
193197 // 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 {
195199 return e .NewError (e .ErrorCodeDeploymentLocked , err )
196200 } else if err != nil {
197201 return err
@@ -206,7 +210,7 @@ func (i *Interactor) isDeployable(ctx context.Context, u *ent.User, r *ent.Repo,
206210 return nil
207211}
208212
209- func (i * Interactor ) runClosingInactiveDeployment (stop <- chan struct {}) {
213+ func (i * DeploymentsInteractor ) runClosingInactiveDeployment (stop <- chan struct {}) {
210214 ctx := context .Background ()
211215
212216 ticker := time .NewTicker (time .Minute )
219223 break L
220224 }
221225 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 )
223227 if err != nil {
224228 i .log .Error ("It has failed to read inactive deployments." , zap .Error (err ))
225229 continue
235239 Description : "Gitploy cancels the inactive deployment." ,
236240 DeploymentID : d .ID ,
237241 }
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 {
239243 i .log .Error ("It has failed to cancel the remote deployment." , zap .Error (err ))
240244 continue
241245 }
242246
243- if _ , err := i .Store .CreateDeploymentStatus (ctx , s ); err != nil {
247+ if _ , err := i .store .CreateDeploymentStatus (ctx , s ); err != nil {
244248 i .log .Error ("It has failed to create a new deployment status." , zap .Error (err ))
245249 continue
246250 }
247251 }
248252 }
249253
250254 d .Status = deployment .StatusCanceled
251- if _ , err := i .UpdateDeployment (ctx , d ); err != nil {
255+ if _ , err := i .store . UpdateDeployment (ctx , d ); err != nil {
252256 i .log .Error ("It has failed to update the deployment canceled." , zap .Error (err ))
253257 }
254258
0 commit comments