Skip to content

Commit 583968f

Browse files
bogdanpetreatechknowlogick
authored andcommitted
Return 409 when creating repo if it already exists. (#6330)
1 parent 7780ea8 commit 583968f

File tree

2 files changed

+74
-2
lines changed

2 files changed

+74
-2
lines changed

integrations/api_repo_test.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ package integrations
66

77
import (
88
"fmt"
9+
"io/ioutil"
910
"net/http"
11+
"net/url"
12+
"os"
1013
"testing"
1114

1215
"code.gitea.io/gitea/models"
@@ -291,6 +294,44 @@ func TestAPIRepoMigrate(t *testing.T) {
291294
}
292295
}
293296

297+
func TestAPIRepoMigrateConflict(t *testing.T) {
298+
onGiteaRun(t, testAPIRepoMigrateConflict)
299+
}
300+
301+
func testAPIRepoMigrateConflict(t *testing.T, u *url.URL) {
302+
username := "user2"
303+
baseAPITestContext := NewAPITestContext(t, username, "repo1")
304+
305+
u.Path = baseAPITestContext.GitPath()
306+
307+
t.Run("Existing", func(t *testing.T) {
308+
httpContext := baseAPITestContext
309+
310+
httpContext.Reponame = "repo-tmp-17"
311+
dstPath, err := ioutil.TempDir("", httpContext.Reponame)
312+
assert.NoError(t, err)
313+
defer os.RemoveAll(dstPath)
314+
t.Run("CreateRepo", doAPICreateRepository(httpContext, false))
315+
316+
user, err := models.GetUserByName(httpContext.Username)
317+
assert.NoError(t, err)
318+
userID := user.ID
319+
320+
cloneURL := "https://github.com/go-gitea/git.git"
321+
322+
req := NewRequestWithJSON(t, "POST", "/api/v1/repos/migrate?token="+httpContext.Token,
323+
&api.MigrateRepoOption{
324+
CloneAddr: cloneURL,
325+
UID: int(userID),
326+
RepoName: httpContext.Reponame,
327+
})
328+
resp := httpContext.Session.MakeRequest(t, req, http.StatusConflict)
329+
respJSON := map[string]string{}
330+
DecodeJSON(t, resp, &respJSON)
331+
assert.Equal(t, respJSON["message"], "The repository with the same name already exists.")
332+
})
333+
}
334+
294335
func TestAPIOrgRepoCreate(t *testing.T) {
295336
testCases := []struct {
296337
ctxUserID int64
@@ -313,3 +354,33 @@ func TestAPIOrgRepoCreate(t *testing.T) {
313354
session.MakeRequest(t, req, testCase.expectedStatus)
314355
}
315356
}
357+
358+
func TestAPIRepoCreateConflict(t *testing.T) {
359+
onGiteaRun(t, testAPIRepoCreateConflict)
360+
}
361+
362+
func testAPIRepoCreateConflict(t *testing.T, u *url.URL) {
363+
username := "user2"
364+
baseAPITestContext := NewAPITestContext(t, username, "repo1")
365+
366+
u.Path = baseAPITestContext.GitPath()
367+
368+
t.Run("Existing", func(t *testing.T) {
369+
httpContext := baseAPITestContext
370+
371+
httpContext.Reponame = "repo-tmp-17"
372+
dstPath, err := ioutil.TempDir("", httpContext.Reponame)
373+
assert.NoError(t, err)
374+
defer os.RemoveAll(dstPath)
375+
t.Run("CreateRepo", doAPICreateRepository(httpContext, false))
376+
377+
req := NewRequestWithJSON(t, "POST", "/api/v1/user/repos?token="+httpContext.Token,
378+
&api.CreateRepoOption{
379+
Name: httpContext.Reponame,
380+
})
381+
resp := httpContext.Session.MakeRequest(t, req, http.StatusConflict)
382+
respJSON := map[string]string{}
383+
DecodeJSON(t, resp, &respJSON)
384+
assert.Equal(t, respJSON["message"], "The repository with the same name already exists.")
385+
})
386+
}

routers/api/v1/repo/repo.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,8 +226,9 @@ func CreateUserRepo(ctx *context.APIContext, owner *models.User, opt api.CreateR
226226
AutoInit: opt.AutoInit,
227227
})
228228
if err != nil {
229-
if models.IsErrRepoAlreadyExist(err) ||
230-
models.IsErrNameReserved(err) ||
229+
if models.IsErrRepoAlreadyExist(err) {
230+
ctx.Error(409, "", "The repository with the same name already exists.")
231+
} else if models.IsErrNameReserved(err) ||
231232
models.IsErrNamePatternNotAllowed(err) {
232233
ctx.Error(422, "", err)
233234
} else {

0 commit comments

Comments
 (0)