diff options
author | Richard Mahn <richmahn@users.noreply.github.com> | 2019-06-29 16:51:10 -0400 |
---|---|---|
committer | techknowlogick <techknowlogick@gitea.io> | 2019-06-29 16:51:10 -0400 |
commit | cd96dee9822c8b744526ba862fd8b5ec0e2c30ff (patch) | |
tree | d7bbf2f2b7adf80b17f3ab3971ae49bae7b010c4 /routers/api | |
parent | 738285a4aac5df2e60f4038aa79be3e9fe921bdb (diff) | |
download | gitea-cd96dee9822c8b744526ba862fd8b5ec0e2c30ff.tar.gz gitea-cd96dee9822c8b744526ba862fd8b5ec0e2c30ff.zip |
Fixes #7292 - API File Contents bug (#7301)
Diffstat (limited to 'routers/api')
-rw-r--r-- | routers/api/v1/api.go | 3 | ||||
-rw-r--r-- | routers/api/v1/repo/file.go | 53 | ||||
-rw-r--r-- | routers/api/v1/swagger/repo.go | 15 |
3 files changed, 55 insertions, 16 deletions
diff --git a/routers/api/v1/api.go b/routers/api/v1/api.go index 2268c1be38..8e7a74eca2 100644 --- a/routers/api/v1/api.go +++ b/routers/api/v1/api.go @@ -766,7 +766,8 @@ func RegisterRoutes(m *macaron.Macaron) { m.Get("/tags/:sha", context.RepoRef(), repo.GetTag) }, reqRepoReader(models.UnitTypeCode)) m.Group("/contents", func() { - m.Get("/*", repo.GetFileContents) + m.Get("", repo.GetContentsList) + m.Get("/*", repo.GetContents) m.Group("/*", func() { m.Post("", bind(api.CreateFileOptions{}), repo.CreateFile) m.Put("", bind(api.UpdateFileOptions{}), repo.UpdateFile) diff --git a/routers/api/v1/repo/file.go b/routers/api/v1/repo/file.go index d510756283..ae20e1e96b 100644 --- a/routers/api/v1/repo/file.go +++ b/routers/api/v1/repo/file.go @@ -366,11 +366,11 @@ func DeleteFile(ctx *context.APIContext, apiOpts api.DeleteFileOptions) { } } -// GetFileContents Get the contents of a fle in a repository -func GetFileContents(ctx *context.APIContext) { - // swagger:operation GET /repos/{owner}/{repo}/contents/{filepath} repository repoGetFileContents +// GetContents Get the metadata and contents (if a file) of an entry in a repository, or a list of entries if a dir +func GetContents(ctx *context.APIContext) { + // swagger:operation GET /repos/{owner}/{repo}/contents/{filepath} repository repoGetContents // --- - // summary: Gets the contents of a file or directory in a repository + // summary: Gets the metadata and contents (if a file) of an entry in a repository, or a list of entries if a dir // produces: // - application/json // parameters: @@ -386,20 +386,20 @@ func GetFileContents(ctx *context.APIContext) { // required: true // - name: filepath // in: path - // description: path of the file to delete + // description: path of the dir, file, symlink or submodule in the repo // type: string // required: true // - name: ref // in: query // description: "The name of the commit/branch/tag. Default the repository’s default branch (usually master)" - // required: false // type: string + // required: false // responses: // "200": - // "$ref": "#/responses/FileContentResponse" + // "$ref": "#/responses/ContentsResponse" if !CanReadFiles(ctx.Repo) { - ctx.Error(http.StatusInternalServerError, "GetFileContents", models.ErrUserDoesNotHaveAccessToRepo{ + ctx.Error(http.StatusInternalServerError, "GetContentsOrList", models.ErrUserDoesNotHaveAccessToRepo{ UserID: ctx.User.ID, RepoName: ctx.Repo.Repository.LowerName, }) @@ -409,9 +409,40 @@ func GetFileContents(ctx *context.APIContext) { treePath := ctx.Params("*") ref := ctx.QueryTrim("ref") - if fileContents, err := repofiles.GetFileContents(ctx.Repo.Repository, treePath, ref); err != nil { - ctx.Error(http.StatusInternalServerError, "GetFileContents", err) + if fileList, err := repofiles.GetContentsOrList(ctx.Repo.Repository, treePath, ref); err != nil { + ctx.Error(http.StatusInternalServerError, "GetContentsOrList", err) } else { - ctx.JSON(http.StatusOK, fileContents) + ctx.JSON(http.StatusOK, fileList) } } + +// GetContentsList Get the metadata of all the entries of the root dir +func GetContentsList(ctx *context.APIContext) { + // swagger:operation GET /repos/{owner}/{repo}/contents repository repoGetContentsList + // --- + // summary: Gets the metadata of all the entries of the root dir + // produces: + // - application/json + // parameters: + // - name: owner + // in: path + // description: owner of the repo + // type: string + // required: true + // - name: repo + // in: path + // description: name of the repo + // type: string + // required: true + // - name: ref + // in: query + // description: "The name of the commit/branch/tag. Default the repository’s default branch (usually master)" + // type: string + // required: false + // responses: + // "200": + // "$ref": "#/responses/ContentsListResponse" + + // same as GetContents(), this function is here because swagger fails if path is empty in GetContents() interface + GetContents(ctx) +} diff --git a/routers/api/v1/swagger/repo.go b/routers/api/v1/swagger/repo.go index 25354b3d66..2cab5b0ed4 100644 --- a/routers/api/v1/swagger/repo.go +++ b/routers/api/v1/swagger/repo.go @@ -197,11 +197,18 @@ type swaggerFileResponse struct { Body api.FileResponse `json:"body"` } -// FileContentResponse -// swagger:response FileContentResponse -type swaggerFileContentResponse struct { +// ContentsResponse +// swagger:response ContentsResponse +type swaggerContentsResponse struct { //in: body - Body api.FileContentResponse `json:"body"` + Body api.ContentsResponse `json:"body"` +} + +// ContentsListResponse +// swagger:response ContentsListResponse +type swaggerContentsListResponse struct { + // in:body + Body []api.ContentsResponse `json:"body"` } // FileDeleteResponse |