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

Commit 1e18919

Browse files
author
noah
committed
Add the productionOnly option
1 parent b0c3736 commit 1e18919

File tree

4 files changed

+77
-74
lines changed

4 files changed

+77
-74
lines changed

Diff for: internal/interactor/deployment.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,11 @@ type (
3939
SearchDeploymentsOfUserOptions struct {
4040
ListOptions
4141

42-
Statuses []deployment.Status
43-
Owned bool
44-
From time.Time
45-
To time.Time
42+
Statuses []deployment.Status
43+
Owned bool
44+
ProductionOnly bool
45+
From time.Time
46+
To time.Time
4647
}
4748

4849
// ListInactiveDeploymentsLessThanTimeOptions specifies the optional parameters that

Diff for: internal/pkg/store/deployment.go

+15-23
Original file line numberDiff line numberDiff line change
@@ -35,27 +35,8 @@ func (s *Store) SearchDeploymentsOfUser(ctx context.Context, u *ent.User, opt *i
3535
return deployment.StatusIn(ss...)
3636
}
3737

38-
// Search deployments that were triggered by the user.
39-
if opt.Owned {
40-
return s.c.Deployment.
41-
Query().
42-
Where(
43-
deployment.And(
44-
deployment.UserIDEQ(u.ID),
45-
statusIn(opt.Statuses),
46-
deployment.CreatedAtGTE(opt.From.UTC()),
47-
deployment.CreatedAtLT(opt.To.UTC()),
48-
),
49-
).
50-
Order(ent.Desc(deployment.FieldCreatedAt)).
51-
Offset(offset(opt.Page, opt.PerPage)).
52-
Limit(opt.PerPage).
53-
WithRepo().
54-
WithUser().
55-
All(ctx)
56-
}
57-
58-
return s.c.Deployment.
38+
// Build the query searching all deployments under the accessible repositories.
39+
qry := s.c.Deployment.
5940
Query().
6041
Where(func(s *sql.Selector) {
6142
p := sql.Table(perm.Table)
@@ -78,8 +59,19 @@ func (s *Store) SearchDeploymentsOfUser(ctx context.Context, u *ent.User, opt *i
7859
Offset(offset(opt.Page, opt.PerPage)).
7960
Limit(opt.PerPage).
8061
WithRepo().
81-
WithUser().
82-
All(ctx)
62+
WithUser()
63+
64+
// Search only deployments that were triggered by the user.
65+
if opt.Owned {
66+
qry.Where(deployment.UserIDEQ(u.ID))
67+
}
68+
69+
// Search only deployments for production environment.
70+
if opt.ProductionOnly {
71+
qry.Where(deployment.ProductionEnvironment(true))
72+
}
73+
74+
return qry.All(ctx)
8375
}
8476

8577
func (s *Store) ListInactiveDeploymentsLessThanTime(ctx context.Context, opt *i.ListInactiveDeploymentsLessThanTimeOptions) ([]*ent.Deployment, error) {

Diff for: internal/pkg/store/deployment_test.go

+29-6
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ func TestStore_SearchDeployments(t *testing.T) {
4343
SetRef("main").
4444
SetEnv("local").
4545
SetStatus(deployment.StatusCreated).
46+
SetProductionEnvironment(true).
4647
SetUserID(1).
4748
SetRepoID(1).
4849
SaveX(ctx)
@@ -99,7 +100,29 @@ func TestStore_SearchDeployments(t *testing.T) {
99100
}
100101
})
101102

102-
t.Run("Returns waiting deployments under the accessible repositories.", func(t *testing.T) {
103+
t.Run("Returns all deployment triggered by myself.", func(t *testing.T) {
104+
store := NewStore(client)
105+
106+
res, err := store.SearchDeploymentsOfUser(ctx,
107+
&ent.User{ID: 1},
108+
&i.SearchDeploymentsOfUserOptions{
109+
ListOptions: i.ListOptions{Page: 1, PerPage: 30},
110+
Statuses: []deployment.Status{},
111+
Owned: true,
112+
From: time.Now().UTC().Add(-time.Minute),
113+
To: time.Now().UTC(),
114+
})
115+
if err != nil {
116+
t.Fatalf("SearchDeployments return an error: %s", err)
117+
}
118+
119+
expected := 2
120+
if len(res) != expected {
121+
t.Fatalf("SearchDeployments = %v, wanted %v", res, expected)
122+
}
123+
})
124+
125+
t.Run("Returns deployments which are 'created'.", func(t *testing.T) {
103126
store := NewStore(client)
104127

105128
res, err := store.SearchDeploymentsOfUser(ctx,
@@ -121,25 +144,25 @@ func TestStore_SearchDeployments(t *testing.T) {
121144
}
122145
})
123146

124-
t.Run("Returns all deployment triggered by myself.", func(t *testing.T) {
147+
t.Run("Returns deployments for production environment.", func(t *testing.T) {
125148
store := NewStore(client)
126149

127150
res, err := store.SearchDeploymentsOfUser(ctx,
128151
&ent.User{ID: 1},
129152
&i.SearchDeploymentsOfUserOptions{
130153
ListOptions: i.ListOptions{Page: 1, PerPage: 30},
131-
Statuses: []deployment.Status{},
132-
Owned: true,
154+
Statuses: []deployment.Status{deployment.StatusCreated},
155+
Owned: false,
133156
From: time.Now().UTC().Add(-time.Minute),
134157
To: time.Now().UTC(),
135158
})
136159
if err != nil {
137160
t.Fatalf("SearchDeployments return an error: %s", err)
138161
}
139162

140-
expected := 2
163+
expected := 1
141164
if len(res) != expected {
142-
t.Fatalf("SearchDeployments = %v, wanted %v", res, expected)
165+
t.Fatalf("SearchDeployments = %v, wanted %v", len(res), expected)
143166
}
144167
})
145168
}

Diff for: internal/server/api/v1/search/search.go

+28-41
Original file line numberDiff line numberDiff line change
@@ -38,68 +38,54 @@ func (s *Search) SearchDeployments(c *gin.Context) {
3838
ctx := c.Request.Context()
3939

4040
var (
41-
statuses = c.DefaultQuery("statuses", "")
42-
owned = c.DefaultQuery("owned", "true")
43-
from = c.DefaultQuery("from", time.Now().Add(-activeDuration).Format(time.RFC3339))
44-
to = c.DefaultQuery("to", time.Now().Format(time.RFC3339))
45-
page = c.DefaultQuery("page", "1")
46-
perPage = c.DefaultQuery("per_page", "30")
47-
)
48-
49-
var (
50-
ss = make([]deployment.Status, 0)
51-
o bool
52-
f time.Time
53-
t time.Time
54-
p int
55-
pp int
56-
err error
41+
statuses = []deployment.Status{}
42+
owned bool
43+
productionOnly bool
44+
from, to time.Time
45+
page, perPage int
46+
err error
5747
)
5848

5949
// Validate query parameters.
60-
for _, st := range strings.Split(statuses, ",") {
61-
if st != "" {
62-
ss = append(ss, deployment.Status(st))
50+
for _, s := range strings.Split(c.DefaultQuery("statuses", ""), ",") {
51+
if s != "" {
52+
statuses = append(statuses, deployment.Status(s))
6353
}
6454
}
6555

66-
if o, err = strconv.ParseBool(owned); err != nil {
67-
gb.ResponseWithError(
68-
c,
69-
e.NewErrorWithMessage(e.ErrorCodeParameterInvalid, "The owned must be boolean.", err),
70-
)
56+
if owned, err = strconv.ParseBool(c.DefaultQuery("owned", "true")); err != nil {
57+
gb.ResponseWithError(c, e.NewErrorWithMessage(e.ErrorCodeParameterInvalid, "The owned must be boolean.", err))
58+
return
59+
}
60+
61+
if productionOnly, err = strconv.ParseBool(c.DefaultQuery("production_only", "false")); err != nil {
62+
gb.ResponseWithError(c, e.NewErrorWithMessage(e.ErrorCodeParameterInvalid, "The production must be boolean.", err))
7163
return
7264
}
7365

74-
if f, err = time.Parse(time.RFC3339, from); err != nil {
66+
if from, err = time.Parse(time.RFC3339, c.DefaultQuery("from", time.Now().Add(-activeDuration).Format(time.RFC3339))); err != nil {
7567
gb.ResponseWithError(
7668
c,
7769
e.NewErrorWithMessage(e.ErrorCodeParameterInvalid, "Invalid format of \"from\" parameter, RFC3339 format only.", err),
7870
)
7971
return
8072
}
8173

82-
if t, err = time.Parse(time.RFC3339, to); err != nil {
74+
if to, err = time.Parse(time.RFC3339, c.DefaultQuery("to", time.Now().Format(time.RFC3339))); err != nil {
8375
gb.ResponseWithError(
8476
c,
8577
e.NewErrorWithMessage(e.ErrorCodeParameterInvalid, "Invalid format of \"to\" parameter, RFC3339 format only.", err),
8678
)
8779
return
8880
}
8981

90-
if p, err = strconv.Atoi(page); err != nil {
91-
gb.ResponseWithError(
92-
c,
93-
e.NewErrorWithMessage(e.ErrorCodeParameterInvalid, "Invalid format of \"page\" parameter.", err),
94-
)
82+
if page, err = strconv.Atoi(c.DefaultQuery("page", "1")); err != nil {
83+
gb.ResponseWithError(c, e.NewErrorWithMessage(e.ErrorCodeParameterInvalid, "The page must be number.", err))
9584
return
9685
}
9786

98-
if pp, err = strconv.Atoi(perPage); err != nil {
99-
gb.ResponseWithError(
100-
c,
101-
e.NewErrorWithMessage(e.ErrorCodeParameterInvalid, "Invalid format of \"per_page\" parameter.", err),
102-
)
87+
if perPage, err = strconv.Atoi(c.DefaultQuery("per_page", "1")); err != nil {
88+
gb.ResponseWithError(c, e.NewErrorWithMessage(e.ErrorCodeParameterInvalid, "The per_page must be number.", err))
10389
return
10490
}
10591

@@ -112,11 +98,12 @@ func (s *Search) SearchDeployments(c *gin.Context) {
11298
u := v.(*ent.User)
11399

114100
if ds, err = s.i.SearchDeploymentsOfUser(ctx, u, &i.SearchDeploymentsOfUserOptions{
115-
ListOptions: i.ListOptions{Page: p, PerPage: pp},
116-
Statuses: ss,
117-
Owned: o,
118-
From: f,
119-
To: t,
101+
ListOptions: i.ListOptions{Page: page, PerPage: perPage},
102+
Statuses: statuses,
103+
Owned: owned,
104+
ProductionOnly: productionOnly,
105+
From: from,
106+
To: to,
120107
}); err != nil {
121108
s.log.Check(gb.GetZapLogLevel(err), "Failed to search deployments.").Write(zap.Error(err))
122109
gb.ResponseWithError(c, err)

0 commit comments

Comments
 (0)