Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
188 changes: 113 additions & 75 deletions api/jiaozifs.gen.go

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions api/swagger.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1205,6 +1205,12 @@ paths:
- repo
operationId: listRepository
summary: list repository in specific owner
parameters:
- in: query
name: repoPrefix
required: false
schema:
type: string
responses:
200:
description: repository list
Expand Down
8 changes: 6 additions & 2 deletions controller/repository_ctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func (repositoryCtl RepositoryController) ListRepositoryOfAuthenticatedUser(ctx
w.JSON(repositories)
}

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

repositories, err := repositoryCtl.Repo.RepositoryRepo().List(ctx, models.NewListRepoParams().SetOwnerID(owner.ID))
listParams := models.NewListRepoParams().SetOwnerID(owner.ID)
if params.RepoPrefix != nil && len(*params.RepoPrefix) > 0 {
listParams.SetName(*params.RepoPrefix, models.PrefixMatch)
}
repositories, err := repositoryCtl.Repo.RepositoryRepo().List(ctx, listParams)
if err != nil {
w.Error(err)
return
Expand Down
53 changes: 42 additions & 11 deletions integrationtest/repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ func RepoSpec(ctx context.Context, urlStr string) func(c convey.C) {
Email: "mock@gmail.com",
})
convey.So(err, convey.ShouldBeNil)
convey.So(http.StatusOK, convey.ShouldEqual, resp.StatusCode)
convey.So(resp.StatusCode, convey.ShouldEqual, http.StatusOK)

loginResp, err := client.Login(ctx, api.LoginJSONRequestBody{
Username: userName,
Password: "12345678",
})
convey.So(err, convey.ShouldBeNil)
convey.So(http.StatusOK, convey.ShouldEqual, loginResp.StatusCode)
convey.So(loginResp.StatusCode, convey.ShouldEqual, http.StatusOK)

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

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

grp, err := api.ParseGetRepositoryResponse(resp)
convey.So(err, convey.ShouldBeNil)
convey.So(controller.DefaultBranchName, convey.ShouldEqual, grp.JSON200.Head)
convey.So(grp.JSON200.Head, convey.ShouldEqual, controller.DefaultBranchName)
fmt.Println(grp.JSON200.ID)
//check default branch created
branchResp, err := client.GetBranch(ctx, userName, grp.JSON200.Name, &api.GetBranchParams{RefName: controller.DefaultBranchName})
convey.So(err, convey.ShouldBeNil)
convey.So(http.StatusOK, convey.ShouldEqual, branchResp.StatusCode)
convey.So(branchResp.StatusCode, convey.ShouldEqual, http.StatusOK)

brp, err := api.ParseGetBranchResponse(branchResp)
convey.So(err, convey.ShouldBeNil)
convey.So(controller.DefaultBranchName, convey.ShouldEqual, brp.JSON200.Name)
convey.So(brp.JSON200.Name, convey.ShouldEqual, controller.DefaultBranchName)
})

c.Convey("add second repo ", func() {
resp, err := client.CreateRepository(ctx, api.CreateRepository{
Description: utils.String("test resp"),
Name: "happygo",
})
convey.So(err, convey.ShouldBeNil)
convey.So(resp.StatusCode, convey.ShouldEqual, http.StatusOK)
})

c.Convey("duplicate repo", func() {
Expand All @@ -80,18 +89,40 @@ func RepoSpec(ctx context.Context, urlStr string) func(c convey.C) {
Name: "happyrun",
})
convey.So(err, convey.ShouldBeNil)
convey.So(http.StatusInternalServerError, convey.ShouldEqual, resp.StatusCode)
convey.So(resp.StatusCode, convey.ShouldEqual, http.StatusInternalServerError)
})

c.Convey("list repository", func() {
resp, err := client.ListRepository(ctx, userName)
resp, err := client.ListRepository(ctx, userName, &api.ListRepositoryParams{})
convey.So(err, convey.ShouldBeNil)
convey.So(resp.StatusCode, convey.ShouldEqual, http.StatusOK)

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

convey.So(len(*listRepos.JSON200), convey.ShouldEqual, 2)
})

c.Convey("list repository by prefix", func() {
resp, err := client.ListRepository(ctx, userName, &api.ListRepositoryParams{RepoPrefix: utils.String("happy")})
convey.So(err, convey.ShouldBeNil)
convey.So(resp.StatusCode, convey.ShouldEqual, http.StatusOK)

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

convey.So(len(*listRepos.JSON200), convey.ShouldEqual, 2)
})

c.Convey("list repository by prefix but found nothing", func() {
resp, err := client.ListRepository(ctx, userName, &api.ListRepositoryParams{RepoPrefix: utils.String("bad")})
convey.So(err, convey.ShouldBeNil)
convey.So(http.StatusOK, convey.ShouldEqual, resp.StatusCode)
convey.So(resp.StatusCode, convey.ShouldEqual, http.StatusOK)

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

convey.So(len(*listRepos.JSON200), convey.ShouldEqual, 1)
convey.So(len(*listRepos.JSON200), convey.ShouldEqual, 0)
})
}
}
9 changes: 9 additions & 0 deletions models/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ import (
"github.com/uptrace/bun"
)

type MatchMode int

const (
ExactMatch MatchMode = iota
PrefixMatch
SuffixMatch
LikeMatch
)

type TxOption func(*sql.TxOptions)

func IsolationLevelOption(level sql.IsolationLevel) TxOption {
Expand Down
27 changes: 25 additions & 2 deletions models/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (
type Repository struct {
bun.BaseModel `bun:"table:repositories"`
ID uuid.UUID `bun:"id,pk,type:uuid,default:uuid_generate_v4()"`
Name string `bun:"name,unique,notnull"`
OwnerID uuid.UUID `bun:"owner_id,unique,type:uuid,notnull"`
Name string `bun:"name,unique:name_owner_unique,notnull"`
OwnerID uuid.UUID `bun:"owner_id,unique:name_owner_unique,type:uuid,notnull"`
HEAD string `bun:"head,notnull"`
Description *string `bun:"description"`
CreatorID uuid.UUID `bun:"creator_id,type:uuid,notnull"`
Expand Down Expand Up @@ -56,6 +56,8 @@ type ListRepoParams struct {
ID uuid.UUID
CreatorID uuid.UUID
OwnerID uuid.UUID
Name *string
NameMatch MatchMode
}

func NewListRepoParams() *ListRepoParams {
Expand All @@ -70,6 +72,13 @@ func (lrp *ListRepoParams) SetOwnerID(ownerID uuid.UUID) *ListRepoParams {
lrp.OwnerID = ownerID
return lrp
}

func (lrp *ListRepoParams) SetName(name string, match MatchMode) *ListRepoParams {
lrp.Name = &name
lrp.NameMatch = match
return lrp
}

func (lrp *ListRepoParams) SetCreatorID(creatorID uuid.UUID) *ListRepoParams {
lrp.CreatorID = creatorID
return lrp
Expand Down Expand Up @@ -166,6 +175,20 @@ func (r *RepositoryRepo) List(ctx context.Context, params *ListRepoParams) ([]*R
if uuid.Nil != params.OwnerID {
query = query.Where("owner_id = ?", params.OwnerID)
}

if params.Name != nil {
switch params.NameMatch {
case ExactMatch:
query = query.Where("name = ?", *params.Name)
case PrefixMatch:
query = query.Where("name LIKE ?", *params.Name+"%")
case SuffixMatch:
query = query.Where("name LIKE ?", "%"+*params.Name)
case LikeMatch:
query = query.Where("name LIKE ?", "%"+*params.Name+"%")
}
}

return repos, query.Scan(ctx)
}

Expand Down
35 changes: 34 additions & 1 deletion models/repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func TestRepositoryRepo_Insert(t *testing.T) {

repoModel := &models.Repository{}
require.NoError(t, gofakeit.Struct(repoModel))
repoModel.Name = "aaabbbb"
newRepo, err := repo.Insert(ctx, repoModel)
require.NoError(t, err)
require.NotEqual(t, uuid.Nil, newRepo.ID)
Expand All @@ -39,15 +40,47 @@ func TestRepositoryRepo_Insert(t *testing.T) {
secModel := &models.Repository{}
require.NoError(t, gofakeit.Struct(secModel))
secModel.CreatorID = repoModel.CreatorID
secModel.Name = "adabbeb"
secRepo, err := repo.Insert(ctx, secModel)
require.NoError(t, err)
require.NotEqual(t, uuid.Nil, secRepo.ID)

//list
repos, err := repo.List(ctx, models.NewListRepoParams().SetCreatorID(secModel.CreatorID))
repos, err := repo.List(ctx, models.NewListRepoParams())
require.NoError(t, err)
require.Len(t, repos, 2)

{
//exact adabbeb
repos, err := repo.List(ctx, models.NewListRepoParams().SetCreatorID(secModel.CreatorID).SetName("adabbeb", models.PrefixMatch))
require.NoError(t, err)
require.Len(t, repos, 1)
}
{
//prefix a
repos, err := repo.List(ctx, models.NewListRepoParams().SetCreatorID(secModel.CreatorID).SetName("a", models.PrefixMatch))
require.NoError(t, err)
require.Len(t, repos, 2)
}

{
//subfix b
repos, err := repo.List(ctx, models.NewListRepoParams().SetCreatorID(secModel.CreatorID).SetName("b", models.SuffixMatch))
require.NoError(t, err)
require.Len(t, repos, 2)
}
{
//like ab
repos, err := repo.List(ctx, models.NewListRepoParams().SetCreatorID(secModel.CreatorID).SetName("ab", models.LikeMatch))
require.NoError(t, err)
require.Len(t, repos, 2)
}
{
//like ab
repos, err := repo.List(ctx, models.NewListRepoParams().SetCreatorID(secModel.CreatorID).SetName("adabbeb", models.LikeMatch))
require.NoError(t, err)
require.Len(t, repos, 1)
}
//delete
err = repo.Delete(ctx, models.NewDeleteRepoParams().SetID(secRepo.ID))
require.NoError(t, err)
Expand Down