diff --git a/services/context/api.go b/services/context/api.go index 396ceb520f..3289727425 100644 --- a/services/context/api.go +++ b/services/context/api.go @@ -353,6 +353,11 @@ 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.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 4122c78ec2..637de7d5c7 100644 --- a/tests/integration/empty_repo_test.go +++ b/tests/integration/empty_repo_test.go @@ -136,3 +136,24 @@ func TestEmptyRepoAddFileByAPI(t *testing.T) { DecodeJSON(t, resp, &apiRepo) assert.Equal(t, "new_branch", apiRepo.DefaultBranch) } + +func TestEmptyRepoAPIRequestsReturn404(t *testing.T) { + defer tests.PrepareTestEnv(t)() + + session := loginUser(t, "user30") + token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeReadRepository) + + t.Run("Raw", func(t *testing.T) { + defer tests.PrintCurrentTest(t)() + + 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) + }) +}