fix(label): sort labels by open issue count, not total (#39464)

The labels page shows open issue counts, but "Most issues" and "Least
issues" sorted by the total including closed issues, so a label with no
open issues could land in the middle of the list.

- Sort repository and organization labels by their open issue count
- Sort organization labels on a repository's labels page by their open
issues in that repository, which is the count they display

Fixes: https://github.com/go-gitea/gitea/issues/39346

---------

Signed-off-by: piyush295 <mr.piyush295@gmail.com>
Co-authored-by: silverwind <me@silverwind.io>
This commit is contained in:
Piyush Kumarandsilverwind authored and GitHub committed 2026-10-04 08:48:18 +00:00
1 parent eb4468ff4e
commit a9ac8c0afd
4 files changed
+46 -16

No files matched your search

+6 -4
View File
@@ -433,12 +433,13 @@ func GetLabelsByRepoID(ctx context.Context, repoID int64, sortType string, listO
case "reversealphabetically":
sess.Desc("name")
case "leastissues":
sess.Asc("num_issues")
sess.OrderBy("num_issues - num_closed_issues ASC")
case "mostissues":
sess.Desc("num_issues")
sess.OrderBy("num_issues - num_closed_issues DESC")
default:
sess.Asc("name")
}
sess.Asc("id")
if listOptions.Page > 0 {
db.SetSessionPagination(sess, &listOptions)
@@ -508,12 +509,13 @@ func GetLabelsByOrgID(ctx context.Context, orgID int64, sortType string, listOpt
case "reversealphabetically":
sess.Desc("name")
case "leastissues":
sess.Asc("num_issues")
sess.OrderBy("num_issues - num_closed_issues ASC")
case "mostissues":
sess.Desc("num_issues")
sess.OrderBy("num_issues - num_closed_issues DESC")
default:
sess.Asc("name")
}
sess.Asc("id")
if listOptions.Page > 0 {
db.SetSessionPagination(sess, &listOptions)
+5 -1
View File
@@ -191,6 +191,8 @@ func TestGetLabelsInRepoByIDs(t *testing.T) {
func TestGetLabelsByRepoID(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
_, err := db.GetEngine(t.Context()).ID(2).Cols("num_issues", "num_closed_issues").Update(&issues_model.Label{NumIssues: 3, NumClosedIssues: 3})
assert.NoError(t, err)
testSuccess := func(repoID int64, sortType string, expectedIssueIDs []int64) {
labels, err := issues_model.GetLabelsByRepoID(t.Context(), repoID, sortType, db.ListOptions{})
assert.NoError(t, err)
@@ -258,6 +260,8 @@ func TestGetLabelsInOrgByIDs(t *testing.T) {
func TestGetLabelsByOrgID(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
_, err := db.GetEngine(t.Context()).ID(3).Cols("num_issues", "num_closed_issues").Update(&issues_model.Label{NumIssues: 3, NumClosedIssues: 3})
assert.NoError(t, err)
testSuccess := func(orgID int64, sortType string, expectedIssueIDs []int64) {
labels, err := issues_model.GetLabelsByOrgID(t.Context(), orgID, sortType, db.ListOptions{})
assert.NoError(t, err)
@@ -271,7 +275,7 @@ func TestGetLabelsByOrgID(t *testing.T) {
testSuccess(3, "reversealphabetically", []int64{4, 3})
testSuccess(3, "default", []int64{3, 4})
_, err := issues_model.GetLabelsByOrgID(t.Context(), 0, "leastissues", db.ListOptions{})
_, err = issues_model.GetLabelsByOrgID(t.Context(), 0, "leastissues", db.ListOptions{})
assert.True(t, issues_model.IsErrOrgLabelNotExist(err))
_, err = issues_model.GetLabelsByOrgID(t.Context(), -1, "leastissues", db.ListOptions{})