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

Commit 01cd01f

Browse files
author
noah
committed
Add limit count of deployment.
1 parent ecd0839 commit 01cd01f

File tree

8 files changed

+93
-33
lines changed

8 files changed

+93
-33
lines changed

internal/interactor/interface.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ type (
4545
UpdatePerm(ctx context.Context, p *ent.Perm) (*ent.Perm, error)
4646
DeletePermsOfUserLessThanSyncedAt(ctx context.Context, u *ent.User, t time.Time) (int, error)
4747

48+
CountDeployments(ctx context.Context) (int, error)
4849
SearchDeployments(ctx context.Context, u *ent.User, s []deployment.Status, owned bool, from time.Time, to time.Time, page, perPage int) ([]*ent.Deployment, error)
4950
ListInactiveDeploymentsLessThanTime(ctx context.Context, t time.Time, page, perPage int) ([]*ent.Deployment, error)
5051
ListDeploymentsOfRepo(ctx context.Context, r *ent.Repo, env string, status string, page, perPage int) ([]*ent.Deployment, error)

internal/interactor/license.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,29 @@ import (
1515

1616
func (i *Interactor) GetLicense(ctx context.Context) (*vo.License, error) {
1717
var (
18-
cnt int
19-
d *vo.SigningData
20-
err error
18+
memberCnt int
19+
deploymentCnt int
20+
d *vo.SigningData
21+
err error
2122
)
2223

23-
if cnt, err = i.Store.CountUsers(ctx); err != nil {
24+
if memberCnt, err = i.Store.CountUsers(ctx); err != nil {
25+
return nil, err
26+
}
27+
28+
if deploymentCnt, err = i.Store.CountDeployments(ctx); err != nil {
2429
return nil, err
2530
}
2631

2732
if i.licenseKey == "" {
28-
lic := vo.NewTrialLicense(cnt)
33+
lic := vo.NewTrialLicense(memberCnt, deploymentCnt)
2934
return lic, nil
3035
}
3136

3237
if d, err = license.Decode(i.licenseKey); err != nil {
3338
return nil, err
3439
}
3540

36-
lic := vo.NewStandardLicense(cnt, d)
41+
lic := vo.NewStandardLicense(memberCnt, d)
3742
return lic, nil
3843
}

internal/interactor/license_test.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,12 @@ func TestStore_GetLicense(t *testing.T) {
1818
store.
1919
EXPECT().
2020
CountUsers(gomock.AssignableToTypeOf(context.Background())).
21-
Return(5, nil)
21+
Return(vo.TrialMemberLimit, nil)
22+
23+
store.
24+
EXPECT().
25+
CountDeployments(gomock.AssignableToTypeOf(context.Background())).
26+
Return(vo.TrialDeploymentLimit, nil)
2227

2328
i := &Interactor{Store: store}
2429

internal/interactor/mock/pkg.go

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

internal/pkg/store/deployment.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ import (
1111
"github.com/gitploy-io/gitploy/ent/predicate"
1212
)
1313

14+
func (s *Store) CountDeployments(ctx context.Context) (int, error) {
15+
return s.c.Deployment.
16+
Query().
17+
Count(ctx)
18+
}
19+
1420
func (s *Store) SearchDeployments(ctx context.Context, u *ent.User, ss []deployment.Status, owned bool, from time.Time, to time.Time, page, perPage int) ([]*ent.Deployment, error) {
1521
statusIn := func(ss []deployment.Status) predicate.Deployment {
1622
if len(ss) == 0 {

internal/server/api/shared/middleware_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func TestMiddleware_IsLicenseExpired(t *testing.T) {
4949
m.
5050
EXPECT().
5151
GetLicense(gomock.Any()).
52-
Return(vo.NewTrialLicense(7), nil)
52+
Return(vo.NewTrialLicense(vo.TrialMemberLimit+1, vo.TrialDeploymentLimit), nil)
5353

5454
gin.SetMode(gin.ReleaseMode)
5555
router := gin.New()
@@ -76,7 +76,7 @@ func TestMiddleware_IsLicenseExpired(t *testing.T) {
7676
m.
7777
EXPECT().
7878
GetLicense(gomock.Any()).
79-
Return(vo.NewTrialLicense(vo.TrialMemberLimit), nil)
79+
Return(vo.NewTrialLicense(vo.TrialMemberLimit, vo.TrialDeploymentLimit), nil)
8080

8181
gin.SetMode(gin.ReleaseMode)
8282
router := gin.New()

vo/license.go

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ package vo
33
import "time"
44

55
const (
6-
TrialMemberLimit = 5
6+
TrialMemberLimit = 5
7+
TrialDeploymentLimit = 5000
78
)
89

910
const (
@@ -19,10 +20,12 @@ type (
1920
LicenseKind string
2021

2122
License struct {
22-
Kind LicenseKind `json:"kind"`
23-
MemberCount int `json:"member_count"`
24-
MemberLimit int `json:"memeber_limit"`
25-
ExpiredAt time.Time `json:"expired_at"`
23+
Kind LicenseKind `json:"kind"`
24+
MemberCount int `json:"member_count"`
25+
MemberLimit int `json:"memeber_limit"`
26+
DeploymentCount int `json:"deployment_count"`
27+
DeploymentLimit int `json:"deployment_limit"`
28+
ExpiredAt time.Time `json:"expired_at"`
2629
}
2730

2831
// SigningData marshal and unmarshal the content of license.
@@ -34,25 +37,29 @@ type (
3437

3538
func NewOSSLicense() *License {
3639
return &License{
37-
Kind: LicenseKindOSS,
38-
MemberCount: -1,
40+
Kind: LicenseKindOSS,
41+
MemberCount: -1,
42+
DeploymentCount: -1,
3943
}
4044
}
4145

42-
func NewTrialLicense(cnt int) *License {
46+
func NewTrialLicense(memberCnt, deploymentCnt int) *License {
4347
return &License{
44-
Kind: LicenseKindTrial,
45-
MemberCount: cnt,
46-
MemberLimit: TrialMemberLimit,
48+
Kind: LicenseKindTrial,
49+
MemberCount: memberCnt,
50+
MemberLimit: TrialMemberLimit,
51+
DeploymentCount: deploymentCnt,
52+
DeploymentLimit: TrialDeploymentLimit,
4753
}
4854
}
4955

50-
func NewStandardLicense(cnt int, d *SigningData) *License {
56+
func NewStandardLicense(memberCnt int, d *SigningData) *License {
5157
return &License{
52-
Kind: LicenseKindStandard,
53-
MemberCount: cnt,
54-
MemberLimit: d.MemberLimit,
55-
ExpiredAt: d.ExpiredAt,
58+
Kind: LicenseKindStandard,
59+
MemberCount: memberCnt,
60+
MemberLimit: d.MemberLimit,
61+
DeploymentCount: -1,
62+
ExpiredAt: d.ExpiredAt,
5663
}
5764
}
5865

@@ -70,7 +77,7 @@ func (l *License) IsStandard() bool {
7077

7178
// IsOverLimit verify it is over the limit of the license.
7279
func (l *License) IsOverLimit() bool {
73-
return l.MemberCount > l.MemberLimit
80+
return l.MemberCount > l.MemberLimit || l.DeploymentCount > l.DeploymentLimit
7481
}
7582

7683
// IsExpired verify that the license is expired or not.

vo/license_test.go

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,44 @@ import (
66
)
77

88
func TestLicense_IsOverLimit(t *testing.T) {
9-
t.Run("Return false when the count of member is over the limit.", func(t *testing.T) {
10-
l := NewTrialLicense(6)
9+
t.Run("Return false when the license is OSS.", func(t *testing.T) {
10+
l := NewOSSLicense()
11+
12+
expected := false
13+
if finished := l.IsOverLimit(); finished != expected {
14+
t.Fatalf("IsOverLimit = %v, wanted %v", finished, expected)
15+
}
16+
})
17+
18+
t.Run("Return true when the trial license is over the member limit.", func(t *testing.T) {
19+
l := NewTrialLicense(TrialMemberLimit+1, 0)
1120

1221
expected := true
1322
if finished := l.IsOverLimit(); finished != expected {
1423
t.Fatalf("IsOverLimit = %v, wanted %v", finished, expected)
1524
}
1625
})
1726

18-
t.Run("Return true when the count of member is under the limit.", func(t *testing.T) {
19-
tl := NewTrialLicense(5)
27+
t.Run("Return true when the trial license is over the deployment limit.", func(t *testing.T) {
28+
l := NewTrialLicense(5, TrialDeploymentLimit+1)
2029

21-
if finished := tl.IsOverLimit(); finished != false {
22-
t.Fatalf("IsOverLimit = %v, wanted %v", finished, false)
30+
expected := true
31+
if finished := l.IsOverLimit(); finished != expected {
32+
t.Fatalf("IsOverLimit = %v, wanted %v", finished, expected)
2333
}
34+
})
35+
36+
t.Run("Return false when the trial license is less than or equal to the limit.", func(t *testing.T) {
37+
l := NewTrialLicense(TrialMemberLimit, TrialDeploymentLimit)
38+
39+
expected := false
40+
if finished := l.IsOverLimit(); finished != expected {
41+
t.Fatalf("IsOverLimit = %v, wanted %v", finished, expected)
42+
}
43+
})
2444

25-
sl := NewStandardLicense(10, &SigningData{
45+
t.Run("Return true when the standard license is less than the limit.", func(t *testing.T) {
46+
sl := NewStandardLicense(20, &SigningData{
2647
MemberLimit: 20,
2748
ExpiredAt: time.Now(),
2849
})

0 commit comments

Comments
 (0)