Skip to content

Commit 694b658

Browse files
wxiaoguanglunny
authored andcommitted
Refactor issue label selection (go-gitea#31497)
Follow go-gitea#26460
1 parent 3fe1f73 commit 694b658

File tree

3 files changed

+61
-16
lines changed

3 files changed

+61
-16
lines changed

models/issues/issue_search.go

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@ package issues
66
import (
77
"context"
88
"fmt"
9+
"strconv"
910
"strings"
1011

1112
"code.gitea.io/gitea/models/db"
1213
"code.gitea.io/gitea/models/organization"
1314
repo_model "code.gitea.io/gitea/models/repo"
1415
"code.gitea.io/gitea/models/unit"
1516
user_model "code.gitea.io/gitea/models/user"
17+
"code.gitea.io/gitea/modules/container"
1618
"code.gitea.io/gitea/modules/optional"
1719

1820
"xorm.io/builder"
@@ -118,14 +120,30 @@ func applyLabelsCondition(sess *xorm.Session, opts *IssuesOptions) *xorm.Session
118120
if opts.LabelIDs[0] == 0 {
119121
sess.Where("issue.id NOT IN (SELECT issue_id FROM issue_label)")
120122
} else {
121-
for i, labelID := range opts.LabelIDs {
123+
// deduplicate the label IDs for inclusion and exclusion
124+
includedLabelIDs := make(container.Set[int64])
125+
excludedLabelIDs := make(container.Set[int64])
126+
for _, labelID := range opts.LabelIDs {
122127
if labelID > 0 {
123-
sess.Join("INNER", fmt.Sprintf("issue_label il%d", i),
124-
fmt.Sprintf("issue.id = il%[1]d.issue_id AND il%[1]d.label_id = %[2]d", i, labelID))
128+
includedLabelIDs.Add(labelID)
125129
} else if labelID < 0 { // 0 is not supported here, so just ignore it
126-
sess.Where("issue.id not in (select issue_id from issue_label where label_id = ?)", -labelID)
130+
excludedLabelIDs.Add(-labelID)
127131
}
128132
}
133+
// ... and use them in a subquery of the form :
134+
// where (select count(*) from issue_label where issue_id=issue.id and label_id in (2, 4, 6)) = 3
135+
// This equality is guaranteed thanks to unique index (issue_id,label_id) on table issue_label.
136+
if len(includedLabelIDs) > 0 {
137+
subQuery := builder.Select("count(*)").From("issue_label").Where(builder.Expr("issue_id = issue.id")).
138+
And(builder.In("label_id", includedLabelIDs.Values()))
139+
sess.Where(builder.Eq{strconv.Itoa(len(includedLabelIDs)): subQuery})
140+
}
141+
// or (select count(*)...) = 0 for excluded labels
142+
if len(excludedLabelIDs) > 0 {
143+
subQuery := builder.Select("count(*)").From("issue_label").Where(builder.Expr("issue_id = issue.id")).
144+
And(builder.In("label_id", excludedLabelIDs.Values()))
145+
sess.Where(builder.Eq{"0": subQuery})
146+
}
129147
}
130148
}
131149

models/issues/label.go

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@ package issues
77
import (
88
"context"
99
"fmt"
10+
"slices"
1011
"strconv"
1112
"strings"
1213

1314
"code.gitea.io/gitea/models/db"
15+
"code.gitea.io/gitea/modules/container"
1416
"code.gitea.io/gitea/modules/label"
1517
"code.gitea.io/gitea/modules/optional"
1618
"code.gitea.io/gitea/modules/timeutil"
@@ -142,28 +144,32 @@ func (l *Label) CalOpenOrgIssues(ctx context.Context, repoID, labelID int64) {
142144

143145
// LoadSelectedLabelsAfterClick calculates the set of selected labels when a label is clicked
144146
func (l *Label) LoadSelectedLabelsAfterClick(currentSelectedLabels []int64, currentSelectedExclusiveScopes []string) {
145-
var labelQuerySlice []string
147+
labelQueryParams := container.Set[string]{}
146148
labelSelected := false
147-
labelID := strconv.FormatInt(l.ID, 10)
148-
labelScope := l.ExclusiveScope()
149-
for i, s := range currentSelectedLabels {
150-
if s == l.ID {
149+
exclusiveScope := l.ExclusiveScope()
150+
for i, curSel := range currentSelectedLabels {
151+
if curSel == l.ID {
151152
labelSelected = true
152-
} else if -s == l.ID {
153+
} else if -curSel == l.ID {
153154
labelSelected = true
154155
l.IsExcluded = true
155-
} else if s != 0 {
156+
} else if curSel != 0 {
156157
// Exclude other labels in the same scope from selection
157-
if s < 0 || labelScope == "" || labelScope != currentSelectedExclusiveScopes[i] {
158-
labelQuerySlice = append(labelQuerySlice, strconv.FormatInt(s, 10))
158+
if curSel < 0 || exclusiveScope == "" || exclusiveScope != currentSelectedExclusiveScopes[i] {
159+
labelQueryParams.Add(strconv.FormatInt(curSel, 10))
159160
}
160161
}
161162
}
162163
if !labelSelected {
163-
labelQuerySlice = append(labelQuerySlice, labelID)
164+
labelQueryParams.Add(strconv.FormatInt(l.ID, 10))
164165
}
165166
l.IsSelected = labelSelected
166-
l.QueryString = strings.Join(labelQuerySlice, ",")
167+
168+
// Sort and deduplicate the ids to avoid the crawlers asking for the
169+
// same thing with simply a different order of parameters
170+
labelQuerySliceStrings := labelQueryParams.Values()
171+
slices.Sort(labelQuerySliceStrings) // the sort is still needed because the underlying map of Set doesn't guarantee order
172+
l.QueryString = strings.Join(labelQuerySliceStrings, ",")
167173
}
168174

169175
// BelongsToOrg returns true if label is an organization label
@@ -176,7 +182,7 @@ func (l *Label) BelongsToRepo() bool {
176182
return l.RepoID > 0
177183
}
178184

179-
// Return scope substring of label name, or empty string if none exists
185+
// ExclusiveScope returns scope substring of label name, or empty string if none exists
180186
func (l *Label) ExclusiveScope() string {
181187
if !l.Exclusive {
182188
return ""

models/issues/label_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,27 @@ func TestLabel_CalOpenIssues(t *testing.T) {
2323
assert.EqualValues(t, 2, label.NumOpenIssues)
2424
}
2525

26+
func TestLabel_LoadSelectedLabelsAfterClick(t *testing.T) {
27+
assert.NoError(t, unittest.PrepareTestDatabase())
28+
// Loading the label id:8 which have a scope and an exclusivity
29+
label := unittest.AssertExistsAndLoadBean(t, &issues_model.Label{ID: 8})
30+
31+
// First test : with negative and scope
32+
label.LoadSelectedLabelsAfterClick([]int64{1, -8}, []string{"", "scope"})
33+
assert.Equal(t, "1", label.QueryString)
34+
assert.Equal(t, true, label.IsSelected)
35+
36+
// Second test : with duplicates
37+
label.LoadSelectedLabelsAfterClick([]int64{1, 7, 1, 7, 7}, []string{"", "scope", "", "scope", "scope"})
38+
assert.Equal(t, "1,8", label.QueryString)
39+
assert.Equal(t, false, label.IsSelected)
40+
41+
// Third test : empty set
42+
label.LoadSelectedLabelsAfterClick([]int64{}, []string{})
43+
assert.False(t, label.IsSelected)
44+
assert.Equal(t, "8", label.QueryString)
45+
}
46+
2647
func TestLabel_ExclusiveScope(t *testing.T) {
2748
assert.NoError(t, unittest.PrepareTestDatabase())
2849
label := unittest.AssertExistsAndLoadBean(t, &issues_model.Label{ID: 7})

0 commit comments

Comments
 (0)