fix(ui): show switch default branch button in branch list only for repo admins (#10814)

The default braunch is configured in the repo settings. Only users with
administrator privileges for the repository can access the repo
settings. When the feature was implemented (72e956b79a),
the button in the branch list was only guarded with a check for repo
write permissions. This means the button is shown to too many users.
If no an user with write, but not admin permissions clicks on the button,
they see just a 404 page. Which is bad UX.

Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/10814
Reviewed-by: Michael Kriese <michael.kriese@gmx.de>
This commit is contained in:
Beowulf 2026-01-14 08:15:39 +01:00
parent eedf7fd388
commit a8a12241ce
2 changed files with 26 additions and 1 deletions

View file

@ -7,7 +7,7 @@
{{if .DefaultBranchBranch}}
<h4 class="ui top attached header">
{{ctx.Locale.Tr "repo.default_branch"}}
{{if and $.IsWriter (not $.Repository.IsArchived) (not .IsDeleted)}}
{{if and $.Permission.IsAdmin (not $.Repository.IsArchived) (not .IsDeleted)}}
<a role="button" class="right" href="{{.RepoLink}}/settings/branches" data-tooltip-content="{{ctx.Locale.Tr "repo.settings.branches.switch_default_branch"}}">
{{svg "octicon-arrow-switch"}}
</a>

View file

@ -186,6 +186,31 @@ func TestDatabaseMissingABranch(t *testing.T) {
})
}
func TestSwitchDefaultBranchButtonVisibility(t *testing.T) {
onApplicationRun(t, func(t *testing.T, u *url.URL) {
session := loginUser(t, "user5")
t.Run("Check switch default branch button", func(t *testing.T) {
t.Run("Repo admin", func(t *testing.T) {
repo4 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 4})
// Check that the button is present
resp := session.MakeRequest(t, NewRequest(t, "GET", "/"+repo4.FullName()+"/branches"), http.StatusOK)
htmlDoc := NewHTMLParser(t, resp.Body)
assert.Equal(t, 1, htmlDoc.doc.Find("a[href='/"+repo4.FullName()+"/settings/branches']").Length())
})
t.Run("None repo admin", func(t *testing.T) {
repo40 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 40})
// Check that the button is NOT present
resp := session.MakeRequest(t, NewRequest(t, "GET", "/"+repo40.FullName()+"/branches"), http.StatusOK)
htmlDoc := NewHTMLParser(t, resp.Body)
assert.Equal(t, 0, htmlDoc.doc.Find("a[href='/"+repo40.FullName()+"/settings/branches']").Length())
})
})
})
}
func TestCreateBranchButtonVisibility(t *testing.T) {
onApplicationRun(t, func(t *testing.T, u *url.URL) {
session := loginUser(t, "user1")