Skip to content

Commit

Permalink
feat: get files by pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
hunjixin committed Mar 15, 2024
1 parent 0cc8567 commit 261ce91
Show file tree
Hide file tree
Showing 16 changed files with 888 additions and 115 deletions.
518 changes: 426 additions & 92 deletions api/jiaozifs.gen.go

Large diffs are not rendered by default.

66 changes: 66 additions & 0 deletions api/swagger.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1290,6 +1290,58 @@ paths:
420:
description: too many requests

/object/{owner}/{repository}/files:
parameters:
- in: path
name: owner
required: true
schema:
type: string
- in: path
name: repository
required: true
schema:
type: string
- in: query
name: refName
description: branch/tag to the ref
required: true
schema:
type: string
get:
tags:
- objects
operationId: getFiles
summary: get files by pattern
parameters:
- in: query
name: pattern
description: glob pattern for match file path
allowEmptyValue: true
schema:
type: string
- in: query
name: type
description: files to retrieve from wip/branch/tag/commit, default branch
required: true
schema:
$ref: "#/components/schemas/RefType"
responses:
200:
description: files list
content:
application/json:
schema:
type: array
items:
type: string
401:
description: Unauthorized
404:
description: object not found
420:
description: too many requests

/wip/{owner}/{repository}:
parameters:
- in: path
Expand Down Expand Up @@ -1565,6 +1617,20 @@ paths:
required: true
schema:
$ref: "#/components/schemas/RefType"
- in: query
name: recursive
description: recursive get entries (include sub files)
required: false
allowEmptyValue: true
schema:
type: boolean
- in: query
name: pattern
description: pattern to get files
required: false
allowEmptyValue: true
schema:
type: boolean
responses:
200:
description: commit
Expand Down
55 changes: 55 additions & 0 deletions controller/object_ctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -409,3 +409,58 @@ func (oct ObjectController) UploadObject(ctx context.Context, w *api.JiaozifsRes
Metadata: &api.ObjectUserMetadata{},
}, http.StatusCreated)
}

func (oct ObjectController) GetFiles(ctx context.Context, w *api.JiaozifsResponse, _ *http.Request, ownerName string, repositoryName string, params api.GetFilesParams) {
owner, err := oct.Repo.UserRepo().Get(ctx, models.NewGetUserParams().SetName(ownerName))
if err != nil {
w.Error(err)
return
}

repository, err := oct.Repo.RepositoryRepo().Get(ctx, models.NewGetRepoParams().SetOwnerID(owner.ID).SetName(repositoryName))
if err != nil {
w.Error(err)
return
}

if !oct.authorizeMember(ctx, w, repository.ID, rbac.Node{
Permission: rbac.Permission{
Action: rbacmodel.ListObjectsAction,
Resource: rbacmodel.RepoURArn(owner.ID.String(), repository.ID.String()),
},
}) {
return
}

operator, err := auth.GetOperator(ctx)
if err != nil {
w.Error(err)
return
}

workRepo, err := versionmgr.NewWorkRepositoryFromConfig(ctx, operator, repository, oct.Repo, oct.PublicStorageConfig)
if err != nil {
w.Error(err)
return
}

err = workRepo.CheckOut(ctx, versionmgr.WorkRepoState(params.Type), params.RefName)
if err != nil {
w.Error(err)
return
}

workTree, err := workRepo.RootTree(ctx)
if err != nil {
w.Error(err)
return
}

files, err := workTree.GetFiles(ctx, utils.StringValue(params.Pattern))
if err != nil {
w.Error(err)
return
}

w.JSON(files)
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ require (
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-openapi/jsonpointer v0.19.6 // indirect
github.com/gobwas/glob v0.2.3 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.3 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,8 @@ github.com/go-test/deep v1.0.8/go.mod h1:5C2ZWiW0ErCdrYzpqxLbTX7MG14M9iiw8DgHncV
github.com/go-test/deep v1.1.0 h1:WOcxcdHcvdgThNXjw0t76K42FXTU7HpNQWHpA2HHNlg=
github.com/go-test/deep v1.1.0/go.mod h1:5C2ZWiW0ErCdrYzpqxLbTX7MG14M9iiw8DgHncVwcsE=
github.com/go-yaml/yaml v2.1.0+incompatible/go.mod h1:w2MrLa16VYP0jy6N7M5kHaCkaLENm+P+Tv+MfurjSw0=
github.com/gobwas/glob v0.2.3 h1:A4xDbljILXROh+kObIiy5kIaPYD8e96x1tgBhUI5J+Y=
github.com/gobwas/glob v0.2.3/go.mod h1:d3Ez4x06l9bZtSvzIay5+Yzi0fmZzPgnTbPcKjJAkT8=
github.com/godbus/dbus/v5 v5.1.0 h1:4KLkAxT3aOY8Li4FRJe/KvhoNFFxo0m6fNuFUO8QJUk=
github.com/godbus/dbus/v5 v5.1.0/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
Expand Down
10 changes: 5 additions & 5 deletions integrationtest/commit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func GetEntriesInRefSpec(ctx context.Context, urlStr string) func(c convey.C) {
})

c.Convey("commit kitty first changes", func(_ convey.C) {
commitWip(ctx, client, userName, repoName, branchName, "test")
_ = commitWip(ctx, client, userName, repoName, branchName, "test")
})

c.Convey("get branch entries", func(c convey.C) {
Expand Down Expand Up @@ -232,7 +232,7 @@ func GetEntriesInRefSpec(ctx context.Context, urlStr string) func(c convey.C) {
createWip(ctx, client, userName, repoName, "main")
uploadObject(ctx, client, userName, repoName, "main", "a.dat") //delete\
uploadObject(ctx, client, userName, repoName, "main", "g/m.dat") //modify
commitWip(ctx, client, userName, repoName, "main", "test")
_ = commitWip(ctx, client, userName, repoName, "main", "test")
})

c.Convey("get commit entries", func(c convey.C) {
Expand Down Expand Up @@ -390,10 +390,10 @@ func GetCommitChangesSpec(ctx context.Context, urlStr string) func(c convey.C) {
createRepo(ctx, client, repoName, false)
createWip(ctx, client, userName, repoName, "main")
uploadObject(ctx, client, userName, repoName, "main", "m.dat")
commitWip(ctx, client, userName, repoName, "main", "test")
_ = commitWip(ctx, client, userName, repoName, "main", "test")

uploadObject(ctx, client, userName, repoName, "main", "g/x.dat")
commitWip(ctx, client, userName, repoName, "main", "test")
_ = commitWip(ctx, client, userName, repoName, "main", "test")

//delete
deleteObject(ctx, client, userName, repoName, "main", "g/x.dat")
Expand All @@ -404,7 +404,7 @@ func GetCommitChangesSpec(ctx context.Context, urlStr string) func(c convey.C) {

//insert
uploadObject(ctx, client, userName, repoName, "main", "g/m.dat")
commitWip(ctx, client, userName, repoName, "main", "test")
_ = commitWip(ctx, client, userName, repoName, "main", "test")

})
c.Convey("get commit change", func(c convey.C) {
Expand Down
6 changes: 5 additions & 1 deletion integrationtest/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,14 +205,18 @@ func createWip(ctx context.Context, client *api.Client, user string, repoName st
return result.JSON200
}

func commitWip(ctx context.Context, client *api.Client, user string, repoName string, refName string, msg string) {
func commitWip(ctx context.Context, client *api.Client, user string, repoName string, refName string, msg string) *api.Wip {
resp, err := client.CommitWip(ctx, user, repoName, &api.CommitWipParams{
RefName: refName,
Msg: msg,
})

convey.So(err, convey.ShouldBeNil)
convey.So(resp.StatusCode, convey.ShouldEqual, http.StatusCreated)

result, err := api.ParseCommitWipResponse(resp)
convey.So(err, convey.ShouldBeNil)
return result.JSON201
}

func createMergeRequest(ctx context.Context, client *api.Client, user string, repoName string, sourceBranch string, targetBranch string) *api.MergeRequest {
Expand Down
4 changes: 2 additions & 2 deletions integrationtest/merge_request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func MergeRequestSpec(ctx context.Context, urlStr string) func(c convey.C) {
_ = createBranch(ctx, client, userName, repoName, "main", branchName)
_ = createWip(ctx, client, userName, repoName, branchName)
_ = uploadObject(ctx, client, userName, repoName, branchName, "a.bin")
commitWip(ctx, client, userName, repoName, branchName, "test")
_ = commitWip(ctx, client, userName, repoName, branchName, "test")
})

c.Convey("create merge request", func(c convey.C) {
Expand Down Expand Up @@ -260,7 +260,7 @@ func MergeRequestSpec(ctx context.Context, urlStr string) func(c convey.C) {
createBranch(ctx, client, userName, repoName, "main", branchName)
createWip(ctx, client, userName, repoName, branchName)
uploadObject(ctx, client, userName, repoName, branchName, fmt.Sprintf("%d.txt", i))
commitWip(ctx, client, userName, repoName, branchName, "test")
_ = commitWip(ctx, client, userName, repoName, branchName, "test")
createMergeRequest(ctx, client, userName, repoName, branchName, "main")
}
})
Expand Down
90 changes: 89 additions & 1 deletion integrationtest/objects_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"io"
"net/http"

"github.com/GitDataAI/jiaozifs/utils"

"github.com/GitDataAI/jiaozifs/api"
apiimpl "github.com/GitDataAI/jiaozifs/api/api_impl"
"github.com/GitDataAI/jiaozifs/utils/hash"
Expand All @@ -28,6 +30,7 @@ func ObjectSpec(ctx context.Context, urlStr string) func(c convey.C) {
_ = createBranch(ctx, client, userName, repoName, "main", branchName)
_ = createWip(ctx, client, userName, repoName, branchName)
})

c.Convey("upload object", func(c convey.C) {
c.Convey("no auth", func() {
re := client.RequestEditors
Expand Down Expand Up @@ -124,7 +127,7 @@ func ObjectSpec(ctx context.Context, urlStr string) func(c convey.C) {

//commit object to branch
c.Convey("commit object to branch", func(_ convey.C) {
commitWip(ctx, client, userName, repoName, branchName, "test commit msg")
_ = commitWip(ctx, client, userName, repoName, branchName, "test commit msg")
})

c.Convey("head object", func(c convey.C) {
Expand Down Expand Up @@ -304,5 +307,90 @@ func ObjectSpec(ctx context.Context, urlStr string) func(c convey.C) {
convey.So(etag, convey.ShouldEqual, exectEtag)
})
})

c.Convey("get files", func(c convey.C) {
repoName := "testGetFiles"
branchName := "ggct"
c.Convey("init", func() {
_ = createRepo(ctx, client, repoName, false)
_ = createBranch(ctx, client, userName, repoName, "main", branchName)
})
c.Convey("no auth", func() {
re := client.RequestEditors
client.RequestEditors = nil
resp, err := client.GetFiles(ctx, userName, repoName, &api.GetFilesParams{
RefName: "main",
Pattern: utils.String("*"),
Type: api.RefTypeBranch,
})
client.RequestEditors = re
convey.So(err, convey.ShouldBeNil)
convey.So(resp.StatusCode, convey.ShouldEqual, http.StatusUnauthorized)
})

c.Convey("fail to get object in non exit user", func() {
resp, err := client.GetFiles(ctx, "fakeuser", repoName, &api.GetFilesParams{
RefName: "main",
Pattern: utils.String("*"),
Type: api.RefTypeBranch,
})
convey.So(err, convey.ShouldBeNil)
convey.So(resp.StatusCode, convey.ShouldEqual, http.StatusNotFound)
})

c.Convey("fail to get object in non exit repo", func() {
resp, err := client.GetFiles(ctx, userName, "fakerepo", &api.GetFilesParams{
RefName: "main",
Pattern: utils.String("*"),
Type: api.RefTypeBranch,
})
convey.So(err, convey.ShouldBeNil)
convey.So(resp.StatusCode, convey.ShouldEqual, http.StatusNotFound)
})

c.Convey("fail to get object in non exit branch", func() {
resp, err := client.GetFiles(ctx, userName, repoName, &api.GetFilesParams{
RefName: "main_bak",
Pattern: utils.String("*"),
Type: api.RefTypeBranch,
})
convey.So(err, convey.ShouldBeNil)
convey.So(resp.StatusCode, convey.ShouldEqual, http.StatusNotFound)
})

c.Convey("forbidden get object in others", func() {
resp, err := client.GetFiles(ctx, "jimmy", "happygo", &api.GetFilesParams{
RefName: "main",
Pattern: utils.String("*"),
Type: api.RefTypeBranch,
})
convey.So(err, convey.ShouldBeNil)
convey.So(resp.StatusCode, convey.ShouldEqual, http.StatusUnauthorized)
})

c.Convey("list success", func() {
_ = createWip(ctx, client, userName, repoName, branchName)
_ = uploadObject(ctx, client, userName, repoName, branchName, "a/b.txt")
_ = uploadObject(ctx, client, userName, repoName, branchName, "a/e.txt")
_ = uploadObject(ctx, client, userName, repoName, branchName, "a/g.txt")
_ = commitWip(ctx, client, userName, repoName, branchName, "wip")

resp, err := client.GetFiles(ctx, userName, repoName, &api.GetFilesParams{
RefName: branchName,
Pattern: utils.String("a/*"),
Type: api.RefTypeBranch,
})
convey.So(err, convey.ShouldBeNil)
convey.So(resp.StatusCode, convey.ShouldEqual, http.StatusOK)

result, err := api.ParseGetFilesResponse(resp)
convey.So(err, convey.ShouldBeNil)
convey.ShouldHaveLength(3, *result.JSON200)
convey.ShouldEqual("a/b.txt", (*result.JSON200)[0])
convey.ShouldEqual("a/e.txt", (*result.JSON200)[1])
convey.ShouldEqual("a/g.txt", (*result.JSON200)[2])
})

})
}
}
6 changes: 3 additions & 3 deletions integrationtest/repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ func RepoSpec(ctx context.Context, urlStr string) func(c convey.C) {
c.Convey("add commit to branch", func(_ convey.C) {
createWip(ctx, client, userName, repoName, controller.DefaultBranchName)
uploadObject(ctx, client, userName, repoName, controller.DefaultBranchName, "a.txt")
commitWip(ctx, client, userName, repoName, controller.DefaultBranchName, "first commit")
_ = commitWip(ctx, client, userName, repoName, controller.DefaultBranchName, "first commit")
})

c.Convey("success get commits", func() {
Expand All @@ -424,9 +424,9 @@ func RepoSpec(ctx context.Context, urlStr string) func(c convey.C) {

c.Convey("add double commit to branch", func(_ convey.C) {
uploadObject(ctx, client, userName, repoName, controller.DefaultBranchName, "b.txt")
commitWip(ctx, client, userName, repoName, controller.DefaultBranchName, "second commit")
_ = commitWip(ctx, client, userName, repoName, controller.DefaultBranchName, "second commit")
uploadObject(ctx, client, userName, repoName, controller.DefaultBranchName, "c.txt")
commitWip(ctx, client, userName, repoName, controller.DefaultBranchName, "third commit")
_ = commitWip(ctx, client, userName, repoName, controller.DefaultBranchName, "third commit")
})

c.Convey("success get commits by params", func() {
Expand Down
4 changes: 2 additions & 2 deletions integrationtest/wip_object_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ func WipObjectSpec(ctx context.Context, urlStr string) func(c convey.C) {
convey.So(resp.StatusCode, convey.ShouldEqual, http.StatusOK)

c.Convey("commit changes", func() {
commitWip(ctx, client, userName, repoName, branchName, "test")
_ = commitWip(ctx, client, userName, repoName, branchName, "test")
})

//ensure not exit
Expand Down Expand Up @@ -482,7 +482,7 @@ func UpdateWipSpec(ctx context.Context, urlStr string) func(c convey.C) {

//make wip base commit has value
_ = uploadObject(ctx, client, userName, repoName, branchName, "a.txt")
commitWip(ctx, client, userName, repoName, branchName, "test")
_ = commitWip(ctx, client, userName, repoName, branchName, "test")

_ = uploadObject(ctx, client, userName, repoName, branchName, "m.dat")
_ = uploadObject(ctx, client, userName, repoName, branchName, "g/m.dat")
Expand Down
2 changes: 1 addition & 1 deletion makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ ifneq ($(strip $(LDFLAGS)),)
ldflags+=-extldflags=$(LDFLAGS)
endif

GOFLAGS+=-ldflags="$(ldflags)"
GOFLAGS+=-ldflags=$(ldflags)

gen-api: ./api/swagger.yml ./api/tmpls/chi
$(GOGENERATE) ./api
Expand Down
Loading

0 comments on commit 261ce91

Please sign in to comment.