Support organization setting to set default repositories list sort type#36999
Support organization setting to set default repositories list sort type#36999lunny wants to merge 2 commits into
Conversation
There was a problem hiding this comment.
Pull request overview
Adds an organization-level setting to control the default repository sort order on the organization repositories page when no explicit sort query parameter is provided.
Changes:
- Introduces an org setting (
org.repo_default_sort) persisted inuser_settingkeyed by org ID. - Reuses a shared template partial for repository sort options across repo list UIs and org settings UI.
- Extends integration coverage to verify persisted setting and default sort behavior on the org page.
Reviewed changes
Copilot reviewed 10 out of 10 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| tests/integration/org_test.go | Adds an integration assertion that org repo list respects the configured default sort. |
| templates/shared/repo/sort_options.tmpl | New shared template to render sort options as either radio items or <option>s. |
| templates/shared/repo/search.tmpl | Replaces duplicated sort-option markup with the new shared template. |
| templates/org/settings/options.tmpl | Adds a “Default repository sort” select to org settings. |
| services/forms/org.go | Extends org settings form to accept repo_default_sort. |
| routers/web/org/setting.go | Loads/saves the org default repo sort setting and validates values against OrderByFlatMap. |
| routers/web/org/repo_default_sort.go | Adds helper to read/validate the org’s stored default sort. |
| routers/web/org/home.go | Applies org default sort when no valid sort is provided. |
| options/locale/locale_en-US.json | Adds new i18n strings for the org default sort setting UI and error. |
| models/user/setting_options.go | Adds the settings key constant for org repo default sort. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| <label for="repo_default_sort">{{ctx.Locale.Tr "org.settings.repo_default_sort"}}</label> | ||
| <select id="repo_default_sort" name="repo_default_sort" class="ui dropdown"> | ||
| <option value="" {{if eq .RepoDefaultSort ""}}selected{{end}}>{{ctx.Locale.Tr "org.settings.repo_default_sort_default"}}</option> | ||
| {{template "shared/repo/sort_options" dict "CurrentSort" .RepoDefaultSort "DisableStars" false "Kind" "select"}} |
There was a problem hiding this comment.
The sort options are always rendered with star-based sort choices enabled (DisableStars hardcoded to false). Since the web context already exposes .DisableStars (from setting.Repository.DisableStars), this will incorrectly show star sort options even when stars are disabled site-wide. Pass the actual .DisableStars value so the options list matches the rest of the UI and the backend feature set.
| {{template "shared/repo/sort_options" dict "CurrentSort" .RepoDefaultSort "DisableStars" false "Kind" "select"}} | |
| {{template "shared/repo/sort_options" dict "CurrentSort" .RepoDefaultSort "DisableStars" .DisableStars "Kind" "select"}} |
| ctx.Data["CurrentVisibility"] = ctx.Org.Organization.Visibility | ||
|
|
||
| if ctx.HasError() { | ||
| if repoDefaultSort, err := getOrgRepoDefaultSort(ctx, ctx.Org.Organization); err == nil { |
There was a problem hiding this comment.
In the ctx.HasError() branch, RepoDefaultSort is reloaded from the saved org setting, which discards the value the user just submitted when some other field fails validation (or when repo_default_sort itself hits a binding error). Prefer populating ctx.Data["RepoDefaultSort"] from form.RepoDefaultSort when present, and only fall back to getOrgRepoDefaultSort when the form didn't provide a value.
| if repoDefaultSort, err := getOrgRepoDefaultSort(ctx, ctx.Org.Organization); err == nil { | |
| if form.RepoDefaultSort != nil { | |
| ctx.Data["RepoDefaultSort"] = *form.RepoDefaultSort | |
| } else if repoDefaultSort, err := getOrgRepoDefaultSort(ctx, ctx.Org.Organization); err == nil { |
| "code.gitea.io/gitea/models/organization" | ||
| repo_model "code.gitea.io/gitea/models/repo" | ||
| user_model "code.gitea.io/gitea/models/user" | ||
| "code.gitea.io/gitea/services/context" | ||
| ) | ||
|
|
||
| func getOrgRepoDefaultSort(ctx *context.Context, org *organization.Organization) (string, error) { | ||
| defaultSort, err := user_model.GetUserSetting(ctx, org.ID, user_model.SettingsKeyOrgRepoDefaultSort) | ||
| if err != nil { |
There was a problem hiding this comment.
getOrgRepoDefaultSort is called on every org home/settings page view, but it uses user_model.GetUserSetting, which bypasses the user_setting cache (user_model.GetSetting does cache). Consider switching to user_model.GetSetting and treating util.ErrNotExist as an empty value to avoid an extra DB query per request.
| "code.gitea.io/gitea/models/organization" | |
| repo_model "code.gitea.io/gitea/models/repo" | |
| user_model "code.gitea.io/gitea/models/user" | |
| "code.gitea.io/gitea/services/context" | |
| ) | |
| func getOrgRepoDefaultSort(ctx *context.Context, org *organization.Organization) (string, error) { | |
| defaultSort, err := user_model.GetUserSetting(ctx, org.ID, user_model.SettingsKeyOrgRepoDefaultSort) | |
| if err != nil { | |
| "errors" | |
| "code.gitea.io/gitea/models/organization" | |
| repo_model "code.gitea.io/gitea/models/repo" | |
| user_model "code.gitea.io/gitea/models/user" | |
| "code.gitea.io/gitea/modules/util" | |
| "code.gitea.io/gitea/services/context" | |
| ) | |
| func getOrgRepoDefaultSort(ctx *context.Context, org *organization.Organization) (string, error) { | |
| defaultSort, err := user_model.GetSetting(ctx, org.ID, user_model.SettingsKeyOrgRepoDefaultSort) | |
| if err != nil { | |
| if errors.Is(err, util.ErrNotExist) { | |
| return "", nil | |
| } |
wxiaoguang
left a comment
There was a problem hiding this comment.
AI slop with messy unnecessary if logics
Fix #8795
Generated by a coding agent with Codex 5.2