Skip to content

Commit 52de00c

Browse files
committed
Add multiple files API endpoint
1 parent e1c2d05 commit 52de00c

File tree

4 files changed

+78
-1
lines changed

4 files changed

+78
-1
lines changed

modules/structs/repo_file.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,3 +176,11 @@ type FileDeleteResponse struct {
176176
Commit *FileCommitResponse `json:"commit"`
177177
Verification *PayloadCommitVerification `json:"verification"`
178178
}
179+
180+
// GetFilesOptions options for retrieving metadate and content of multiple files
181+
type GetFilesOptions struct {
182+
Files []string `json:"files" binding:"Required"`
183+
}
184+
185+
// FilesList contains multiple items if ContentsResponse
186+
type FilesList []*ContentsResponse

routers/api/v1/api.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1382,6 +1382,7 @@ func Routes() *web.Router {
13821382
m.Group("/contents", func() {
13831383
m.Get("", repo.GetContentsList)
13841384
m.Post("", reqToken(), bind(api.ChangeFilesOptions{}), reqRepoBranchWriter, mustNotBeArchived, repo.ChangeFiles)
1385+
m.Post("/files", bind(api.GetFilesOptions{}), repo.GetContentsFiles)
13851386
m.Get("/*", repo.GetContents)
13861387
m.Group("/*", func() {
13871388
m.Post("", bind(api.CreateFileOptions{}), reqRepoBranchWriter, mustNotBeArchived, repo.CreateFile)

routers/api/v1/repo/file.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ import (
2525
"code.gitea.io/gitea/modules/setting"
2626
"code.gitea.io/gitea/modules/storage"
2727
api "code.gitea.io/gitea/modules/structs"
28+
"code.gitea.io/gitea/modules/util"
2829
"code.gitea.io/gitea/modules/web"
30+
"code.gitea.io/gitea/routers/api/v1/utils"
2931
"code.gitea.io/gitea/routers/common"
3032
"code.gitea.io/gitea/services/context"
3133
pull_service "code.gitea.io/gitea/services/pull"
@@ -982,3 +984,64 @@ func GetContentsList(ctx *context.APIContext) {
982984
// same as GetContents(), this function is here because swagger fails if path is empty in GetContents() interface
983985
GetContents(ctx)
984986
}
987+
988+
// GetContentsFiles Get the metadata and contents of requested files
989+
func GetContentsFiles(ctx *context.APIContext) {
990+
// swagger:operation POST /repos/{owner}/{repo}/contents/files repository repoGetContentsFiles
991+
// ---
992+
// summary: Get the metadata and contents of requested files
993+
// produces:
994+
// - application/json
995+
// parameters:
996+
// - name: owner
997+
// in: path
998+
// description: owner of the repo
999+
// type: string
1000+
// required: true
1001+
// - name: repo
1002+
// in: path
1003+
// description: name of the repo
1004+
// type: string
1005+
// required: true
1006+
// - name: ref
1007+
// in: query
1008+
// description: "The name of the commit/branch/tag. Default the repository’s default branch (usually master)"
1009+
// type: string
1010+
// required: false
1011+
// - name: page
1012+
// in: query
1013+
// description: page number of results to return (1-based)
1014+
// type: integer
1015+
// - name: limit
1016+
// in: query
1017+
// description: page size of results
1018+
// type: integer
1019+
// - name: body
1020+
// in: body
1021+
// required: true
1022+
// schema:
1023+
// "$ref": "#/definitions/GetFilesOptions"
1024+
// responses:
1025+
// "200":
1026+
// "$ref": "#/responses/FilesList"
1027+
// "404":
1028+
// "$ref": "#/responses/notFound"
1029+
1030+
apiOpts := web.GetForm(ctx).(*api.GetFilesOptions)
1031+
1032+
ref := ctx.FormTrim("ref")
1033+
if ref == "" {
1034+
ref = ctx.Repo.Repository.DefaultBranch
1035+
}
1036+
1037+
files := apiOpts.Files
1038+
1039+
filesResponse := files_service.GetContentsListFromTrees(ctx, ctx.Repo.Repository, ref, files)
1040+
count := len(filesResponse)
1041+
1042+
listOpts := utils.GetListOptions(ctx)
1043+
filesResponse = util.PaginateSlice(filesResponse, listOpts.Page, listOpts.PageSize).([]*api.ContentsResponse)
1044+
1045+
ctx.SetTotalCountHeader(int64(count))
1046+
ctx.JSON(http.StatusOK, filesResponse)
1047+
}

services/repository/files/file.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,17 @@ import (
1717
"code.gitea.io/gitea/modules/util"
1818
)
1919

20-
func GetFilesResponseFromCommit(ctx context.Context, repo *repo_model.Repository, commit *git.Commit, branch string, treeNames []string) (*api.FilesResponse, error) {
20+
func GetContentsListFromTrees(ctx context.Context, repo *repo_model.Repository, branch string, treeNames []string) []*api.ContentsResponse {
2121
files := []*api.ContentsResponse{}
2222
for _, file := range treeNames {
2323
fileContents, _ := GetContents(ctx, repo, file, branch, false) // ok if fails, then will be nil
2424
files = append(files, fileContents)
2525
}
26+
return files
27+
}
28+
29+
func GetFilesResponseFromCommit(ctx context.Context, repo *repo_model.Repository, commit *git.Commit, branch string, treeNames []string) (*api.FilesResponse, error) {
30+
files := GetContentsListFromTrees(ctx, repo, branch, treeNames)
2631
fileCommitResponse, _ := GetFileCommitResponse(repo, commit) // ok if fails, then will be nil
2732
verification := GetPayloadCommitVerification(ctx, commit)
2833
filesResponse := &api.FilesResponse{

0 commit comments

Comments
 (0)