@@ -13,6 +13,7 @@ import (
1313 "github.com/gitploy-io/gitploy/ent/deployment"
1414 "github.com/gitploy-io/gitploy/ent/event"
1515 gb "github.com/gitploy-io/gitploy/internal/server/global"
16+ "github.com/gitploy-io/gitploy/pkg/e"
1617 "github.com/gitploy-io/gitploy/vo"
1718)
1819
@@ -88,23 +89,25 @@ func (r *Repo) CreateDeployment(c *gin.Context) {
8889 re := vr .(* ent.Repo )
8990
9091 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 )
9496 return
9597 } 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 )
98100 return
99101 }
100102
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." )
104107 return
105108 }
106109
107- if err := cf . GetEnv ( p . Env ) .Eval (& vo.EvalValues {}); err != nil {
110+ if err := env .Eval (& vo.EvalValues {}); err != nil {
108111 r .log .Warn ("It has failed to eval variables in the config." , zap .Error (err ))
109112 gb .ErrorResponse (c , http .StatusUnprocessableEntity , "It has failed to eval variables in the config." )
110113 return
@@ -124,7 +127,6 @@ func (r *Repo) CreateDeployment(c *gin.Context) {
124127 return
125128 }
126129
127- // Dispatch the event.
128130 if _ , err := r .i .CreateEvent (ctx , & ent.Event {
129131 Kind : event .KindDeployment ,
130132 Type : event .TypeCreated ,
@@ -169,54 +171,34 @@ func (r *Repo) UpdateDeployment(c *gin.Context) {
169171 }
170172
171173 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 )
175178 return
176179 } 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 )
179182 return
180183 }
181184
182- if ! cf .HasEnv (d .Env ) {
185+ var env * vo.Env
186+ if env = cf .GetEnv (d .Env ); env == nil {
183187 r .log .Warn ("The environment is not defined in the config." , zap .Error (err ))
184188 gb .ErrorResponse (c , http .StatusUnprocessableEntity , "The environment is not defined in the config." )
185189 return
186190 }
187191
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 {
189193 r .log .Warn ("It has failed to eval variables in the config." , zap .Error (err ))
190194 gb .ErrorResponse (c , http .StatusUnprocessableEntity , "It has failed to eval variables in the config." )
191195 return
192196 }
193197
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-
204198 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 )
220202 return
221203 }
222204
@@ -258,23 +240,25 @@ func (r *Repo) RollbackDeployment(c *gin.Context) {
258240 }
259241
260242 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 )
264247 return
265248 } 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 )
268251 return
269252 }
270253
271- if ! cf .HasEnv (d .Env ) {
254+ var env * vo.Env
255+ if env = cf .GetEnv (d .Env ); env == nil {
272256 r .log .Warn ("The environment is not defined in the configuration." , zap .Error (err ))
273257 gb .ErrorResponse (c , http .StatusUnprocessableEntity , "The environment is not defined in the configuration." )
274258 return
275259 }
276260
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 {
278262 r .log .Warn ("It has failed to eval variables in the config." , zap .Error (err ))
279263 gb .ErrorResponse (c , http .StatusUnprocessableEntity , "It has failed to eval variables in the config." )
280264 return
@@ -410,17 +394,9 @@ func (r *Repo) GetConfig(c *gin.Context) {
410394 ctx := c .Request .Context ()
411395
412396 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 )
424400 return
425401 }
426402
0 commit comments