diff --git a/routers/api/v1/api.go b/routers/api/v1/api.go index 53e61d333..78d2b8743 100644 --- a/routers/api/v1/api.go +++ b/routers/api/v1/api.go @@ -968,6 +968,7 @@ func Routes() *web.Route { m.Group("/contents", func() { m.Get("", repo.GetContentsList) m.Get("/*", repo.GetContents) + m.Get("/readme", repo.GetReadmeContents) 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 db98955be..b14f582b7 100644 --- a/routers/api/v1/repo/file.go +++ b/routers/api/v1/repo/file.go @@ -596,3 +596,54 @@ func GetContentsList(ctx *context.APIContext) { // same as GetContents(), this function is here because swagger fails if path is empty in GetContents() interface GetContents(ctx) } + +// GetReadmeContents Get the metadata and contents (if a file) of an entry in a repository, or a list of entries if a dir +func GetReadmeContents(ctx *context.APIContext) { + // swagger:operation GET /repos/{owner}/{repo}/contents/readme repository repoGetReadmeContents + // --- + // summary: Gets the README.md's contents (if a file) of an entry in a repository *** + // 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/ContentsResponse" + // "404": + // "$ref": "#/responses/notFound" + + if !canReadFiles(ctx.Repo) { + ctx.Error(http.StatusInternalServerError, "GetContentsOrList", models.ErrUserDoesNotHaveAccessToRepo{ + UserID: ctx.User.ID, + RepoName: ctx.Repo.Repository.LowerName, + }) + return + } + + // treePath := ctx.Params("*") + ref := ctx.QueryTrim("ref") + + if fileList, err := repofiles.GetContentsOrList(ctx.Repo.Repository, "README.md", ref); err != nil { + if git.IsErrNotExist(err) { + ctx.NotFound("GetContentsOrList", err) + return + } + ctx.Error(http.StatusInternalServerError, "GetContentsOrList", err) + } else { + ctx.JSON(http.StatusOK, fileList) + } +} diff --git a/templates/swagger/v1_json.tmpl b/templates/swagger/v1_json.tmpl index f66bec2c7..1986615c7 100644 --- a/templates/swagger/v1_json.tmpl +++ b/templates/swagger/v1_json.tmpl @@ -3215,6 +3215,48 @@ } } }, + "/repos/{owner}/{repo}/contents/readme": { + "get": { + "produces": [ + "application/json" + ], + "tags": [ + "repository" + ], + "summary": "Gets the README.md's contents (if a file) of an entry in a repository ***", + "operationId": "repoGetReadmeContents", + "parameters": [ + { + "type": "string", + "description": "owner of the repo", + "name": "owner", + "in": "path", + "required": true + }, + { + "type": "string", + "description": "name of the repo", + "name": "repo", + "in": "path", + "required": true + }, + { + "type": "string", + "description": "The name of the commit/branch/tag. Default the repository’s default branch (usually master)", + "name": "ref", + "in": "query" + } + ], + "responses": { + "200": { + "$ref": "#/responses/ContentsResponse" + }, + "404": { + "$ref": "#/responses/notFound" + } + } + } + }, "/repos/{owner}/{repo}/contents/{filepath}": { "get": { "produces": [