From 7d2700c8be5da8f2073a576dae209ae07ac6ed22 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Sat, 14 Nov 2020 17:13:55 +0100 Subject: [API] Only Return Json (#13511) * Let Branch and Raw Endpoint return json error if not found * Revert "RM RepoRefByTypeForAPI and move needed parts into GetRawFile directly" This reverts commit d826d08577b23765cb3c257e7a861191d1aa9a04. * more similar to RepoRefByType * dedub-code * API should just speak JSON * nice name Co-authored-by: zeripath --- modules/context/api.go | 58 +++++++++++++++++++++++++++++++++++++++++++++++++ modules/context/repo.go | 3 +-- 2 files changed, 59 insertions(+), 2 deletions(-) (limited to 'modules') diff --git a/modules/context/api.go b/modules/context/api.go index 9dad588c7f..b5f521f63c 100644 --- a/modules/context/api.go +++ b/modules/context/api.go @@ -259,3 +259,61 @@ func (ctx *APIContext) NotFound(objs ...interface{}) { "errors": errors, }) } + +// RepoRefForAPI handles repository reference names when the ref name is not explicitly given +func RepoRefForAPI() macaron.Handler { + return func(ctx *APIContext) { + // Empty repository does not have reference information. + if ctx.Repo.Repository.IsEmpty { + return + } + + var err error + + if ctx.Repo.GitRepo == nil { + repoPath := models.RepoPath(ctx.Repo.Owner.Name, ctx.Repo.Repository.Name) + ctx.Repo.GitRepo, err = git.OpenRepository(repoPath) + if err != nil { + ctx.InternalServerError(err) + return + } + // We opened it, we should close it + defer func() { + // If it's been set to nil then assume someone else has closed it. + if ctx.Repo.GitRepo != nil { + ctx.Repo.GitRepo.Close() + } + }() + } + + refName := getRefName(ctx.Context, RepoRefAny) + + if ctx.Repo.GitRepo.IsBranchExist(refName) { + ctx.Repo.Commit, err = ctx.Repo.GitRepo.GetBranchCommit(refName) + if err != nil { + ctx.InternalServerError(err) + return + } + ctx.Repo.CommitID = ctx.Repo.Commit.ID.String() + } else if ctx.Repo.GitRepo.IsTagExist(refName) { + ctx.Repo.Commit, err = ctx.Repo.GitRepo.GetTagCommit(refName) + if err != nil { + ctx.InternalServerError(err) + return + } + ctx.Repo.CommitID = ctx.Repo.Commit.ID.String() + } else if len(refName) == 40 { + ctx.Repo.CommitID = refName + ctx.Repo.Commit, err = ctx.Repo.GitRepo.GetCommit(refName) + if err != nil { + ctx.NotFound("GetCommit", err) + return + } + } else { + ctx.NotFound(fmt.Errorf("not exist: '%s'", ctx.Params("*"))) + return + } + + ctx.Next() + } +} diff --git a/modules/context/repo.go b/modules/context/repo.go index 0ef644b522..8a2b99c854 100644 --- a/modules/context/repo.go +++ b/modules/context/repo.go @@ -716,7 +716,6 @@ func RepoRefByType(refType RepoRefType) macaron.Handler { err error ) - // For API calls. if ctx.Repo.GitRepo == nil { repoPath := models.RepoPath(ctx.Repo.Owner.Name, ctx.Repo.Repository.Name) ctx.Repo.GitRepo, err = git.OpenRepository(repoPath) @@ -785,7 +784,7 @@ func RepoRefByType(refType RepoRefType) macaron.Handler { ctx.Repo.Commit, err = ctx.Repo.GitRepo.GetCommit(refName) if err != nil { - ctx.NotFound("GetCommit", nil) + ctx.NotFound("GetCommit", err) return } } else { -- cgit v1.2.3