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

Commit cc02b63

Browse files
author
Noah Lee
authored
Refactoring the deployment interactor (#350)
* Add 'GetEvaluatedConfig' method * Refactoring review method * Create an event in the interactor
1 parent e672d1a commit cc02b63

17 files changed

+181
-147
lines changed

internal/interactor/_mock.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ mockgen \
22
-aux_files \
33
github.com/gitploy-io/gitploy/internal/interactor=user.go\
44
,github.com/gitploy-io/gitploy/internal/interactor=repo.go\
5+
,github.com/gitploy-io/gitploy/internal/interactor=config.go\
56
,github.com/gitploy-io/gitploy/internal/interactor=deployment.go\
67
,github.com/gitploy-io/gitploy/internal/interactor=deploymentstatistics.go\
78
,github.com/gitploy-io/gitploy/internal/interactor=lock.go\

internal/interactor/config.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package interactor
2+
3+
import (
4+
"context"
5+
6+
"github.com/gitploy-io/gitploy/model/ent"
7+
"github.com/gitploy-io/gitploy/model/extent"
8+
)
9+
10+
type (
11+
ConfigInteractor service
12+
13+
ConfigSCM interface {
14+
GetConfig(ctx context.Context, u *ent.User, r *ent.Repo) (*extent.Config, error)
15+
GetConfigRedirectURL(ctx context.Context, u *ent.User, r *ent.Repo) (string, error)
16+
GetNewConfigRedirectURL(ctx context.Context, u *ent.User, r *ent.Repo) (string, error)
17+
}
18+
)
19+
20+
// GetEvaluatedConfig returns the config after evaluating the variables.
21+
func (i *ConfigInteractor) GetEvaluatedConfig(ctx context.Context, u *ent.User, r *ent.Repo, v *extent.EvalValues) (*extent.Config, error) {
22+
config, err := i.scm.GetConfig(ctx, u, r)
23+
if err != nil {
24+
return nil, err
25+
}
26+
27+
if err = config.Eval(v); err != nil {
28+
return nil, err
29+
}
30+
31+
return config, nil
32+
}

internal/interactor/config_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package interactor_test
2+
3+
import (
4+
"context"
5+
"testing"
6+
7+
"github.com/golang/mock/gomock"
8+
9+
i "github.com/gitploy-io/gitploy/internal/interactor"
10+
"github.com/gitploy-io/gitploy/internal/interactor/mock"
11+
"github.com/gitploy-io/gitploy/model/ent"
12+
"github.com/gitploy-io/gitploy/model/extent"
13+
)
14+
15+
func TestConfigInteractor_GetEvaluatedConfig(t *testing.T) {
16+
t.Run("Return the evaluated config", func(t *testing.T) {
17+
t.Log("Start mocking: ")
18+
ctrl := gomock.NewController(t)
19+
scm := mock.NewMockSCM(ctrl)
20+
21+
t.Log("\tGet the config.")
22+
scm.EXPECT().
23+
GetConfig(gomock.Any(), gomock.AssignableToTypeOf(&ent.User{}), gomock.AssignableToTypeOf(&ent.Repo{})).
24+
Return(&extent.Config{}, nil)
25+
26+
it := i.NewInteractor(&i.InteractorConfig{
27+
Store: mock.NewMockStore(ctrl),
28+
SCM: scm,
29+
})
30+
31+
_, err := it.GetEvaluatedConfig(context.Background(), &ent.User{}, &ent.Repo{}, &extent.EvalValues{})
32+
if err != nil {
33+
t.Fatalf("GetEvaluatedConfig returns an error: %v", err)
34+
}
35+
})
36+
}

internal/interactor/deployment.go

Lines changed: 60 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/AlekSi/pointer"
99
"github.com/gitploy-io/gitploy/model/ent"
1010
"github.com/gitploy-io/gitploy/model/ent/deployment"
11+
"github.com/gitploy-io/gitploy/model/ent/event"
1112
"github.com/gitploy-io/gitploy/model/extent"
1213
"github.com/gitploy-io/gitploy/pkg/e"
1314
"go.uber.org/zap"
@@ -63,10 +64,6 @@ type (
6364
// SCM returns the deployment with UID and SHA.
6465
CreateRemoteDeployment(ctx context.Context, u *ent.User, r *ent.Repo, d *ent.Deployment, e *extent.Env) (*extent.RemoteDeployment, error)
6566
CancelDeployment(ctx context.Context, u *ent.User, r *ent.Repo, d *ent.Deployment, s *ent.DeploymentStatus) error
66-
67-
GetConfig(ctx context.Context, u *ent.User, r *ent.Repo) (*extent.Config, error)
68-
GetConfigRedirectURL(ctx context.Context, u *ent.User, r *ent.Repo) (string, error)
69-
GetNewConfigRedirectURL(ctx context.Context, u *ent.User, r *ent.Repo) (string, error)
7067
}
7168
)
7269

@@ -108,17 +105,20 @@ func (i *DeploymentInteractor) Deploy(ctx context.Context, u *ent.User, r *ent.R
108105
return nil, err
109106
}
110107

111-
for _, rvr := range env.Review.Reviewers {
112-
if rvr == u.Login {
113-
continue
114-
}
115-
116-
i.log.Debug(fmt.Sprintf("Request a review to %s.", rvr))
117-
if _, err := i.requestReviewByLogin(ctx, d, rvr); err != nil {
118-
i.log.Error("Failed to request the review.", zap.Error(err))
119-
}
108+
i.log.Debug("Request reviews.")
109+
errs := i.requestReviews(ctx, u, d, env)
110+
if len(errs) != 0 {
111+
i.log.Error("Failed to request a review.", zap.Errors("errs", errs))
120112
}
121113

114+
i.log.Debug("Dispatch a event.")
115+
if _, err := i.store.CreateEvent(ctx, &ent.Event{
116+
Kind: event.KindDeployment,
117+
Type: event.TypeCreated,
118+
DeploymentID: d.ID,
119+
}); err != nil {
120+
i.log.Error("Failed to create the event.", zap.Error(err))
121+
}
122122
return d, nil
123123
}
124124

@@ -145,9 +145,56 @@ func (i *DeploymentInteractor) Deploy(ctx context.Context, u *ent.User, r *ent.R
145145
DeploymentID: d.ID,
146146
})
147147

148+
i.log.Debug("Dispatch a event.")
149+
if _, err := i.store.CreateEvent(ctx, &ent.Event{
150+
Kind: event.KindDeployment,
151+
Type: event.TypeCreated,
152+
DeploymentID: d.ID,
153+
}); err != nil {
154+
i.log.Error("Failed to create the event.", zap.Error(err))
155+
}
148156
return d, nil
149157
}
150158

159+
func (i *DeploymentInteractor) requestReviews(ctx context.Context, u *ent.User, d *ent.Deployment, env *extent.Env) []error {
160+
errs := make([]error, 0)
161+
162+
for _, login := range env.Review.Reviewers {
163+
if login == u.Login {
164+
i.log.Debug("Skip the deployer.")
165+
continue
166+
}
167+
168+
reviewer, err := i.store.FindUserByLogin(ctx, login)
169+
if err != nil {
170+
i.log.Error("Failed to find the reviewer.", zap.Error(err))
171+
errs = append(errs, err)
172+
continue
173+
}
174+
175+
r, err := i.store.CreateReview(ctx, &ent.Review{
176+
DeploymentID: d.ID,
177+
UserID: reviewer.ID,
178+
})
179+
if err != nil {
180+
i.log.Error("Failed to create a review.", zap.Error(err))
181+
errs = append(errs, err)
182+
continue
183+
}
184+
185+
if _, err := i.store.CreateEvent(ctx, &ent.Event{
186+
Kind: event.KindReview,
187+
Type: event.TypeCreated,
188+
ReviewID: r.ID,
189+
}); err != nil {
190+
i.log.Error("Failed to create a review event.", zap.Error(err))
191+
errs = append(errs, err)
192+
}
193+
}
194+
195+
return errs
196+
}
197+
151198
// DeployToRemote posts a new deployment to SCM with the saved payload
152199
// after review has finished.
153200
// It returns an error for a undeployable payload.

internal/interactor/deployment_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ func TestInteractor_Deploy(t *testing.T) {
5050
return d, nil
5151
})
5252

53+
store.
54+
EXPECT().
55+
CreateEvent(gomock.Any(), gomock.AssignableToTypeOf(&ent.Event{})).
56+
Return(&ent.Event{}, nil)
57+
5358
store.
5459
EXPECT().
5560
CreateDeploymentStatus(ctx, gomock.AssignableToTypeOf(&ent.DeploymentStatus{}))
@@ -94,6 +99,11 @@ func TestInteractor_Deploy(t *testing.T) {
9499
return d, nil
95100
})
96101

102+
store.
103+
EXPECT().
104+
CreateEvent(gomock.Any(), gomock.AssignableToTypeOf(&ent.Event{})).
105+
Return(&ent.Event{}, nil)
106+
97107
it := i.NewInteractor(&i.InteractorConfig{
98108
Store: store,
99109
SCM: scm,

internal/interactor/interactor.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ type (
1818
common *service
1919

2020
// services used for talking to different parts of the entities.
21+
*ConfigInteractor
2122
*DeploymentInteractor
2223
*DeploymentStatisticsInteractor
2324
*EventInteractor
@@ -67,6 +68,7 @@ func NewInteractor(c *InteractorConfig) *Interactor {
6768
log: log,
6869
}
6970

71+
i.ConfigInteractor = (*ConfigInteractor)(i.common)
7072
i.DeploymentInteractor = (*DeploymentInteractor)(i.common)
7173
i.DeploymentStatisticsInteractor = (*DeploymentStatisticsInteractor)(i.common)
7274
i.EventInteractor = &EventInteractor{

internal/interactor/interface.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ type (
6262
RepoSCM
6363
DeploymentSCM
6464
DeploymentStatusSCM
65+
ConfigSCM
6566
CommitSCM
6667
BranchSCM
6768
TagSCM

internal/interactor/review.go

Lines changed: 0 additions & 41 deletions
This file was deleted.

internal/interactor/review_oss.go

Lines changed: 0 additions & 14 deletions
This file was deleted.

0 commit comments

Comments
 (0)