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

Commit 93cf822

Browse files
author
Noah Lee
authored
Refactoring interactors (#347)
* Refactoring interactors * Fix the 'ListCommits' method * Fix the 'CompareCommits' method * Fix the 'ListBranches' method
1 parent ca735a5 commit 93cf822

File tree

15 files changed

+555
-87
lines changed

15 files changed

+555
-87
lines changed

internal/interactor/_mock.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ mockgen \
33
github.com/gitploy-io/gitploy/internal/interactor=user.go\
44
,github.com/gitploy-io/gitploy/internal/interactor=repo.go\
55
,github.com/gitploy-io/gitploy/internal/interactor=deployment.go\
6+
,github.com/gitploy-io/gitploy/internal/interactor=deploymentstatistics.go\
7+
,github.com/gitploy-io/gitploy/internal/interactor=lock.go\
8+
,github.com/gitploy-io/gitploy/internal/interactor=event.go\
69
-source ./interface.go \
710
-package mock \
811
-destination ./mock/pkg.go

internal/interactor/deploymentstatistics.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,23 @@ package interactor
33
import (
44
"context"
55
"fmt"
6+
"time"
67

78
"github.com/gitploy-io/gitploy/model/ent"
89
)
910

1011
type (
12+
// DeploymentStatisticsInteractor provides application logic for interacting with deployment_statistics.
1113
DeploymentStatisticsInteractor service
14+
15+
// DeploymentStatisticsStore defines operations for working with deployment_statistics.
16+
DeploymentStatisticsStore interface {
17+
ListAllDeploymentStatistics(ctx context.Context) ([]*ent.DeploymentStatistics, error)
18+
ListDeploymentStatisticsGreaterThanTime(ctx context.Context, updated time.Time) ([]*ent.DeploymentStatistics, error)
19+
FindDeploymentStatisticsOfRepoByEnv(ctx context.Context, r *ent.Repo, env string) (*ent.DeploymentStatistics, error)
20+
CreateDeploymentStatistics(ctx context.Context, s *ent.DeploymentStatistics) (*ent.DeploymentStatistics, error)
21+
UpdateDeploymentStatistics(ctx context.Context, s *ent.DeploymentStatistics) (*ent.DeploymentStatistics, error)
22+
}
1223
)
1324

1425
func (i *DeploymentStatisticsInteractor) ProduceDeploymentStatisticsOfRepo(ctx context.Context, r *ent.Repo, d *ent.Deployment) (*ent.DeploymentStatistics, error) {
@@ -55,7 +66,10 @@ func (i *DeploymentStatisticsInteractor) produceDeploymentStatisticsOfRepo(ctx c
5566
}
5667
}
5768

58-
cms, fs, err := i.scm.CompareCommits(ctx, d.Edges.User, r, ld.Sha, d.Sha, 1, 100)
69+
cms, fs, err := i.scm.CompareCommits(ctx, d.Edges.User, r, ld.Sha, d.Sha, &ListOptions{
70+
Page: 1,
71+
PerPage: 100,
72+
})
5973
if err != nil {
6074
return nil, err
6175
}

internal/interactor/deploymentstatistics_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ func TestInteractor_ProduceDeploymentStatisticsOfRepo(t *testing.T) {
104104
t.Log("MOCK - Get changed commits from the SCM.")
105105
scm.
106106
EXPECT().
107-
CompareCommits(gomock.Any(), gomock.AssignableToTypeOf(&ent.User{}), gomock.AssignableToTypeOf(&ent.Repo{}), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).
107+
CompareCommits(gomock.Any(), gomock.AssignableToTypeOf(&ent.User{}), gomock.AssignableToTypeOf(&ent.Repo{}), gomock.Any(), gomock.Any(), gomock.Any()).
108108
Return([]*extent.Commit{}, []*extent.CommitFile{
109109
{Additions: 1, Deletions: 1, Changes: 2},
110110
}, nil)

internal/interactor/event.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@ type (
2020

2121
events evbus.Bus
2222
}
23+
24+
// EventStore defines operations for working with events.
25+
EventStore interface {
26+
ListEventsGreaterThanTime(ctx context.Context, t time.Time) ([]*ent.Event, error)
27+
CreateEvent(ctx context.Context, e *ent.Event) (*ent.Event, error)
28+
CheckNotificationRecordOfEvent(ctx context.Context, e *ent.Event) bool
29+
}
2330
)
2431

2532
func (i *EventInteractor) SubscribeEvent(fn func(e *ent.Event)) error {

internal/interactor/interface.go

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -18,33 +18,10 @@ type (
1818
PermStore
1919
DeploymentStore
2020
DeploymentStatusStore
21-
22-
FindDeploymentStatisticsOfRepoByEnv(ctx context.Context, r *ent.Repo, env string) (*ent.DeploymentStatistics, error)
23-
CreateDeploymentStatistics(ctx context.Context, s *ent.DeploymentStatistics) (*ent.DeploymentStatistics, error)
24-
UpdateDeploymentStatistics(ctx context.Context, s *ent.DeploymentStatistics) (*ent.DeploymentStatistics, error)
25-
26-
ListAllDeploymentStatistics(ctx context.Context) ([]*ent.DeploymentStatistics, error)
27-
ListDeploymentStatisticsGreaterThanTime(ctx context.Context, updated time.Time) ([]*ent.DeploymentStatistics, error)
28-
29-
SearchReviews(ctx context.Context, u *ent.User) ([]*ent.Review, error)
30-
ListReviews(ctx context.Context, d *ent.Deployment) ([]*ent.Review, error)
31-
FindReviewOfUser(ctx context.Context, u *ent.User, d *ent.Deployment) (*ent.Review, error)
32-
FindReviewByID(ctx context.Context, id int) (*ent.Review, error)
33-
CreateReview(ctx context.Context, rv *ent.Review) (*ent.Review, error)
34-
UpdateReview(ctx context.Context, rv *ent.Review) (*ent.Review, error)
35-
36-
ListExpiredLocksLessThanTime(ctx context.Context, t time.Time) ([]*ent.Lock, error)
37-
ListLocksOfRepo(ctx context.Context, r *ent.Repo) ([]*ent.Lock, error)
38-
FindLockOfRepoByEnv(ctx context.Context, r *ent.Repo, env string) (*ent.Lock, error)
39-
HasLockOfRepoForEnv(ctx context.Context, r *ent.Repo, env string) (bool, error)
40-
FindLockByID(ctx context.Context, id int) (*ent.Lock, error)
41-
CreateLock(ctx context.Context, l *ent.Lock) (*ent.Lock, error)
42-
UpdateLock(ctx context.Context, l *ent.Lock) (*ent.Lock, error)
43-
DeleteLock(ctx context.Context, l *ent.Lock) error
44-
45-
ListEventsGreaterThanTime(ctx context.Context, t time.Time) ([]*ent.Event, error)
46-
CreateEvent(ctx context.Context, e *ent.Event) (*ent.Event, error)
47-
CheckNotificationRecordOfEvent(ctx context.Context, e *ent.Event) bool
21+
DeploymentStatisticsStore
22+
LockStore
23+
ReviewStore
24+
EventStore
4825
}
4926

5027
// PermStore defines operations for working with perms.
@@ -70,24 +47,48 @@ type (
7047
SyncDeploymentStatus(ctx context.Context, ds *ent.DeploymentStatus) (*ent.DeploymentStatus, error)
7148
}
7249

50+
// ReviewStore defines operations for working with reviews.
51+
ReviewStore interface {
52+
SearchReviews(ctx context.Context, u *ent.User) ([]*ent.Review, error)
53+
ListReviews(ctx context.Context, d *ent.Deployment) ([]*ent.Review, error)
54+
FindReviewOfUser(ctx context.Context, u *ent.User, d *ent.Deployment) (*ent.Review, error)
55+
FindReviewByID(ctx context.Context, id int) (*ent.Review, error)
56+
CreateReview(ctx context.Context, rv *ent.Review) (*ent.Review, error)
57+
UpdateReview(ctx context.Context, rv *ent.Review) (*ent.Review, error)
58+
}
59+
7360
SCM interface {
7461
UserSCM
7562
RepoSCM
7663
DeploymentSCM
64+
DeploymentStatusSCM
65+
CommitSCM
66+
BranchSCM
67+
TagSCM
7768

69+
GetRateLimit(ctx context.Context, u *ent.User) (*extent.RateLimit, error)
70+
}
71+
72+
// DeploymentStatusSCM defines operations for working with remote deployment status.
73+
DeploymentStatusSCM interface {
7874
CreateRemoteDeploymentStatus(ctx context.Context, u *ent.User, r *ent.Repo, d *ent.Deployment, ds *extent.RemoteDeploymentStatus) (*extent.RemoteDeploymentStatus, error)
75+
}
7976

80-
ListCommits(ctx context.Context, u *ent.User, r *ent.Repo, branch string, page, perPage int) ([]*extent.Commit, error)
81-
CompareCommits(ctx context.Context, u *ent.User, r *ent.Repo, base, head string, page, perPage int) ([]*extent.Commit, []*extent.CommitFile, error)
77+
// CommitSCM defines operations for working with commit.
78+
CommitSCM interface {
79+
ListCommits(ctx context.Context, u *ent.User, r *ent.Repo, branch string, opt *ListOptions) ([]*extent.Commit, error)
80+
CompareCommits(ctx context.Context, u *ent.User, r *ent.Repo, base, head string, opt *ListOptions) ([]*extent.Commit, []*extent.CommitFile, error)
8281
GetCommit(ctx context.Context, u *ent.User, r *ent.Repo, sha string) (*extent.Commit, error)
8382
ListCommitStatuses(ctx context.Context, u *ent.User, r *ent.Repo, sha string) ([]*extent.Status, error)
83+
}
8484

85-
ListBranches(ctx context.Context, u *ent.User, r *ent.Repo, page, perPage int) ([]*extent.Branch, error)
85+
BranchSCM interface {
86+
ListBranches(ctx context.Context, u *ent.User, r *ent.Repo, opt *ListOptions) ([]*extent.Branch, error)
8687
GetBranch(ctx context.Context, u *ent.User, r *ent.Repo, branch string) (*extent.Branch, error)
88+
}
8789

88-
ListTags(ctx context.Context, u *ent.User, r *ent.Repo, page, perPage int) ([]*extent.Tag, error)
90+
TagSCM interface {
91+
ListTags(ctx context.Context, u *ent.User, r *ent.Repo, opt *ListOptions) ([]*extent.Tag, error)
8992
GetTag(ctx context.Context, u *ent.User, r *ent.Repo, tag string) (*extent.Tag, error)
90-
91-
GetRateLimit(ctx context.Context, u *ent.User) (*extent.RateLimit, error)
9293
}
9394
)

internal/interactor/lock.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,26 @@ import (
44
"context"
55
"time"
66

7+
"github.com/gitploy-io/gitploy/model/ent"
78
"go.uber.org/zap"
89
)
910

10-
type LockInteractor service
11+
type (
12+
// LockInteractor provides application logic for interacting with users.
13+
LockInteractor service
14+
15+
// LockStore defines operations for working with locks.
16+
LockStore interface {
17+
ListExpiredLocksLessThanTime(ctx context.Context, t time.Time) ([]*ent.Lock, error)
18+
ListLocksOfRepo(ctx context.Context, r *ent.Repo) ([]*ent.Lock, error)
19+
FindLockOfRepoByEnv(ctx context.Context, r *ent.Repo, env string) (*ent.Lock, error)
20+
HasLockOfRepoForEnv(ctx context.Context, r *ent.Repo, env string) (bool, error)
21+
FindLockByID(ctx context.Context, id int) (*ent.Lock, error)
22+
CreateLock(ctx context.Context, l *ent.Lock) (*ent.Lock, error)
23+
UpdateLock(ctx context.Context, l *ent.Lock) (*ent.Lock, error)
24+
DeleteLock(ctx context.Context, l *ent.Lock) error
25+
}
26+
)
1127

1228
func (i *LockInteractor) runAutoUnlock(stop <-chan struct{}) {
1329
ctx := context.Background()

0 commit comments

Comments
 (0)