From 3162dc4cbfcdafcfec065fe675839f469bb5dc3c Mon Sep 17 00:00:00 2001 From: Giteabot Date: Fri, 30 Oct 2026 14:26:30 -0700 Subject: [PATCH] fix(api): answer 409 when repository files already exist on disk (#40095) (#40098) Backport #40095 by @hktitof when a repository directory exists on disk without a database row, for example after a failed delete, POST /user/repos answered 500 with an empty message the migrate endpoint already answers 409 for this error and the fork API got the same treatment in https://github.com/go-gitea/gitea/pull/39909, so the create handler now maps ErrRepoFilesAlreadyExist to 409 too. this covers POST /user/repos, POST /orgs/{org}/repos and the admin user route since they all go through CreateUserRepo the integration test creates the leftover directory and asserts 409, on master it gets 500. runs on sqlite as usual this also touches routers/api/v1/repo/repo.go, which several open PRs modify, but none of them changes this error mapping Signed-off-by: wxiaoguang Signed-off-by: hktitof Co-authored-by: Abdellatif Anaflous <62770500+hktitof@users.noreply.github.com> Co-authored-by: hktitof Co-authored-by: wxiaoguang --- models/repo/update.go | 2 +- routers/api/v1/repo/repo.go | 13 +------------ tests/integration/api_repo_test.go | 4 ++-- 3 files changed, 4 insertions(+), 15 deletions(-) diff --git a/models/repo/update.go b/models/repo/update.go index 5f5e8eb7e56..38bac3e10c1 100644 --- a/models/repo/update.go +++ b/models/repo/update.go @@ -77,7 +77,7 @@ func IsErrRepoAlreadyExist(err error) bool { } func (err ErrRepoAlreadyExist) Error() string { - return fmt.Sprintf("repository already exists [uname: %s, name: %s]", err.Uname, err.Name) + return fmt.Sprintf("repository already exists [owner: %s, repo: %s]", err.Uname, err.Name) } func (err ErrRepoAlreadyExist) Unwrap() error { diff --git a/routers/api/v1/repo/repo.go b/routers/api/v1/repo/repo.go index 73f93a9c3cc..a3ee0cf697b 100644 --- a/routers/api/v1/repo/repo.go +++ b/routers/api/v1/repo/repo.go @@ -23,7 +23,6 @@ import ( unit_model "gitea.dev/models/unit" user_model "gitea.dev/models/user" "gitea.dev/modules/git" - "gitea.dev/modules/label" "gitea.dev/modules/log" "gitea.dev/modules/markup" "gitea.dev/modules/optional" @@ -255,17 +254,7 @@ func CreateUserRepo(ctx *context.APIContext, owner *user_model.User, opt api.Cre ObjectFormatName: string(opt.ObjectFormatName), }) if err != nil { - if repo_model.IsErrRepoAlreadyExist(err) { - ctx.APIError(http.StatusConflict, "The repository with the same name already exists.") - } else if db.IsErrNameReserved(err) || - db.IsErrNamePatternNotAllowed(err) || - label.IsErrTemplateLoad(err) { - ctx.APIError(http.StatusUnprocessableEntity, err.Error()) - } else if errors.Is(err, util.ErrPermissionDenied) { - ctx.APIError(http.StatusForbidden, err.Error()) - } else { - ctx.APIErrorInternal(err) - } + ctx.APIErrorAuto(err) return } diff --git a/tests/integration/api_repo_test.go b/tests/integration/api_repo_test.go index 794f42c7838..c0488736f4c 100644 --- a/tests/integration/api_repo_test.go +++ b/tests/integration/api_repo_test.go @@ -423,7 +423,7 @@ func testAPIRepoMigrateConflict(t *testing.T, u *url.URL) { AddTokenAuth(httpContext.Token) resp := httpContext.Session.MakeRequest(t, req, http.StatusConflict) respJSON := DecodeJSON(t, resp, map[string]string{}) - assert.Equal(t, "The repository with the same name already exists.", respJSON["message"]) + assert.Contains(t, respJSON["message"], "already exists") }) } @@ -500,7 +500,7 @@ func testAPIRepoCreateConflict(t *testing.T, u *url.URL) { AddTokenAuth(httpContext.Token) resp := httpContext.Session.MakeRequest(t, req, http.StatusConflict) respJSON := DecodeJSON(t, resp, map[string]string{}) - assert.Equal(t, "The repository with the same name already exists.", respJSON["message"]) + assert.Contains(t, respJSON["message"], "already exists") }) }