新增:获取仓库目录下readme文件

This commit is contained in:
yystopf 2022-11-25 17:04:55 +08:00
parent 564abb9bb8
commit 72f4095786
2 changed files with 58 additions and 5 deletions

View File

@ -72,6 +72,7 @@ func Routers() *web.Route {
})
m.Group("/readme", func() {
m.Get("", repo.GetReadmeContents)
m.Get("/*", repo.GetReadmeContentsByPath)
})
m.Get("/commits_slice", repo.GetAllCommitsSliceByTime)
}, repoAssignment())

View File

@ -22,10 +22,10 @@ func GetReadmeContents(ctx *context.APIContext) {
return
}
treePath := ctx.Params("*")
ref := ctx.Params(":ref")
readmePath := "README.md"
filesListInterface, err := files_service.GetContentsOrList(ctx, ctx.Repo.Repository, treePath, ref)
readmezhPath := ""
filesListInterface, err := files_service.GetContentsOrList(ctx, ctx.Repo.Repository, readmePath, ref)
if err != nil {
ctx.Error(http.StatusInternalServerError, "GetContentsOrList", err)
return
@ -35,12 +35,18 @@ func GetReadmeContents(ctx *context.APIContext) {
for _, file := range filesList {
if strings.ToLower(file.Name) == "readme.md" {
readmePath = file.Name
break
}
if strings.ToLower(file.Name) == "readme_zh.md" {
readmezhPath = file.Name
}
}
}
newTreePath := treePath + "/" + readmePath
if fileList, err := files_service.GetContentsOrList(ctx, ctx.Repo.Repository, newTreePath, ref); err != nil {
if readmezhPath != "" {
readmePath = readmezhPath
}
if fileList, err := files_service.GetContentsOrList(ctx, ctx.Repo.Repository, readmePath, ref); err != nil {
if git.IsErrNotExist(err) {
ctx.NotFound("GetContentsOrList", err)
return
@ -55,3 +61,49 @@ func GetReadmeContents(ctx *context.APIContext) {
func canReadFiles(r *context.Repository) bool {
return r.Permission.CanRead(unit_model.UnitCode.Type)
}
func GetReadmeContentsByPath(ctx *context.APIContext) {
if !canReadFiles(ctx.Repo) {
ctx.Error(http.StatusInternalServerError, "canReadFiles", repo_model.ErrUserDoesNotHaveAccessToRepo{
UserID: ctx.ContextUser.ID,
RepoName: ctx.Repo.Repository.LowerName,
})
return
}
treePath := ctx.Params("*")
ref := ctx.Params(":ref")
readmePath := "README.md"
readmezhPath := ""
filesListInterface, err := files_service.GetContentsOrList(ctx, ctx.Repo.Repository, treePath, ref)
if err != nil {
ctx.Error(http.StatusInternalServerError, "GetContentsOrList", err)
return
}
filesList, ok := filesListInterface.([]*api.ContentsResponse)
if ok {
for _, file := range filesList {
if strings.ToLower(file.Name) == "readme.md" {
readmePath = file.Name
}
if strings.ToLower(file.Name) == "readme_zh.md" {
readmezhPath = file.Name
}
}
}
if readmezhPath != "" {
readmePath = readmezhPath
}
newTreePath := treePath + "/" + readmePath
if fileList, err := files_service.GetContentsOrList(ctx, ctx.Repo.Repository, newTreePath, ref); err != nil {
if git.IsErrNotExist(err) {
ctx.NotFound("GetContentsOrList", err)
return
}
ctx.Error(http.StatusInternalServerError, "GetContentsOrList", err)
} else {
ctx.JSON(http.StatusOK, fileList)
}
}