Skip to content

Support organization setting to set default repositories list sort type#36999

Open
lunny wants to merge 2 commits into
go-gitea:mainfrom
lunny:lunny/org_default_repo_sort
Open

Support organization setting to set default repositories list sort type#36999
lunny wants to merge 2 commits into
go-gitea:mainfrom
lunny:lunny/org_default_repo_sort

Conversation

@lunny
Copy link
Copy Markdown
Member

@lunny lunny commented Mar 26, 2026

Fix #8795

  • add org-level default repo sort stored in user_setting
  • reuse shared repo sort options template for org settings and repo lists
  • update org settings flow and integration test to cover default sort behavior

Generated by a coding agent with Codex 5.2

@lunny lunny added the type/enhancement An improvement of existing functionality label Mar 26, 2026
@GiteaBot GiteaBot added the lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. label Mar 26, 2026
@lunny lunny marked this pull request as ready for review March 26, 2026 19:25
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 in user_setting keyed 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"}}
Copy link

Copilot AI Mar 26, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
{{template "shared/repo/sort_options" dict "CurrentSort" .RepoDefaultSort "DisableStars" false "Kind" "select"}}
{{template "shared/repo/sort_options" dict "CurrentSort" .RepoDefaultSort "DisableStars" .DisableStars "Kind" "select"}}

Copilot uses AI. Check for mistakes.
ctx.Data["CurrentVisibility"] = ctx.Org.Organization.Visibility

if ctx.HasError() {
if repoDefaultSort, err := getOrgRepoDefaultSort(ctx, ctx.Org.Organization); err == nil {
Copy link

Copilot AI Mar 26, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
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 {

Copilot uses AI. Check for mistakes.
Comment on lines +7 to +15
"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 {
Copy link

Copilot AI Mar 26, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
"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
}

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Contributor

@wxiaoguang wxiaoguang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI slop with messy unnecessary if logics

@GiteaBot GiteaBot added lgtm/blocked A maintainer has reservations with the PR and thus it cannot be merged and removed lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. labels Mar 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

lgtm/blocked A maintainer has reservations with the PR and thus it cannot be merged type/enhancement An improvement of existing functionality

Projects

None yet

Development

Successfully merging this pull request may close these issues.

default organization repository sort order

4 participants