diff --git a/services/context/api.go b/services/context/api.go index 5592637caf..871a2f012d 100644 --- a/services/context/api.go +++ b/services/context/api.go @@ -364,8 +364,13 @@ func RepoRefForAPI(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { ctx := GetAPIContext(req) + if ctx.Repo.Repository.IsEmpty { + ctx.NotFound(fmt.Errorf("repository is empty")) + return + } + if ctx.Repo.GitRepo == nil { - ctx.NotFound(fmt.Errorf("no open git repo")) + ctx.InternalServerError(fmt.Errorf("no open git repo")) return } diff --git a/tests/integration/empty_repo_test.go b/tests/integration/empty_repo_test.go index 73cf35a6c3..637de7d5c7 100644 --- a/tests/integration/empty_repo_test.go +++ b/tests/integration/empty_repo_test.go @@ -137,20 +137,23 @@ func TestEmptyRepoAddFileByAPI(t *testing.T) { assert.Equal(t, "new_branch", apiRepo.DefaultBranch) } -func TestEmptyRepoRawAPIRequestsReturn404(t *testing.T) { +func TestEmptyRepoAPIRequestsReturn404(t *testing.T) { defer tests.PrepareTestEnv(t)() session := loginUser(t, "user30") token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeReadRepository) - req := NewRequest(t, "GET", "/api/v1/repos/user30/empty/raw/main/something").AddTokenAuth(token) - _ = session.MakeRequest(t, req, http.StatusNotFound) -} -func TestEmptyRepoMediaAPIRequestsReturn404(t *testing.T) { - defer tests.PrepareTestEnv(t)() + t.Run("Raw", func(t *testing.T) { + defer tests.PrintCurrentTest(t)() - session := loginUser(t, "user30") - token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeReadRepository) - req := NewRequest(t, "GET", "/api/v1/repos/user30/empty/media/main/something").AddTokenAuth(token) - _ = session.MakeRequest(t, req, http.StatusNotFound) + req := NewRequest(t, "GET", "/api/v1/repos/user30/empty/raw/main/something").AddTokenAuth(token) + _ = session.MakeRequest(t, req, http.StatusNotFound) + }) + + t.Run("Media", func(t *testing.T) { + defer tests.PrintCurrentTest(t)() + + req := NewRequest(t, "GET", "/api/v1/repos/user30/empty/media/main/something").AddTokenAuth(token) + _ = session.MakeRequest(t, req, http.StatusNotFound) + }) }