Skip to content
This repository has been archived by the owner on May 30, 2024. It is now read-only.

Commit

Permalink
Fix to evaluation to avoid an escape character error (#250)
Browse files Browse the repository at this point in the history
* Add a method Eval to Config

* Fix to evaluate the configuration in the server
  • Loading branch information
Noah Lee authored Nov 23, 2021
1 parent 07a24b4 commit 27656de
Show file tree
Hide file tree
Showing 14 changed files with 212 additions and 278 deletions.
22 changes: 0 additions & 22 deletions internal/interactor/config.go

This file was deleted.

57 changes: 0 additions & 57 deletions internal/interactor/config_test.go

This file was deleted.

8 changes: 0 additions & 8 deletions internal/interactor/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,6 @@ func (i *Interactor) Deploy(ctx context.Context, u *ent.User, r *ent.Repo, d *en
return nil, err
}

if err := env.Eval(&vo.EvalValues{IsRollback: d.IsRollback}); err != nil {
return nil, err
}

number, err := i.Store.GetNextDeploymentNumberOfRepo(ctx, r)
if err != nil {
return nil, e.NewError(
Expand Down Expand Up @@ -139,10 +135,6 @@ func (i *Interactor) DeployToRemote(ctx context.Context, u *ent.User, r *ent.Rep
)
}

if err := env.Eval(&vo.EvalValues{IsRollback: d.IsRollback}); err != nil {
return nil, err
}

rd, err := i.createRemoteDeployment(ctx, u, r, d, env)
if err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion internal/interactor/deployment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ func TestInteractor_DeployToRemote(t *testing.T) {
}
})

t.Run("create a new remote deployment and update the deployment.", func(t *testing.T) {
t.Run("Create a new remote deployment and update the deployment.", func(t *testing.T) {
input := struct {
d *ent.Deployment
e *vo.Env
Expand Down
64 changes: 45 additions & 19 deletions internal/server/api/v1/repos/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,23 @@ func (r *Repo) CreateDeployment(c *gin.Context) {
vr, _ := c.Get(KeyRepo)
re := vr.(*ent.Repo)

env, err := r.i.GetEnv(ctx, u, re, p.Env)
config, err := r.i.GetConfig(ctx, u, re)
if e.HasErrorCode(err, e.ErrorCodeEntityNotFound) {
r.log.Check(gb.GetZapLogLevel(err), "The configuration file is not found.").Write(zap.Error(err))
// To override the HTTP status 422.
r.log.Check(gb.GetZapLogLevel(err), "Failed to get the configuration.").Write(zap.Error(err))
gb.ResponseWithStatusAndError(c, http.StatusUnprocessableEntity, err)
return
} else if err != nil {
r.log.Check(gb.GetZapLogLevel(err), "It has failed to get the configuration.").Write(zap.Error(err))
gb.ResponseWithError(c, err)
}

if err := config.Eval(&vo.EvalValues{}); err != nil {
r.log.Check(gb.GetZapLogLevel(err), "Failed to evaluate the configuration.").Write(zap.Error(err))
gb.ResponseWithStatusAndError(c, http.StatusUnprocessableEntity, err)
return
}

var env *vo.Env
if env = config.GetEnv(p.Env); env == nil {
r.log.Warn("The environment is not found.", zap.String("env", p.Env))
gb.ResponseWithStatusAndError(c, http.StatusUnprocessableEntity, e.NewError(e.ErrorCodeConfigUndefinedEnv, nil))
return
}

Expand All @@ -110,7 +118,6 @@ func (r *Repo) CreateDeployment(c *gin.Context) {
return
}

// TODO: Migrate the event logic into the interactor.
if _, err := r.i.CreateEvent(ctx, &ent.Event{
Kind: event.KindDeployment,
Type: event.TypeCreated,
Expand All @@ -124,6 +131,7 @@ func (r *Repo) CreateDeployment(c *gin.Context) {
d = de
}

r.log.Info("Start to deploy.", zap.String("repo", re.GetFullName()), zap.String("env", p.Env))
gb.Response(c, http.StatusCreated, d)
}

Expand All @@ -149,15 +157,23 @@ func (r *Repo) UpdateDeployment(c *gin.Context) {
return
}

env, err := r.i.GetEnv(ctx, u, re, d.Env)
config, err := r.i.GetConfig(ctx, u, re)
if e.HasErrorCode(err, e.ErrorCodeEntityNotFound) {
r.log.Check(gb.GetZapLogLevel(err), "The configuration file is not found.").Write(zap.Error(err))
// To override the HTTP status 422.
r.log.Check(gb.GetZapLogLevel(err), "Failed to get the configuration.").Write(zap.Error(err))
gb.ResponseWithStatusAndError(c, http.StatusUnprocessableEntity, err)
return
} else if err != nil {
r.log.Check(gb.GetZapLogLevel(err), "It has failed to get the configuration.").Write(zap.Error(err))
gb.ResponseWithError(c, err)
}

if err := config.Eval(&vo.EvalValues{IsRollback: d.IsRollback}); err != nil {
r.log.Check(gb.GetZapLogLevel(err), "Failed to evaludate the configuration.").Write(zap.Error(err))
gb.ResponseWithStatusAndError(c, http.StatusUnprocessableEntity, err)
return
}

var env *vo.Env
if env = config.GetEnv(d.Env); env == nil {
r.log.Warn("The environment is not found.", zap.String("env", d.Env))
gb.ResponseWithStatusAndError(c, http.StatusUnprocessableEntity, e.NewError(e.ErrorCodeConfigUndefinedEnv, nil))
return
}

Expand All @@ -180,6 +196,7 @@ func (r *Repo) UpdateDeployment(c *gin.Context) {
d = de
}

r.log.Info("Start to deploy.", zap.String("repo", re.GetFullName()), zap.Int("number", d.Number))
gb.Response(c, http.StatusOK, d)
}

Expand All @@ -203,15 +220,23 @@ func (r *Repo) RollbackDeployment(c *gin.Context) {
return
}

env, err := r.i.GetEnv(ctx, u, re, d.Env)
config, err := r.i.GetConfig(ctx, u, re)
if e.HasErrorCode(err, e.ErrorCodeEntityNotFound) {
r.log.Check(gb.GetZapLogLevel(err), "The configuration file is not found.").Write(zap.Error(err))
// To override the HTTP status 422.
r.log.Check(gb.GetZapLogLevel(err), "Failed to get the configuration.").Write(zap.Error(err))
gb.ResponseWithStatusAndError(c, http.StatusUnprocessableEntity, err)
return
} else if err != nil {
r.log.Check(gb.GetZapLogLevel(err), "It has failed to get the configuration.").Write(zap.Error(err))
gb.ResponseWithError(c, err)
}

if err := config.Eval(&vo.EvalValues{IsRollback: true}); err != nil {
r.log.Check(gb.GetZapLogLevel(err), "Failed to evaludate the configuration.").Write(zap.Error(err))
gb.ResponseWithStatusAndError(c, http.StatusUnprocessableEntity, err)
return
}

var env *vo.Env
if env = config.GetEnv(d.Env); env == nil {
r.log.Warn("The environment is not found.", zap.String("env", d.Env))
gb.ResponseWithStatusAndError(c, http.StatusUnprocessableEntity, e.NewError(e.ErrorCodeConfigUndefinedEnv, nil))
return
}

Expand Down Expand Up @@ -242,6 +267,7 @@ func (r *Repo) RollbackDeployment(c *gin.Context) {
d = de
}

r.log.Info("Start to rollback.", zap.String("repo", re.GetFullName()), zap.Int("number", d.Number))
gb.Response(c, http.StatusCreated, d)
}

Expand Down
12 changes: 7 additions & 5 deletions internal/server/api/v1/repos/deployment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,15 @@ func TestRepo_CreateDeployment(t *testing.T) {
ctrl := gomock.NewController(t)
m := mock.NewMockInteractor(ctrl)

t.Log("Read the config file.")
m.
EXPECT().
GetEnv(gomock.Any(), gomock.AssignableToTypeOf(&ent.User{}), gomock.AssignableToTypeOf(&ent.Repo{}), gomock.Any()).
Return(&vo.Env{
Name: "prod",
}, nil)
GetConfig(gomock.Any(), gomock.AssignableToTypeOf(&ent.User{}), gomock.AssignableToTypeOf(&ent.Repo{})).
Return(&vo.Config{
Envs: []*vo.Env{
{
Name: "prod",
},
}}, nil)

t.Log("Deploy with the payload successfully.")
m.
Expand Down
1 change: 0 additions & 1 deletion internal/server/api/v1/repos/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ type (
Deploy(ctx context.Context, u *ent.User, re *ent.Repo, d *ent.Deployment, env *vo.Env) (*ent.Deployment, error)
DeployToRemote(ctx context.Context, u *ent.User, r *ent.Repo, d *ent.Deployment, env *vo.Env) (*ent.Deployment, error)
GetConfig(ctx context.Context, u *ent.User, r *ent.Repo) (*vo.Config, error)
GetEnv(ctx context.Context, u *ent.User, r *ent.Repo, env string) (*vo.Env, error)

ListReviews(ctx context.Context, d *ent.Deployment) ([]*ent.Review, error)
FindReviewOfUser(ctx context.Context, u *ent.User, d *ent.Deployment) (*ent.Review, error)
Expand Down
15 changes: 9 additions & 6 deletions internal/server/api/v1/repos/lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/gitploy-io/gitploy/internal/server/global"
gb "github.com/gitploy-io/gitploy/internal/server/global"
"github.com/gitploy-io/gitploy/pkg/e"
"github.com/gitploy-io/gitploy/vo"
)

type (
Expand Down Expand Up @@ -84,15 +85,17 @@ func (r *Repo) CreateLock(c *gin.Context) {
vu, _ := c.Get(global.KeyUser)
u := vu.(*ent.User)

env, err := r.i.GetEnv(ctx, u, re, p.Env)
config, err := r.i.GetConfig(ctx, u, re)
if e.HasErrorCode(err, e.ErrorCodeEntityNotFound) {
r.log.Check(gb.GetZapLogLevel(err), "The configuration file is not found.").Write(zap.Error(err))
// To override the HTTP status 422.
r.log.Check(gb.GetZapLogLevel(err), "Failed to get the configuration.").Write(zap.Error(err))
gb.ResponseWithStatusAndError(c, http.StatusUnprocessableEntity, err)
return
} else if err != nil {
r.log.Check(gb.GetZapLogLevel(err), "It has failed to get the configuration.").Write(zap.Error(err))
gb.ResponseWithError(c, err)
}

var env *vo.Env
if env = config.GetEnv(p.Env); env == nil {
r.log.Warn("The environment is not found.", zap.String("env", p.Env))
gb.ResponseWithStatusAndError(c, http.StatusUnprocessableEntity, e.NewError(e.ErrorCodeConfigUndefinedEnv, nil))
return
}

Expand Down
23 changes: 14 additions & 9 deletions internal/server/api/v1/repos/lock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"github.com/gitploy-io/gitploy/ent"
"github.com/gitploy-io/gitploy/internal/server/api/v1/repos/mock"
"github.com/gitploy-io/gitploy/internal/server/global"
"github.com/gitploy-io/gitploy/pkg/e"
"github.com/gitploy-io/gitploy/vo"
)

Expand All @@ -34,11 +33,15 @@ func TestRepo_CreateLock(t *testing.T) {
ctrl := gomock.NewController(t)
m := mock.NewMockInteractor(ctrl)

t.Log("Read deploy.yml and check the env.")
m.
EXPECT().
GetEnv(gomock.Any(), gomock.AssignableToTypeOf(&ent.User{}), gomock.AssignableToTypeOf(&ent.Repo{}), gomock.Any()).
Return(nil, e.NewError(e.ErrorCodeConfigUndefinedEnv, nil))
GetConfig(gomock.Any(), gomock.AssignableToTypeOf(&ent.User{}), gomock.AssignableToTypeOf(&ent.Repo{})).
Return(&vo.Config{
Envs: []*vo.Env{
{
Name: "dev",
},
}}, nil)

r := NewRepo(RepoConfig{}, m)

Expand Down Expand Up @@ -72,13 +75,15 @@ func TestRepo_CreateLock(t *testing.T) {
ctrl := gomock.NewController(t)
m := mock.NewMockInteractor(ctrl)

t.Log("Read deploy.yml and check the env.")
m.
EXPECT().
GetEnv(gomock.Any(), gomock.AssignableToTypeOf(&ent.User{}), gomock.AssignableToTypeOf(&ent.Repo{}), gomock.Any()).
Return(&vo.Env{
Name: "production",
}, nil)
GetConfig(gomock.Any(), gomock.AssignableToTypeOf(&ent.User{}), gomock.AssignableToTypeOf(&ent.Repo{})).
Return(&vo.Config{
Envs: []*vo.Env{
{
Name: "production",
},
}}, nil)

t.Log("Lock the env.")
m.
Expand Down
15 changes: 0 additions & 15 deletions internal/server/api/v1/repos/mock/interactor.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 8 additions & 2 deletions internal/server/slack/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,15 +273,21 @@ func (s *Slack) interactDeploy(c *gin.Context) {
return
}

cfg, err := s.i.GetConfig(ctx, cu.Edges.User, cb.Edges.Repo)
config, err := s.i.GetConfig(ctx, cu.Edges.User, cb.Edges.Repo)
if err != nil {
postMessageWithError(cu, err)
c.Status(http.StatusOK)
return
}

if err := config.Eval(&vo.EvalValues{}); err != nil {
postMessageWithError(cu, err)
c.Status(http.StatusOK)
return
}

var env *vo.Env
if env = cfg.GetEnv(sm.Env); env == nil {
if env = config.GetEnv(sm.Env); env == nil {
postBotMessage(cu, "The env is not defined in the config.")
c.Status(http.StatusOK)
return
Expand Down
Loading

0 comments on commit 27656de

Please sign in to comment.