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

Commit 389fedb

Browse files
author
Noah Lee
authored
Refactoring the RepoInteractor (#344)
* Add ChatUserStore interface * Refactorying the `RepoInteractor` * Fix the parameters of 'ActivateRepo'
1 parent 724a073 commit 389fedb

23 files changed

+204
-190
lines changed

cmd/server/main.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@ func newServerConfig(c *Config) *server.ServerConfig {
9494
Proto: c.ServerProto,
9595
ProxyHost: proxyHost,
9696
ProxyProto: proxyProto,
97-
WebhookSecret: c.WebhookSecret,
9897
PrometheusEnabled: c.PrometheusEnabled,
9998
PrometheusAuthSecret: c.PrometheusAuthSecret,
10099
}
@@ -134,14 +133,21 @@ func newChatConfig(c *Config) *server.ChatConfig {
134133
func NewInteractor(c *Config) server.Interactor {
135134
return interactor.NewInteractor(
136135
&interactor.InteractorConfig{
137-
ServerHost: c.ServerHost,
138-
ServerProto: c.ServerProto,
136+
// Server Configurations:
137+
ServerHost: c.ServerHost,
138+
ServerProto: c.ServerProto,
139+
ServerProxyHost: c.ServerProxyHost,
140+
ServerProxyProto: c.ServerProxyProto,
141+
// Sign-in Configurations:
139142
OrgEntries: c.OrganizationEntries,
140143
MemberEntries: c.MemberEntries,
141144
AdminUsers: c.AdminUsers,
142-
LicenseKey: c.License,
143-
Store: newStore(c),
144-
SCM: newSCM(c),
145+
// Webhook Configurations:
146+
WebhookSecret: c.WebhookSecret,
147+
// License Configurations:
148+
LicenseKey: c.License,
149+
Store: newStore(c),
150+
SCM: newSCM(c),
145151
},
146152
)
147153
}

internal/interactor/_mock.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
mockgen \
22
-aux_files \
33
github.com/gitploy-io/gitploy/internal/interactor=user.go\
4-
,github.com/gitploy-io/gitploy/internal/interactor=sync.go\
4+
,github.com/gitploy-io/gitploy/internal/interactor=repo.go\
55
-source ./interface.go \
66
-package mock \
77
-destination ./mock/pkg.go

internal/interactor/deployment_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,12 @@ import (
1919

2020
func TestInteractor_IsApproved(t *testing.T) {
2121
t.Run("Return false when a review is rejected.", func(t *testing.T) {
22+
t.Log("Start mocking:")
2223
ctrl := gomock.NewController(t)
2324
store := mock.NewMockStore(ctrl)
2425
scm := mock.NewMockSCM(ctrl)
2526

26-
t.Log("Return various status reviews")
27+
t.Log("\tList reviews.")
2728
store.
2829
EXPECT().
2930
ListReviews(gomock.Any(), gomock.AssignableToTypeOf(&ent.Deployment{})).

internal/interactor/interactor.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package interactor
22

33
import (
4+
"fmt"
5+
46
evbus "github.com/asaskevich/EventBus"
57
"go.uber.org/zap"
68
)
@@ -26,13 +28,17 @@ type (
2628
}
2729

2830
InteractorConfig struct {
29-
ServerHost string
30-
ServerProto string
31+
ServerHost string
32+
ServerProto string
33+
ServerProxyHost string
34+
ServerProxyProto string
3135

3236
OrgEntries []string
3337
MemberEntries []string
3438
AdminUsers []string
3539

40+
WebhookSecret string
41+
3642
LicenseKey string
3743

3844
Store
@@ -72,7 +78,12 @@ func NewInteractor(c *InteractorConfig) *Interactor {
7278
LicenseKey: c.LicenseKey,
7379
}
7480
i.LockInteractor = (*LockInteractor)(i.common)
75-
i.RepoInteractor = (*RepoInteractor)(i.common)
81+
i.RepoInteractor = &RepoInteractor{
82+
service: i.common,
83+
WebhookURL: fmt.Sprintf("%s://%s/hooks", c.ServerProxyProto, c.ServerProxyHost),
84+
WebhookSSL: c.ServerProxyProto == "https",
85+
WebhookSecret: c.WebhookSecret,
86+
}
7687
i.UserInteractor = &UserInteractor{
7788
service: i.common,
7889
admins: c.AdminUsers,

internal/interactor/interface.go

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,8 @@ import (
1414
type (
1515
Store interface {
1616
UserStore
17-
18-
FindChatUserByID(ctx context.Context, id string) (*ent.ChatUser, error)
19-
CreateChatUser(ctx context.Context, cu *ent.ChatUser) (*ent.ChatUser, error)
20-
UpdateChatUser(ctx context.Context, cu *ent.ChatUser) (*ent.ChatUser, error)
21-
DeleteChatUser(ctx context.Context, cu *ent.ChatUser) error
22-
23-
CountActiveRepos(ctx context.Context) (int, error)
24-
CountRepos(ctx context.Context) (int, error)
25-
ListReposOfUser(ctx context.Context, u *ent.User, q, namespace, name string, sorted bool, page, perPage int) ([]*ent.Repo, error)
26-
FindRepoOfUserByID(ctx context.Context, u *ent.User, id int64) (*ent.Repo, error)
27-
FindRepoOfUserByNamespaceName(ctx context.Context, u *ent.User, namespace, name string) (*ent.Repo, error)
28-
FindRepoByID(ctx context.Context, id int64) (*ent.Repo, error)
29-
SyncRepo(ctx context.Context, r *extent.RemoteRepo) (*ent.Repo, error)
30-
UpdateRepo(ctx context.Context, r *ent.Repo) (*ent.Repo, error)
31-
Activate(ctx context.Context, r *ent.Repo) (*ent.Repo, error)
32-
Deactivate(ctx context.Context, r *ent.Repo) (*ent.Repo, error)
17+
ChatUserStore
18+
RepoStore
3319

3420
ListPermsOfRepo(ctx context.Context, r *ent.Repo, q string, page, perPage int) ([]*ent.Perm, error)
3521
FindPermOfRepo(ctx context.Context, r *ent.Repo, u *ent.User) (*ent.Perm, error)
@@ -83,8 +69,7 @@ type (
8369

8470
SCM interface {
8571
UserSCM
86-
87-
ListRemoteRepos(ctx context.Context, u *ent.User) ([]*extent.RemoteRepo, error)
72+
RepoSCM
8873

8974
GetConfigRedirectURL(ctx context.Context, u *ent.User, r *ent.Repo) (string, error)
9075
GetNewConfigRedirectURL(ctx context.Context, u *ent.User, r *ent.Repo) (string, error)

internal/interactor/mock/pkg.go

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/interactor/repo.go

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,60 @@ import (
1010
"go.uber.org/zap"
1111
)
1212

13-
type RepoInteractor service
13+
type (
14+
// RepoInteractor provides application logic for interacting with repos.
15+
RepoInteractor struct {
16+
*service
1417

15-
func (i *RepoInteractor) ActivateRepo(ctx context.Context, u *ent.User, r *ent.Repo, c *extent.WebhookConfig) (*ent.Repo, error) {
16-
hid, err := i.scm.CreateWebhook(ctx, u, r, c)
18+
WebhookURL string
19+
WebhookSSL bool
20+
WebhookSecret string
21+
}
22+
23+
// RepoStore defines operations for working with repos.
24+
RepoStore interface {
25+
CountActiveRepos(ctx context.Context) (int, error)
26+
CountRepos(ctx context.Context) (int, error)
27+
ListReposOfUser(ctx context.Context, u *ent.User, opt *ListReposOfUserOptions) ([]*ent.Repo, error)
28+
FindRepoOfUserByID(ctx context.Context, u *ent.User, id int64) (*ent.Repo, error)
29+
FindRepoOfUserByNamespaceName(ctx context.Context, u *ent.User, opt *FindRepoOfUserByNamespaceNameOptions) (*ent.Repo, error)
30+
FindRepoByID(ctx context.Context, id int64) (*ent.Repo, error)
31+
SyncRepo(ctx context.Context, r *extent.RemoteRepo) (*ent.Repo, error)
32+
UpdateRepo(ctx context.Context, r *ent.Repo) (*ent.Repo, error)
33+
Activate(ctx context.Context, r *ent.Repo) (*ent.Repo, error)
34+
Deactivate(ctx context.Context, r *ent.Repo) (*ent.Repo, error)
35+
}
36+
37+
// ListReposOfUser specifies the optional parameters that
38+
// search repos.
39+
ListReposOfUserOptions struct {
40+
ListOptions
41+
42+
// Query search the repos contains the query in the namespace or name.
43+
Query string
44+
// Sorted instructs the system to sort by the 'deployed_at' field.
45+
Sorted bool
46+
}
47+
48+
// FindRepoOfUserByNamespaceName specifies the parameters to get the repository.
49+
FindRepoOfUserByNamespaceNameOptions struct {
50+
Namespace, Name string
51+
}
52+
53+
// RepoSCM defines operations for working with remote repos.
54+
RepoSCM interface {
55+
ListRemoteRepos(ctx context.Context, u *ent.User) ([]*extent.RemoteRepo, error)
56+
}
57+
)
58+
59+
// ActivateRepo create a new hook to listen events, and saves
60+
// the hook ID.
61+
func (i *RepoInteractor) ActivateRepo(ctx context.Context, u *ent.User, r *ent.Repo) (*ent.Repo, error) {
62+
hid, err := i.scm.CreateWebhook(ctx, u, r, &extent.WebhookConfig{
63+
URL: i.WebhookURL,
64+
InsecureSSL: i.WebhookSSL,
65+
Secret: i.WebhookSecret,
66+
})
1767
if err != nil {
1868
return nil, fmt.Errorf("failed to create a webhook: %s", err)
1969
}
@@ -29,6 +79,7 @@ func (i *RepoInteractor) ActivateRepo(ctx context.Context, u *ent.User, r *ent.R
2979
return r, nil
3080
}
3181

82+
// DeactivateRepo removes the webhook.
3283
func (i *RepoInteractor) DeactivateRepo(ctx context.Context, u *ent.User, r *ent.Repo) (*ent.Repo, error) {
3384
err := i.scm.DeleteWebhook(ctx, u, r, r.WebhookID)
3485
if e.HasErrorCode(err, e.ErrorCodeEntityNotFound) {

internal/interactor/shared.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package interactor
22

33
type (
4-
// ListCursorOptions specifies the optional parameters that
4+
// ListOptions specifies the optional parameters that
55
// support cursor pagination.
66
ListOptions struct {
77
Page int

internal/interactor/user.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,14 @@ type (
4545
GetRemoteUserByToken(ctx context.Context, token string) (*extent.RemoteUser, error)
4646
ListRemoteOrgsByToken(ctx context.Context, token string) ([]string, error)
4747
}
48+
49+
// ChatUserStore defines operations for working with chat_users.
50+
ChatUserStore interface {
51+
FindChatUserByID(ctx context.Context, id string) (*ent.ChatUser, error)
52+
CreateChatUser(ctx context.Context, cu *ent.ChatUser) (*ent.ChatUser, error)
53+
UpdateChatUser(ctx context.Context, cu *ent.ChatUser) (*ent.ChatUser, error)
54+
DeleteChatUser(ctx context.Context, cu *ent.ChatUser) error
55+
}
4856
)
4957

5058
// IsAdminUser verifies that the login is an admin or not.

internal/pkg/store/repo.go

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66

77
"entgo.io/ent/dialect/sql"
8+
i "github.com/gitploy-io/gitploy/internal/interactor"
89
"github.com/gitploy-io/gitploy/model/ent"
910
"github.com/gitploy-io/gitploy/model/ent/deployment"
1011
"github.com/gitploy-io/gitploy/model/ent/perm"
@@ -36,7 +37,7 @@ func (s *Store) CountRepos(ctx context.Context) (int, error) {
3637
return cnt, nil
3738
}
3839

39-
func (s *Store) ListReposOfUser(ctx context.Context, u *ent.User, q, namespace, name string, sorted bool, page, perPage int) ([]*ent.Repo, error) {
40+
func (s *Store) ListReposOfUser(ctx context.Context, u *ent.User, opt *i.ListReposOfUserOptions) ([]*ent.Repo, error) {
4041
// Build the query with parameters.
4142
qry := s.c.Repo.
4243
Query().
@@ -48,10 +49,10 @@ func (s *Store) ListReposOfUser(ctx context.Context, u *ent.User, q, namespace,
4849
Where(sql.EQ(t.C(perm.FieldUserID), u.ID))
4950
}).
5051
WithOwner().
51-
Limit(perPage).
52-
Offset(offset(page, perPage))
52+
Limit(opt.PerPage).
53+
Offset(offset(opt.Page, opt.PerPage))
5354

54-
if q != "" {
55+
if q := opt.Query; q != "" {
5556
qry = qry.Where(
5657
repo.Or(
5758
repo.NamespaceContains(q),
@@ -60,15 +61,7 @@ func (s *Store) ListReposOfUser(ctx context.Context, u *ent.User, q, namespace,
6061
)
6162
}
6263

63-
if namespace != "" {
64-
qry = qry.Where(repo.NamespaceEQ(namespace))
65-
}
66-
67-
if name != "" {
68-
qry = qry.Where(repo.NameEQ(name))
69-
}
70-
71-
if sorted {
64+
if opt.Sorted {
7265
qry = qry.Order(
7366
ent.Desc(repo.FieldLatestDeployedAt),
7467
)
@@ -136,7 +129,7 @@ func (s *Store) FindRepoOfUserByID(ctx context.Context, u *ent.User, id int64) (
136129
return r, nil
137130
}
138131

139-
func (s *Store) FindRepoOfUserByNamespaceName(ctx context.Context, u *ent.User, namespace, name string) (*ent.Repo, error) {
132+
func (s *Store) FindRepoOfUserByNamespaceName(ctx context.Context, u *ent.User, opt *i.FindRepoOfUserByNamespaceNameOptions) (*ent.Repo, error) {
140133
r, err := s.c.Repo.
141134
Query().
142135
Where(func(s *sql.Selector) {
@@ -148,8 +141,8 @@ func (s *Store) FindRepoOfUserByNamespaceName(ctx context.Context, u *ent.User,
148141
}).
149142
Where(
150143
repo.And(
151-
repo.NamespaceEQ(namespace),
152-
repo.NameEQ(name),
144+
repo.NamespaceEQ(opt.Namespace),
145+
repo.NameEQ(opt.Name),
153146
),
154147
).
155148
WithOwner().

0 commit comments

Comments
 (0)