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

Commit a2ccc36

Browse files
author
Noah Lee
authored
Refactorying the interactor package (#338)
1 parent 33557e2 commit a2ccc36

23 files changed

+228
-137
lines changed

internal/interactor/deployment.go

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -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)
@@ -219,7 +223,7 @@ L:
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
@@ -235,20 +239,20 @@ L:
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

internal/interactor/deployment_test.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ import (
1717
"github.com/gitploy-io/gitploy/pkg/e"
1818
)
1919

20-
func newMockInteractor(store Store, scm SCM) *Interactor {
21-
return &Interactor{
22-
Store: store,
23-
SCM: scm,
20+
func newMockDeploymentsInteractor(store Store, scm SCM) *DeploymentsInteractor {
21+
return &DeploymentsInteractor{
22+
store: store,
23+
scm: scm,
2424
log: zap.L(),
2525
}
2626
}
@@ -44,7 +44,7 @@ func TestInteractor_IsApproved(t *testing.T) {
4444
},
4545
}, nil)
4646

47-
i := newMockInteractor(store, scm)
47+
i := newMockDeploymentsInteractor(store, scm)
4848

4949
expected := false
5050
if ret := i.IsApproved(context.Background(), &ent.Deployment{}); ret != expected {
@@ -75,7 +75,7 @@ func TestInteractor_Deploy(t *testing.T) {
7575
store := mock.NewMockStore(ctrl)
7676
scm := mock.NewMockSCM(ctrl)
7777

78-
i := newMockInteractor(store, scm)
78+
i := newMockDeploymentsInteractor(store, scm)
7979

8080
_, err := i.Deploy(context.Background(), &ent.User{}, &ent.Repo{}, input.d, input.e)
8181
if !e.HasErrorCode(err, e.ErrorCodeEntityUnprocessable) {
@@ -93,7 +93,7 @@ func TestInteractor_Deploy(t *testing.T) {
9393
HasLockOfRepoForEnv(ctx, gomock.AssignableToTypeOf(&ent.Repo{}), "").
9494
Return(true, nil)
9595

96-
i := newMockInteractor(store, scm)
96+
i := newMockDeploymentsInteractor(store, scm)
9797

9898
_, err := i.Deploy(context.Background(), &ent.User{}, &ent.Repo{}, &ent.Deployment{}, &extent.Env{})
9999
if !e.HasErrorCode(err, e.ErrorCodeDeploymentLocked) {
@@ -159,7 +159,7 @@ func TestInteractor_Deploy(t *testing.T) {
159159
EXPECT().
160160
CreateDeploymentStatus(ctx, gomock.AssignableToTypeOf(&ent.DeploymentStatus{}))
161161

162-
i := newMockInteractor(store, scm)
162+
i := newMockDeploymentsInteractor(store, scm)
163163

164164
d, err := i.Deploy(context.Background(), &ent.User{}, &ent.Repo{}, input.d, input.e)
165165
if err != nil {
@@ -243,7 +243,7 @@ func TestInteractor_Deploy(t *testing.T) {
243243
CreateEvent(ctx, gomock.AssignableToTypeOf(&ent.Event{})).
244244
Return(&ent.Event{}, nil)
245245

246-
i := newMockInteractor(store, scm)
246+
i := newMockDeploymentsInteractor(store, scm)
247247

248248
d, err := i.Deploy(context.Background(), &ent.User{}, &ent.Repo{}, input.d, input.e)
249249
if err != nil {
@@ -281,7 +281,7 @@ func TestInteractor_DeployToRemote(t *testing.T) {
281281
store := mock.NewMockStore(ctrl)
282282
scm := mock.NewMockSCM(ctrl)
283283

284-
i := newMockInteractor(store, scm)
284+
i := newMockDeploymentsInteractor(store, scm)
285285

286286
_, err := i.DeployToRemote(context.Background(), &ent.User{}, &ent.Repo{}, input.d, input.e)
287287
if !e.HasErrorCode(err, e.ErrorCodeDeploymentStatusInvalid) {
@@ -345,7 +345,7 @@ func TestInteractor_DeployToRemote(t *testing.T) {
345345
EXPECT().
346346
CreateDeploymentStatus(ctx, gomock.AssignableToTypeOf(&ent.DeploymentStatus{}))
347347

348-
i := newMockInteractor(store, scm)
348+
i := newMockDeploymentsInteractor(store, scm)
349349

350350
d, err := i.DeployToRemote(context.Background(), &ent.User{}, &ent.Repo{}, input.d, input.e)
351351
if err != nil {

internal/interactor/deploymentstatistics.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,15 @@ import (
77
"github.com/gitploy-io/gitploy/model/ent"
88
)
99

10-
func (i *Interactor) ProduceDeploymentStatisticsOfRepo(ctx context.Context, r *ent.Repo, d *ent.Deployment) (*ent.DeploymentStatistics, error) {
11-
s, err := i.Store.FindDeploymentStatisticsOfRepoByEnv(ctx, r, d.Env)
10+
type (
11+
DeploymentStatisticsInteractor service
12+
)
13+
14+
func (i *DeploymentStatisticsInteractor) ProduceDeploymentStatisticsOfRepo(ctx context.Context, r *ent.Repo, d *ent.Deployment) (*ent.DeploymentStatistics, error) {
15+
s, err := i.store.FindDeploymentStatisticsOfRepoByEnv(ctx, r, d.Env)
1216

1317
if ent.IsNotFound(err) {
14-
if s, err = i.Store.CreateDeploymentStatistics(ctx, &ent.DeploymentStatistics{
18+
if s, err = i.store.CreateDeploymentStatistics(ctx, &ent.DeploymentStatistics{
1519
Env: d.Env,
1620
RepoID: r.ID,
1721
}); err != nil {
@@ -23,10 +27,10 @@ func (i *Interactor) ProduceDeploymentStatisticsOfRepo(ctx context.Context, r *e
2327
return nil, err
2428
}
2529

26-
return i.Store.UpdateDeploymentStatistics(ctx, s)
30+
return i.store.UpdateDeploymentStatistics(ctx, s)
2731
}
2832

29-
func (i *Interactor) produceDeploymentStatisticsOfRepo(ctx context.Context, r *ent.Repo, d *ent.Deployment, s *ent.DeploymentStatistics) (*ent.DeploymentStatistics, error) {
33+
func (i *DeploymentStatisticsInteractor) produceDeploymentStatisticsOfRepo(ctx context.Context, r *ent.Repo, d *ent.Deployment, s *ent.DeploymentStatistics) (*ent.DeploymentStatistics, error) {
3034
{
3135
if d.IsRollback {
3236
s.RollbackCount = s.RollbackCount + 1
@@ -36,22 +40,22 @@ func (i *Interactor) produceDeploymentStatisticsOfRepo(ctx context.Context, r *e
3640
}
3741

3842
{
39-
ld, err := i.Store.FindPrevSuccessDeployment(ctx, d)
43+
ld, err := i.store.FindPrevSuccessDeployment(ctx, d)
4044
if ent.IsNotFound(err) {
4145
return s, nil
4246
} else if err != nil {
4347
return nil, err
4448
}
4549

4650
if d.Edges.User == nil {
47-
if d, err = i.Store.FindDeploymentByID(ctx, d.ID); err != nil {
51+
if d, err = i.store.FindDeploymentByID(ctx, d.ID); err != nil {
4852
return nil, err
4953
} else if d.Edges.User == nil {
5054
return nil, fmt.Errorf("The deployer is not found.")
5155
}
5256
}
5357

54-
cms, fs, err := i.SCM.CompareCommits(ctx, d.Edges.User, r, ld.Sha, d.Sha, 1, 100)
58+
cms, fs, err := i.scm.CompareCommits(ctx, d.Edges.User, r, ld.Sha, d.Sha, 1, 100)
5559
if err != nil {
5660
return nil, err
5761
}

internal/interactor/deploymentstatistics_test.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/gitploy-io/gitploy/model/ent"
99
"github.com/gitploy-io/gitploy/model/extent"
1010
"github.com/golang/mock/gomock"
11+
"go.uber.org/zap"
1112
)
1213

1314
func TestInteractor_ProduceDeploymentStatisticsOfRepo(t *testing.T) {
@@ -55,7 +56,11 @@ func TestInteractor_ProduceDeploymentStatisticsOfRepo(t *testing.T) {
5556
return s, nil
5657
})
5758

58-
i := newMockInteractor(store, scm)
59+
i := &DeploymentStatisticsInteractor{
60+
store: store,
61+
scm: scm,
62+
log: zap.L(),
63+
}
5964

6065
_, err := i.ProduceDeploymentStatisticsOfRepo(context.Background(), input.repo, input.d)
6166
if err != nil {
@@ -113,7 +118,11 @@ func TestInteractor_ProduceDeploymentStatisticsOfRepo(t *testing.T) {
113118
return s, nil
114119
})
115120

116-
i := newMockInteractor(store, scm)
121+
i := &DeploymentStatisticsInteractor{
122+
store: store,
123+
scm: scm,
124+
log: zap.L(),
125+
}
117126

118127
_, err := i.ProduceDeploymentStatisticsOfRepo(context.Background(), input.repo, input.d)
119128
if err != nil {

internal/interactor/event.go

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,33 @@ import (
44
"context"
55
"time"
66

7-
"github.com/gitploy-io/gitploy/model/ent"
7+
evbus "github.com/asaskevich/EventBus"
88
"go.uber.org/zap"
9+
10+
"github.com/gitploy-io/gitploy/model/ent"
911
)
1012

1113
const (
1214
gitployEvent = "gitploy:event"
1315
)
1416

15-
func (i *Interactor) runPublishingEvents(stop <-chan struct{}) {
17+
type (
18+
EventsInteractor struct {
19+
*service
20+
21+
events evbus.Bus
22+
}
23+
)
24+
25+
func (i *EventsInteractor) SubscribeEvent(fn func(e *ent.Event)) error {
26+
return i.events.SubscribeAsync(gitployEvent, fn, false)
27+
}
28+
29+
func (i *EventsInteractor) UnsubscribeEvent(fn func(e *ent.Event)) error {
30+
return i.events.Unsubscribe(gitployEvent, fn)
31+
}
32+
33+
func (i *EventsInteractor) runPublishingEvents(stop <-chan struct{}) {
1634
ctx := context.Background()
1735

1836
// Read events periodically and publish to subscribers.
@@ -29,7 +47,7 @@ L:
2947
}
3048

3149
case t := <-ticker.C:
32-
es, err := i.ListEventsGreaterThanTime(ctx, t.Add(-period).UTC())
50+
es, err := i.store.ListEventsGreaterThanTime(ctx, t.Add(-period).UTC())
3351
if err != nil {
3452
i.log.Error("It has failed to read events.", zap.Error(err))
3553
continue
@@ -42,11 +60,3 @@ L:
4260
}
4361
}
4462
}
45-
46-
func (i *Interactor) SubscribeEvent(fn func(e *ent.Event)) error {
47-
return i.events.SubscribeAsync(gitployEvent, fn, false)
48-
}
49-
50-
func (i *Interactor) UnsubscribeEvent(fn func(e *ent.Event)) error {
51-
return i.events.Unsubscribe(gitployEvent, fn)
52-
}

0 commit comments

Comments
 (0)