From 668e9bb2ac41d4d207fd8035246f51e83ae4f815 Mon Sep 17 00:00:00 2001 From: patka Date: Sun, 21 Sep 2025 08:47:10 +0200 Subject: [PATCH] fix: check target repo limit instead of user repo limit (#9360) Fixes https://codeberg.org/forgejo/forgejo/issues/9344 ## Checklist The [contributor guide](https://forgejo.org/docs/next/contributor/) contains information that will be helpful to first time contributors. There also are a few [conditions for merging Pull Requests in Forgejo repositories](https://codeberg.org/forgejo/governance/src/branch/main/PullRequestsAgreement.md). You are also welcome to join the [Forgejo development chatroom](https://matrix.to/#/#forgejo-development:matrix.org). ### Tests - I added test coverage for Go changes... - [x] in their respective `*_test.go` for unit tests. - [ ] in the `tests/integration` directory if it involves interactions with a live Forgejo server. - I added test coverage for JavaScript changes... - [ ] in `web_src/js/*.test.js` if it can be unit tested. - [ ] in `tests/e2e/*.test.e2e.js` if it requires interactions with a live Forgejo server (see also the [developer guide for JavaScript testing](https://codeberg.org/forgejo/forgejo/src/branch/forgejo/tests/e2e/README.md#end-to-end-tests)). ### Documentation - [ ] I created a pull request [to the documentation](https://codeberg.org/forgejo/docs) to explain to Forgejo users how to use this change. - [x] I did not document these changes and I do not expect someone else to do it. ### Release notes - [x] I do not want this change to show in the release notes. - [ ] I want the title to show in the release notes with a link to this pull request. - [ ] I want the content of the `release-notes/.md` to be be used for the release notes instead of the title. Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/9360 Reviewed-by: Mathieu Fenniak Reviewed-by: Earl Warren Co-authored-by: patka Co-committed-by: patka --- models/repo/update.go | 4 +-- models/repo/update_test.go | 74 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+), 2 deletions(-) create mode 100644 models/repo/update_test.go diff --git a/models/repo/update.go b/models/repo/update.go index 0222d09de5..658b01501d 100644 --- a/models/repo/update.go +++ b/models/repo/update.go @@ -105,9 +105,9 @@ func (err ErrRepoFilesAlreadyExist) Unwrap() error { return util.ErrAlreadyExist } -// CheckCreateRepository check if could created a repository +// CheckCreateRepository check if a repository can be created func CheckCreateRepository(ctx context.Context, doer, u *user_model.User, name string, overwriteOrAdopt bool) error { - if !doer.CanCreateRepo() { + if !doer.IsAdmin && !u.CanCreateRepo() { return ErrReachLimitOfRepo{u.MaxRepoCreation} } diff --git a/models/repo/update_test.go b/models/repo/update_test.go new file mode 100644 index 0000000000..88a0c675f6 --- /dev/null +++ b/models/repo/update_test.go @@ -0,0 +1,74 @@ +// Copyright 2025 The Forgejo Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +package repo_test + +import ( + "testing" + + "forgejo.org/models/db" + repo_model "forgejo.org/models/repo" + "forgejo.org/models/unittest" + user_model "forgejo.org/models/user" + "forgejo.org/modules/util" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestCheckCreateRepository(t *testing.T) { + require.NoError(t, unittest.PrepareTestDatabase()) + + t.Run("Success", func(t *testing.T) { + user := &user_model.User{MaxRepoCreation: 1} + + err := repo_model.CheckCreateRepository(db.DefaultContext, user, user, "testName", true) + + require.NoError(t, err) + }) + + t.Run("AdminIgnoresRepoLimit", func(t *testing.T) { + user := &user_model.User{MaxRepoCreation: 0, IsAdmin: true} + + err := repo_model.CheckCreateRepository(db.DefaultContext, user, user, "testName", true) + + require.NoError(t, err) + }) + + t.Run("RepoLimitReached", func(t *testing.T) { + user := &user_model.User{MaxRepoCreation: 0} + + err := repo_model.CheckCreateRepository(db.DefaultContext, user, user, "testName", true) + + require.ErrorIs(t, err, repo_model.ErrReachLimitOfRepo{}) + }) + + t.Run("UnusableRepoName", func(t *testing.T) { + user := &user_model.User{MaxRepoCreation: 1} + + err := repo_model.CheckCreateRepository(db.DefaultContext, user, user, "testName/", true) + + require.ErrorIs(t, err, db.ErrNameCharsNotAllowed{Name: "testName/"}) + }) + + t.Run("RepoAlreadyExists", func(t *testing.T) { + unittest.AssertExistsIf(t, true, &repo_model.Repository{Name: "repo1"}) + user := &user_model.User{MaxRepoCreation: 2} + + err := repo_model.CheckCreateRepository(db.DefaultContext, user, user, "repo1", true) + + require.ErrorIs(t, err, repo_model.ErrRepoAlreadyExist{Name: "repo1"}) + }) + + t.Run("RepoDirAlreadyExists", func(t *testing.T) { + db.DeleteByBean(db.DefaultContext, &repo_model.Repository{Name: "repo1"}) + user := &user_model.User{MaxRepoCreation: 2, Name: "user2"} + + exists, _ := util.IsExist(repo_model.RepoPath("user2", "repo1")) + assert.True(t, exists) + + err := repo_model.CheckCreateRepository(db.DefaultContext, user, user, "repo1", false) + + require.ErrorIs(t, err, repo_model.ErrRepoAlreadyExist{Name: "repo1", Uname: "user2"}) + }) +}