fix(ui): Fix disabled create repository button on create repo form (#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 repo limit, but were stll able to create a repo in an org,
  the button would still be disabled.
- Resolves #7341
- Add Integration Tests to the create repo form

Signed-off-by: Ryan Lerch <rlerch@redhat.com>
This commit is contained in:
Ryan Lerch 2025-03-31 16:07:41 +10:00
parent 738f721f3a
commit 4dab438127
5 changed files with 121 additions and 43 deletions

View file

@ -93,7 +93,7 @@
login_name: org3
type: 1
salt: ZogKvWdyEx
max_repo_creation: -1
max_repo_creation: 1000
is_active: false
is_admin: false
is_restricted: false

View file

@ -22,5 +22,10 @@
"alert.range_error": " must be a number between %[1]s and %[2]s.",
"settings.adopt": "Adopt",
"install.invalid_lfs_path": "Unable to create the LFS root at the specified path: %[1]s",
"install.lfs_jwt_secret_failed": "Unable to generate a LFS JWT secret: %[1]s"
"install.lfs_jwt_secret_failed": "Unable to generate a LFS JWT secret: %[1]s",
"repo.form.reach_limit_of_creation": {
"one": "%[1]s has reached the limit of %[2]s repository per user.",
"other": "%[1]s has reached the limit of %[2]s repositories per user."
},
"repo.form.cannot_create": "You are not permitted to create a repository"
}

View file

@ -8,42 +8,48 @@
{{ctx.Locale.Tr "new_repo.title"}}
</h3>
<div class="ui attached segment">
{{template "base/alert" .}}
{{template "repo/create_helper" .}}
{{if or .CanCreateRepo .Orgs}}
{{template "base/alert" .}}
{{template "repo/create_helper" .}}
{{if not .CanCreateRepo}}
{{if and (not .CanCreateRepo) (ne .MaxCreationLimit 0)}}
<div class="ui negative message">
<p>{{ctx.Locale.TrPluralString .MaxCreationLimit "repo.form.reach_limit_of_creation" .SignedUser.Name .MaxCreationLimit}}</p>
</div>
{{end}}
<fieldset>
{{template "repo/create_basic" .}}
</fieldset>
<fieldset>
<legend>
{{ctx.Locale.Tr "repo.new_from_template"}}
<span class="help">{{ctx.Locale.Tr "repo.new_from_template_description"}}</span>
</legend>
{{template "repo/create_from_template" .}}
</fieldset>
<div id="non_template">
<fieldset>
<legend>{{ctx.Locale.Tr "repo.auto_init"}}</legend>
{{template "repo/create_init" .}}
</fieldset>
<fieldset>
<legend>{{ctx.Locale.Tr "repo.new_advanced"}}</legend>
<details><summary>{{ctx.Locale.Tr "repo.new_advanced_expand"}}</summary>
{{template "repo/create_advanced" .}}
</details>
</fieldset>
</div>
<button class="ui primary button">
{{ctx.Locale.Tr "repo.create_repo"}}
</button>
{{else}}
<div class="ui negative message">
<p>{{ctx.Locale.TrN .MaxCreationLimit "repo.form.reach_limit_of_creation_1" "repo.form.reach_limit_of_creation_n" .MaxCreationLimit}}</p>
{{ctx.Locale.Tr "repo.form.cannot_create"}}
</div>
{{end}}
<fieldset>
{{template "repo/create_basic" .}}
</fieldset>
<fieldset>
<legend>
{{ctx.Locale.Tr "repo.new_from_template"}}
<span class="help">{{ctx.Locale.Tr "repo.new_from_template_description"}}</span>
</legend>
{{template "repo/create_from_template" .}}
</fieldset>
<div id="non_template">
<fieldset>
<legend>{{ctx.Locale.Tr "repo.auto_init"}}</legend>
{{template "repo/create_init" .}}
</fieldset>
<fieldset>
<legend>{{ctx.Locale.Tr "repo.new_advanced"}}</legend>
<details><summary>{{ctx.Locale.Tr "repo.new_advanced_expand"}}</summary>
{{template "repo/create_advanced" .}}
</details>
</fieldset>
</div>
<button class="ui primary button{{if not .CanCreateRepo}} disabled{{end}}">
{{ctx.Locale.Tr "repo.create_repo"}}
</button>
</div>
</form>
</div>

View file

@ -2,17 +2,27 @@
{{ctx.Locale.Tr "repo.owner"}}
<div class="ui selection required dropdown" aria-labelledby="repo_owner_label">
{{/* uid id is used by the repo-template code */}}
<input type="hidden" id="uid" name="uid" value="{{.ContextUser.ID}}" required>
<span class="text truncated-item-container" title="{{.ContextUser.Name}}">
{{ctx.AvatarUtils.Avatar .ContextUser 28 "mini"}}
<span class="truncated-item-name">{{.ContextUser.ShortName 40}}</span>
</span>
{{if .CanCreateRepo}}
<input type="hidden" id="uid" name="uid" value="{{.ContextUser.ID}}" required>
<span class="text truncated-item-container" title="{{.ContextUser.Name}}">
{{ctx.AvatarUtils.Avatar .ContextUser 28 "mini"}}
<span class="truncated-item-name">{{.ContextUser.ShortName 40}}</span>
</span>
{{else if .Orgs}}
<input type="hidden" id="uid" name="uid" value="{{(index .Orgs 0).ID}}" required>
<span class="text truncated-item-container" title="{{(index .Orgs 0).Name}}">
{{ctx.AvatarUtils.Avatar (index .Orgs 0) 28 "mini"}}
<span class="truncated-item-name">{{(index .Orgs 0).ShortName 40}}</span>
</span>
{{end}}
{{svg "octicon-triangle-down" 14 "dropdown icon"}}
<div class="menu">
<div class="item truncated-item-container" data-value="{{.SignedUser.ID}}" title="{{.SignedUser.Name}}">
{{ctx.AvatarUtils.Avatar .SignedUser 28 "mini"}}
<span class="truncated-item-name">{{.SignedUser.ShortName 40}}</span>
</div>
{{if .CanCreateRepo}}
<div class="item truncated-item-container" data-value="{{.SignedUser.ID}}" title="{{.SignedUser.Name}}">
{{ctx.AvatarUtils.Avatar .SignedUser 28 "mini"}}
<span class="truncated-item-name">{{.SignedUser.ShortName 40}}</span>
</div>
{{end}}
{{range .Orgs}}
<div class="item truncated-item-container" data-value="{{.ID}}" title="{{.Name}}">
{{ctx.AvatarUtils.Avatar . 28 "mini"}}

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.TrPluralString(maxCreationLimit, "repo.form.reach_limit_of_creation", userName, 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"