Skip to content

Commit c5ae68f

Browse files
authored
Merge pull request #54 from jiaozifs/feat/repo_like_match
feat: support like match in listreposotory
2 parents c5a7d11 + d6750e9 commit c5ae68f

File tree

7 files changed

+235
-91
lines changed

7 files changed

+235
-91
lines changed

api/jiaozifs.gen.go

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

api/swagger.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1205,6 +1205,12 @@ paths:
12051205
- repo
12061206
operationId: listRepository
12071207
summary: list repository in specific owner
1208+
parameters:
1209+
- in: query
1210+
name: repoPrefix
1211+
required: false
1212+
schema:
1213+
type: string
12081214
responses:
12091215
200:
12101216
description: repository list

controller/repository_ctl.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func (repositoryCtl RepositoryController) ListRepositoryOfAuthenticatedUser(ctx
6868
w.JSON(repositories)
6969
}
7070

71-
func (repositoryCtl RepositoryController) ListRepository(ctx context.Context, w *api.JiaozifsResponse, _ *http.Request, ownerName string) {
71+
func (repositoryCtl RepositoryController) ListRepository(ctx context.Context, w *api.JiaozifsResponse, _ *http.Request, ownerName string, params api.ListRepositoryParams) {
7272
owner, err := repositoryCtl.Repo.UserRepo().Get(ctx, models.NewGetUserParams().SetName(ownerName))
7373
if err != nil {
7474
w.Error(err)
@@ -85,7 +85,11 @@ func (repositoryCtl RepositoryController) ListRepository(ctx context.Context, w
8585
return
8686
}
8787

88-
repositories, err := repositoryCtl.Repo.RepositoryRepo().List(ctx, models.NewListRepoParams().SetOwnerID(owner.ID))
88+
listParams := models.NewListRepoParams().SetOwnerID(owner.ID)
89+
if params.RepoPrefix != nil && len(*params.RepoPrefix) > 0 {
90+
listParams.SetName(*params.RepoPrefix, models.PrefixMatch)
91+
}
92+
repositories, err := repositoryCtl.Repo.RepositoryRepo().List(ctx, listParams)
8993
if err != nil {
9094
w.Error(err)
9195
return

integrationtest/repo_test.go

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@ func RepoSpec(ctx context.Context, urlStr string) func(c convey.C) {
2626
2727
})
2828
convey.So(err, convey.ShouldBeNil)
29-
convey.So(http.StatusOK, convey.ShouldEqual, resp.StatusCode)
29+
convey.So(resp.StatusCode, convey.ShouldEqual, http.StatusOK)
3030

3131
loginResp, err := client.Login(ctx, api.LoginJSONRequestBody{
3232
Username: userName,
3333
Password: "12345678",
3434
})
3535
convey.So(err, convey.ShouldBeNil)
36-
convey.So(http.StatusOK, convey.ShouldEqual, loginResp.StatusCode)
36+
convey.So(loginResp.StatusCode, convey.ShouldEqual, http.StatusOK)
3737

3838
client.RequestEditors = append(client.RequestEditors, func(ctx context.Context, req *http.Request) error {
3939
for _, cookie := range loginResp.Cookies() {
@@ -49,7 +49,7 @@ func RepoSpec(ctx context.Context, urlStr string) func(c convey.C) {
4949
Name: "repo",
5050
})
5151
convey.So(err, convey.ShouldBeNil)
52-
convey.So(http.StatusBadRequest, convey.ShouldEqual, resp.StatusCode)
52+
convey.So(resp.StatusCode, convey.ShouldEqual, http.StatusBadRequest)
5353
})
5454

5555
c.Convey("success create repo name", func() {
@@ -58,20 +58,29 @@ func RepoSpec(ctx context.Context, urlStr string) func(c convey.C) {
5858
Name: "happyrun",
5959
})
6060
convey.So(err, convey.ShouldBeNil)
61-
convey.So(http.StatusOK, convey.ShouldEqual, resp.StatusCode)
61+
convey.So(resp.StatusCode, convey.ShouldEqual, http.StatusOK)
6262

6363
grp, err := api.ParseGetRepositoryResponse(resp)
6464
convey.So(err, convey.ShouldBeNil)
65-
convey.So(controller.DefaultBranchName, convey.ShouldEqual, grp.JSON200.Head)
65+
convey.So(grp.JSON200.Head, convey.ShouldEqual, controller.DefaultBranchName)
6666
fmt.Println(grp.JSON200.ID)
6767
//check default branch created
6868
branchResp, err := client.GetBranch(ctx, userName, grp.JSON200.Name, &api.GetBranchParams{RefName: controller.DefaultBranchName})
6969
convey.So(err, convey.ShouldBeNil)
70-
convey.So(http.StatusOK, convey.ShouldEqual, branchResp.StatusCode)
70+
convey.So(branchResp.StatusCode, convey.ShouldEqual, http.StatusOK)
7171

7272
brp, err := api.ParseGetBranchResponse(branchResp)
7373
convey.So(err, convey.ShouldBeNil)
74-
convey.So(controller.DefaultBranchName, convey.ShouldEqual, brp.JSON200.Name)
74+
convey.So(brp.JSON200.Name, convey.ShouldEqual, controller.DefaultBranchName)
75+
})
76+
77+
c.Convey("add second repo ", func() {
78+
resp, err := client.CreateRepository(ctx, api.CreateRepository{
79+
Description: utils.String("test resp"),
80+
Name: "happygo",
81+
})
82+
convey.So(err, convey.ShouldBeNil)
83+
convey.So(resp.StatusCode, convey.ShouldEqual, http.StatusOK)
7584
})
7685

7786
c.Convey("duplicate repo", func() {
@@ -80,18 +89,40 @@ func RepoSpec(ctx context.Context, urlStr string) func(c convey.C) {
8089
Name: "happyrun",
8190
})
8291
convey.So(err, convey.ShouldBeNil)
83-
convey.So(http.StatusInternalServerError, convey.ShouldEqual, resp.StatusCode)
92+
convey.So(resp.StatusCode, convey.ShouldEqual, http.StatusInternalServerError)
8493
})
8594

8695
c.Convey("list repository", func() {
87-
resp, err := client.ListRepository(ctx, userName)
96+
resp, err := client.ListRepository(ctx, userName, &api.ListRepositoryParams{})
97+
convey.So(err, convey.ShouldBeNil)
98+
convey.So(resp.StatusCode, convey.ShouldEqual, http.StatusOK)
99+
100+
listRepos, err := api.ParseListRepositoryResponse(resp)
101+
convey.So(err, convey.ShouldBeNil)
102+
103+
convey.So(len(*listRepos.JSON200), convey.ShouldEqual, 2)
104+
})
105+
106+
c.Convey("list repository by prefix", func() {
107+
resp, err := client.ListRepository(ctx, userName, &api.ListRepositoryParams{RepoPrefix: utils.String("happy")})
108+
convey.So(err, convey.ShouldBeNil)
109+
convey.So(resp.StatusCode, convey.ShouldEqual, http.StatusOK)
110+
111+
listRepos, err := api.ParseListRepositoryResponse(resp)
112+
convey.So(err, convey.ShouldBeNil)
113+
114+
convey.So(len(*listRepos.JSON200), convey.ShouldEqual, 2)
115+
})
116+
117+
c.Convey("list repository by prefix but found nothing", func() {
118+
resp, err := client.ListRepository(ctx, userName, &api.ListRepositoryParams{RepoPrefix: utils.String("bad")})
88119
convey.So(err, convey.ShouldBeNil)
89-
convey.So(http.StatusOK, convey.ShouldEqual, resp.StatusCode)
120+
convey.So(resp.StatusCode, convey.ShouldEqual, http.StatusOK)
90121

91122
listRepos, err := api.ParseListRepositoryResponse(resp)
92123
convey.So(err, convey.ShouldBeNil)
93124

94-
convey.So(len(*listRepos.JSON200), convey.ShouldEqual, 1)
125+
convey.So(len(*listRepos.JSON200), convey.ShouldEqual, 0)
95126
})
96127
}
97128
}

models/repo.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,15 @@ import (
77
"github.com/uptrace/bun"
88
)
99

10+
type MatchMode int
11+
12+
const (
13+
ExactMatch MatchMode = iota
14+
PrefixMatch
15+
SuffixMatch
16+
LikeMatch
17+
)
18+
1019
type TxOption func(*sql.TxOptions)
1120

1221
func IsolationLevelOption(level sql.IsolationLevel) TxOption {

models/repository.go

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import (
1111
type Repository struct {
1212
bun.BaseModel `bun:"table:repositories"`
1313
ID uuid.UUID `bun:"id,pk,type:uuid,default:uuid_generate_v4()"`
14-
Name string `bun:"name,unique,notnull"`
15-
OwnerID uuid.UUID `bun:"owner_id,unique,type:uuid,notnull"`
14+
Name string `bun:"name,unique:name_owner_unique,notnull"`
15+
OwnerID uuid.UUID `bun:"owner_id,unique:name_owner_unique,type:uuid,notnull"`
1616
HEAD string `bun:"head,notnull"`
1717
Description *string `bun:"description"`
1818
CreatorID uuid.UUID `bun:"creator_id,type:uuid,notnull"`
@@ -56,6 +56,8 @@ type ListRepoParams struct {
5656
ID uuid.UUID
5757
CreatorID uuid.UUID
5858
OwnerID uuid.UUID
59+
Name *string
60+
NameMatch MatchMode
5961
}
6062

6163
func NewListRepoParams() *ListRepoParams {
@@ -70,6 +72,13 @@ func (lrp *ListRepoParams) SetOwnerID(ownerID uuid.UUID) *ListRepoParams {
7072
lrp.OwnerID = ownerID
7173
return lrp
7274
}
75+
76+
func (lrp *ListRepoParams) SetName(name string, match MatchMode) *ListRepoParams {
77+
lrp.Name = &name
78+
lrp.NameMatch = match
79+
return lrp
80+
}
81+
7382
func (lrp *ListRepoParams) SetCreatorID(creatorID uuid.UUID) *ListRepoParams {
7483
lrp.CreatorID = creatorID
7584
return lrp
@@ -166,6 +175,20 @@ func (r *RepositoryRepo) List(ctx context.Context, params *ListRepoParams) ([]*R
166175
if uuid.Nil != params.OwnerID {
167176
query = query.Where("owner_id = ?", params.OwnerID)
168177
}
178+
179+
if params.Name != nil {
180+
switch params.NameMatch {
181+
case ExactMatch:
182+
query = query.Where("name = ?", *params.Name)
183+
case PrefixMatch:
184+
query = query.Where("name LIKE ?", *params.Name+"%")
185+
case SuffixMatch:
186+
query = query.Where("name LIKE ?", "%"+*params.Name)
187+
case LikeMatch:
188+
query = query.Where("name LIKE ?", "%"+*params.Name+"%")
189+
}
190+
}
191+
169192
return repos, query.Scan(ctx)
170193
}
171194

models/repository_test.go

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ func TestRepositoryRepo_Insert(t *testing.T) {
2121

2222
repoModel := &models.Repository{}
2323
require.NoError(t, gofakeit.Struct(repoModel))
24+
repoModel.Name = "aaabbbb"
2425
newRepo, err := repo.Insert(ctx, repoModel)
2526
require.NoError(t, err)
2627
require.NotEqual(t, uuid.Nil, newRepo.ID)
@@ -39,15 +40,47 @@ func TestRepositoryRepo_Insert(t *testing.T) {
3940
secModel := &models.Repository{}
4041
require.NoError(t, gofakeit.Struct(secModel))
4142
secModel.CreatorID = repoModel.CreatorID
43+
secModel.Name = "adabbeb"
4244
secRepo, err := repo.Insert(ctx, secModel)
4345
require.NoError(t, err)
4446
require.NotEqual(t, uuid.Nil, secRepo.ID)
4547

4648
//list
47-
repos, err := repo.List(ctx, models.NewListRepoParams().SetCreatorID(secModel.CreatorID))
49+
repos, err := repo.List(ctx, models.NewListRepoParams())
4850
require.NoError(t, err)
4951
require.Len(t, repos, 2)
5052

53+
{
54+
//exact adabbeb
55+
repos, err := repo.List(ctx, models.NewListRepoParams().SetCreatorID(secModel.CreatorID).SetName("adabbeb", models.PrefixMatch))
56+
require.NoError(t, err)
57+
require.Len(t, repos, 1)
58+
}
59+
{
60+
//prefix a
61+
repos, err := repo.List(ctx, models.NewListRepoParams().SetCreatorID(secModel.CreatorID).SetName("a", models.PrefixMatch))
62+
require.NoError(t, err)
63+
require.Len(t, repos, 2)
64+
}
65+
66+
{
67+
//subfix b
68+
repos, err := repo.List(ctx, models.NewListRepoParams().SetCreatorID(secModel.CreatorID).SetName("b", models.SuffixMatch))
69+
require.NoError(t, err)
70+
require.Len(t, repos, 2)
71+
}
72+
{
73+
//like ab
74+
repos, err := repo.List(ctx, models.NewListRepoParams().SetCreatorID(secModel.CreatorID).SetName("ab", models.LikeMatch))
75+
require.NoError(t, err)
76+
require.Len(t, repos, 2)
77+
}
78+
{
79+
//like ab
80+
repos, err := repo.List(ctx, models.NewListRepoParams().SetCreatorID(secModel.CreatorID).SetName("adabbeb", models.LikeMatch))
81+
require.NoError(t, err)
82+
require.Len(t, repos, 1)
83+
}
5184
//delete
5285
err = repo.Delete(ctx, models.NewDeleteRepoParams().SetID(secRepo.ID))
5386
require.NoError(t, err)

0 commit comments

Comments
 (0)