fix(ui): make limits clearer in create repo form (#7402)

Resolves: #7341

Previously, the Create Repository button was only enabled if a user was able to create a repo in their own namespace. However, if they had reached the global repo limit, but were stlll able to create a repo in an org, the button would still be disabled.

In this pull request, the create repo form now:

1. Behaves like it always did previously if the user has not reached the repo limit.

2. If the User has reached the repo limit, and they are unable to create a repo in any of their orgs (or they have no orgs), the create repo form is displayed as:
![Screenshot](/attachments/9f22f43d-0036-4c48-b794-54302c0f241c)

3. If the User has reached the repo limit, and the **limit is greater than zero**, an alert appears at the top of the form, and they are only allowed to choose from the orgs that they are allowed to create repos in:
![Screenshot](/attachments/f5508e05-74fd-4858-9e95-967bd7017abd)

4. If the User has reached the repo limit, and the **limit is equal to zero**, no alert is displayed, as no user can create repos on that instance, and they are only allowed to choose from the orgs that they are allowed to create repos in:
![localhost_3000_repo_create (4).png](/attachments/e7a87da8-c19c-47e1-845e-2afc3667ab02)

Co-authored-by: 0ko <0ko@noreply.codeberg.org>
Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/7402
Reviewed-by: Gusted <gusted@noreply.codeberg.org>
Co-authored-by: Ryan Lerch <rlerch@redhat.com>
Co-committed-by: Ryan Lerch <rlerch@redhat.com>
This commit is contained in:
Ryan Lerch 2025-05-20 16:37:15 +02:00 committed by 0ko
parent 43fee56011
commit a0f902f635
5 changed files with 116 additions and 42 deletions

View file

@ -126,6 +126,63 @@ func TestRepoCreateForm(t *testing.T) {
assertRepoCreateForm(t, htmlDoc, user, "")
}
func TestRepoCreateFormRepoLimit(t *testing.T) {
defer tests.PrepareTestEnv(t)()
org := unittest.AssertExistsAndLoadBean(t, &user_model.User{Name: "org3"})
userName := "user2"
session := loginUser(t, userName)
locale := translation.NewLocale("en-US")
cannotCreateTr := locale.Tr("repo.form.cannot_create")
// Test the case where a user has hit the global max creation limit, but can still create
// a repo in an organization. Because the limit is greater than 0 we also show an alert
// to tell the user they have hit the limit.
t.Run("Limit above zero", func(t *testing.T) {
defer tests.PrintCurrentTest(t)()
maxCreationLimit := 1
creationLimitTr := locale.TrN(maxCreationLimit, "repo.form.reach_limit_of_creation_1", "repo.form.reach_limit_of_creation_n", maxCreationLimit)
defer test.MockVariableValue(&setting.Repository.MaxCreationLimit, maxCreationLimit)()
resp := session.MakeRequest(t, NewRequest(t, "GET", "/repo/create"), http.StatusOK)
htmlDoc := NewHTMLParser(t, resp.Body)
assertRepoCreateForm(t, htmlDoc, org, "")
alert := htmlDoc.doc.Find("div.ui.negative.message").Text()
assert.Contains(t, alert, creationLimitTr)
})
// Test the case where a user has hit the global max creation limit, but can still create
// a repo in an organization. Because the limit is 0 we DO NOT show the alert.
t.Run("Limit is zero", func(t *testing.T) {
defer tests.PrintCurrentTest(t)()
maxCreationLimit := 0
defer test.MockVariableValue(&setting.Repository.MaxCreationLimit, maxCreationLimit)()
resp := session.MakeRequest(t, NewRequest(t, "GET", "/repo/create"), http.StatusOK)
htmlDoc := NewHTMLParser(t, resp.Body)
assertRepoCreateForm(t, htmlDoc, org, "")
htmlDoc.AssertElement(t, "div.ui.negative.message", false)
})
// Test the case where a user has hit the global max creation limit, and also cannot create
// a repo in any of their orgs. The form isnt shown, and we deisplay an alert telling the user
// they can't create a repo.
t.Run("Global limit", func(t *testing.T) {
defer tests.PrintCurrentTest(t)()
maxCreationLimit := 0
defer test.MockVariableValue(&setting.Repository.MaxCreationLimit, maxCreationLimit)()
session := loginUser(t, "user8")
resp := session.MakeRequest(t, NewRequest(t, "GET", "/repo/create"), http.StatusOK)
htmlDoc := NewHTMLParser(t, resp.Body)
alert := htmlDoc.doc.Find("div.ui.negative.message").Text()
assert.Contains(t, alert, cannotCreateTr)
})
}
func TestRepoGenerate(t *testing.T) {
defer tests.PrepareTestEnv(t)()
userName := "user1"