@@ -13,6 +13,7 @@ import (
13
13
"github.com/gitploy-io/gitploy/ent/deployment"
14
14
"github.com/gitploy-io/gitploy/ent/event"
15
15
gb "github.com/gitploy-io/gitploy/internal/server/global"
16
+ "github.com/gitploy-io/gitploy/pkg/e"
16
17
"github.com/gitploy-io/gitploy/vo"
17
18
)
18
19
@@ -88,23 +89,25 @@ func (r *Repo) CreateDeployment(c *gin.Context) {
88
89
re := vr .(* ent.Repo )
89
90
90
91
cf , err := r .i .GetConfig (ctx , u , re )
91
- if vo .IsConfigNotFoundError (err ) || vo .IsConfigParseError (err ) {
92
- r .log .Warn ("The configuration is invalid." , zap .Error (err ))
93
- gb .ErrorResponse (c , http .StatusUnprocessableEntity , "The configuration is invalid." )
92
+ if e .HasErrorCode (err , e .ErrorCodeConfigNotFound ) {
93
+ r .log .Error ("The configuration file is not found." , zap .Error (err ))
94
+ // To override the HTTP status 422.
95
+ gb .ResponseWithStatusAndError (c , http .StatusUnprocessableEntity , err )
94
96
return
95
97
} else if err != nil {
96
- r .log .Error ("failed to get the configuration file." , zap . Error ( err ) )
97
- gb .ErrorResponse (c , http . StatusInternalServerError , "It has failed to get the configuraton file." )
98
+ r .log .Error ("It has failed to get the configuration." )
99
+ gb .ResponseWithError (c , err )
98
100
return
99
101
}
100
102
101
- if ! cf .HasEnv (p .Env ) {
102
- r .log .Warn ("The environment is not defined in the config." , zap .Error (err ))
103
- gb .ErrorResponse (c , http .StatusUnprocessableEntity , "The environment is not defined in the config." )
103
+ var env * vo.Env
104
+ if env = cf .GetEnv (p .Env ); env == nil {
105
+ r .log .Warn ("The environment is not defined in the configuration." , zap .Error (err ))
106
+ gb .ErrorResponse (c , http .StatusUnprocessableEntity , "The environment is not defined in the configuration." )
104
107
return
105
108
}
106
109
107
- if err := cf . GetEnv ( p . Env ) .Eval (& vo.EvalValues {}); err != nil {
110
+ if err := env .Eval (& vo.EvalValues {}); err != nil {
108
111
r .log .Warn ("It has failed to eval variables in the config." , zap .Error (err ))
109
112
gb .ErrorResponse (c , http .StatusUnprocessableEntity , "It has failed to eval variables in the config." )
110
113
return
@@ -124,7 +127,6 @@ func (r *Repo) CreateDeployment(c *gin.Context) {
124
127
return
125
128
}
126
129
127
- // Dispatch the event.
128
130
if _ , err := r .i .CreateEvent (ctx , & ent.Event {
129
131
Kind : event .KindDeployment ,
130
132
Type : event .TypeCreated ,
@@ -169,54 +171,34 @@ func (r *Repo) UpdateDeployment(c *gin.Context) {
169
171
}
170
172
171
173
cf , err := r .i .GetConfig (ctx , u , re )
172
- if vo .IsConfigNotFoundError (err ) || vo .IsConfigParseError (err ) {
173
- r .log .Warn ("The configuration is invalid." , zap .Error (err ))
174
- gb .ErrorResponse (c , http .StatusUnprocessableEntity , "The configuration is invalid." )
174
+ if e .HasErrorCode (err , e .ErrorCodeConfigNotFound ) {
175
+ r .log .Error ("The configuration file is not found." , zap .Error (err ))
176
+ // To override the HTTP status 422.
177
+ gb .ResponseWithStatusAndError (c , http .StatusUnprocessableEntity , err )
175
178
return
176
179
} else if err != nil {
177
- r .log .Error ("failed to get the configuration file." , zap . Error ( err ) )
178
- gb .ErrorResponse (c , http . StatusInternalServerError , "It has failed to get the configuraton file." )
180
+ r .log .Error ("It has failed to get the configuration." )
181
+ gb .ResponseWithError (c , err )
179
182
return
180
183
}
181
184
182
- if ! cf .HasEnv (d .Env ) {
185
+ var env * vo.Env
186
+ if env = cf .GetEnv (d .Env ); env == nil {
183
187
r .log .Warn ("The environment is not defined in the config." , zap .Error (err ))
184
188
gb .ErrorResponse (c , http .StatusUnprocessableEntity , "The environment is not defined in the config." )
185
189
return
186
190
}
187
191
188
- if err := cf . GetEnv ( d . Env ) .Eval (& vo.EvalValues {IsRollback : d .IsRollback }); err != nil {
192
+ if err := env .Eval (& vo.EvalValues {IsRollback : d .IsRollback }); err != nil {
189
193
r .log .Warn ("It has failed to eval variables in the config." , zap .Error (err ))
190
194
gb .ErrorResponse (c , http .StatusUnprocessableEntity , "It has failed to eval variables in the config." )
191
195
return
192
196
}
193
197
194
- if locked , err := r .i .HasLockOfRepoForEnv (ctx , re , d .Env ); locked {
195
- r .log .Info ("The environment is locked." , zap .String ("env" , d .Env ))
196
- gb .ErrorResponse (c , http .StatusUnprocessableEntity , "The environment is locked." )
197
- return
198
- } else if err != nil {
199
- r .log .Error ("It has failed to check the lock." , zap .Error (err ))
200
- gb .ErrorResponse (c , http .StatusInternalServerError , "It has failed to check the lock." )
201
- return
202
- }
203
-
204
198
if p .Status == string (deployment .StatusCreated ) && d .Status == deployment .StatusWaiting {
205
- // Check the deployment is approved:
206
- // Approved >= Required Approval Count
207
- if ! r .i .IsApproved (ctx , d ) {
208
- r .log .Warn ("The deployment is not approved yet." , zap .Int ("deployment_id" , d .ID ))
209
- gb .ErrorResponse (c , http .StatusUnprocessableEntity , "It is not approved yet." )
210
- return
211
- }
212
-
213
- if d , err = r .i .CreateRemoteDeployment (ctx , u , re , d , cf .GetEnv (d .Env )); vo .IsUnprocessibleDeploymentError (err ) {
214
- r .log .Warn ("It is unprocessible entity." , zap .Error (err ))
215
- gb .ErrorResponse (c , http .StatusUnprocessableEntity , "There is a merge conflict or the commit's status checks failed." )
216
- return
217
- } else if err != nil {
218
- r .log .Error ("It has failed to create the remote deployment." , zap .Error (err ))
219
- gb .ErrorResponse (c , http .StatusInternalServerError , "It has failed to create the remote deployment." )
199
+ if d , err = r .i .DeployToRemote (ctx , u , re , d , env ); err != nil {
200
+ r .log .Error ("It has failed to deploy to the remote." , zap .Error (err ))
201
+ gb .ResponseWithError (c , err )
220
202
return
221
203
}
222
204
@@ -258,23 +240,25 @@ func (r *Repo) RollbackDeployment(c *gin.Context) {
258
240
}
259
241
260
242
cf , err := r .i .GetConfig (ctx , u , re )
261
- if vo .IsConfigNotFoundError (err ) || vo .IsConfigParseError (err ) {
262
- r .log .Warn ("The configuration is invalid." , zap .Error (err ))
263
- gb .ErrorResponse (c , http .StatusUnprocessableEntity , "The configuration is invalid." )
243
+ if e .HasErrorCode (err , e .ErrorCodeConfigNotFound ) {
244
+ r .log .Error ("The configuration file is not found." , zap .Error (err ))
245
+ // To override the HTTP status 422.
246
+ gb .ResponseWithStatusAndError (c , http .StatusUnprocessableEntity , err )
264
247
return
265
248
} else if err != nil {
266
- r .log .Error ("failed to get the configuration file." , zap . Error ( err ) )
267
- gb .ErrorResponse (c , http . StatusInternalServerError , "It has failed to get the configuraton file." )
249
+ r .log .Error ("It has failed to get the configuration." )
250
+ gb .ResponseWithError (c , err )
268
251
return
269
252
}
270
253
271
- if ! cf .HasEnv (d .Env ) {
254
+ var env * vo.Env
255
+ if env = cf .GetEnv (d .Env ); env == nil {
272
256
r .log .Warn ("The environment is not defined in the configuration." , zap .Error (err ))
273
257
gb .ErrorResponse (c , http .StatusUnprocessableEntity , "The environment is not defined in the configuration." )
274
258
return
275
259
}
276
260
277
- if err := cf . GetEnv ( d . Env ) .Eval (& vo.EvalValues {IsRollback : true }); err != nil {
261
+ if err := env .Eval (& vo.EvalValues {IsRollback : true }); err != nil {
278
262
r .log .Warn ("It has failed to eval variables in the config." , zap .Error (err ))
279
263
gb .ErrorResponse (c , http .StatusUnprocessableEntity , "It has failed to eval variables in the config." )
280
264
return
@@ -410,17 +394,9 @@ func (r *Repo) GetConfig(c *gin.Context) {
410
394
ctx := c .Request .Context ()
411
395
412
396
config , err := r .i .GetConfig (ctx , u , re )
413
- if vo .IsConfigNotFoundError (err ) {
414
- r .log .Warn ("failed to find the config file." , zap .Error (err ))
415
- gb .ErrorResponse (c , http .StatusNotFound , "It has failed to find the configuraton file." )
416
- return
417
- } else if vo .IsConfigParseError (err ) {
418
- r .log .Warn ("failed to parse the config." , zap .Error (err ))
419
- gb .ErrorResponse (c , http .StatusUnprocessableEntity , "It has failed to parse the configuraton file." )
420
- return
421
- } else if err != nil {
422
- r .log .Error ("failed to get the config file." , zap .Error (err ))
423
- gb .ErrorResponse (c , http .StatusInternalServerError , "It has failed to get the config file." )
397
+ if err != nil {
398
+ r .log .Error ("It has failed to get the configuration." , zap .Error (err ))
399
+ gb .ResponseWithError (c , err )
424
400
return
425
401
}
426
402
0 commit comments