Skip to content

Commit f490291

Browse files
lunnyzeripath
andauthored
Use subquery to instead In (#10874)
* Use subquery to instead In * Support excludedLabelNames on issues options * Fix tests Co-authored-by: zeripath <[email protected]>
1 parent 5c3be56 commit f490291

File tree

3 files changed

+45
-26
lines changed

3 files changed

+45
-26
lines changed

models/issue.go

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1045,16 +1045,18 @@ func GetIssuesByIDs(issueIDs []int64) ([]*Issue, error) {
10451045
// IssuesOptions represents options of an issue.
10461046
type IssuesOptions struct {
10471047
ListOptions
1048-
RepoIDs []int64 // include all repos if empty
1049-
AssigneeID int64
1050-
PosterID int64
1051-
MentionedID int64
1052-
MilestoneID int64
1053-
IsClosed util.OptionalBool
1054-
IsPull util.OptionalBool
1055-
LabelIDs []int64
1056-
SortType string
1057-
IssueIDs []int64
1048+
RepoIDs []int64 // include all repos if empty
1049+
AssigneeID int64
1050+
PosterID int64
1051+
MentionedID int64
1052+
MilestoneID int64
1053+
IsClosed util.OptionalBool
1054+
IsPull util.OptionalBool
1055+
LabelIDs []int64
1056+
IncludedLabelNames []string
1057+
ExcludedLabelNames []string
1058+
SortType string
1059+
IssueIDs []int64
10581060
// prioritize issues from this repo
10591061
PriorityRepoID int64
10601062
}
@@ -1153,6 +1155,14 @@ func (opts *IssuesOptions) setupSession(sess *xorm.Session) {
11531155
}
11541156
}
11551157
}
1158+
1159+
if len(opts.IncludedLabelNames) > 0 {
1160+
sess.In("issue.id", BuildLabelNamesIssueIDsCondition(opts.IncludedLabelNames))
1161+
}
1162+
1163+
if len(opts.ExcludedLabelNames) > 0 {
1164+
sess.And(builder.NotIn("issue.id", BuildLabelNamesIssueIDsCondition(opts.ExcludedLabelNames)))
1165+
}
11561166
}
11571167

11581168
// CountIssuesByRepo map from repoID to number of issues matching the options

models/issue_label.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,17 @@ func GetLabelIDsInRepoByNames(repoID int64, labelNames []string) ([]int64, error
269269
Find(&labelIDs)
270270
}
271271

272+
// BuildLabelNamesIssueIDsCondition returns a builder where get issue ids match label names
273+
func BuildLabelNamesIssueIDsCondition(labelNames []string) *builder.Builder {
274+
return builder.Select("issue_label.issue_id").
275+
From("issue_label").
276+
InnerJoin("label", "label.id = issue_label.label_id").
277+
Where(
278+
builder.In("label.name", labelNames),
279+
).
280+
GroupBy("issue_label.issue_id")
281+
}
282+
272283
// GetLabelIDsInReposByNames returns a list of labelIDs by names in one of the given
273284
// repositories.
274285
// it silently ignores label names that do not belong to the repository.

routers/api/v1/repo/issue.go

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ func SearchIssues(ctx *context.APIContext) {
8888
opts.Private = true
8989
opts.AllLimited = true
9090
}
91+
9192
issueCount := 0
9293
for page := 1; ; page++ {
9394
opts.Page = page
@@ -127,15 +128,6 @@ func SearchIssues(ctx *context.APIContext) {
127128
issueIDs, err = issue_indexer.SearchIssuesByKeyword(repoIDs, keyword)
128129
}
129130

130-
labels := ctx.Query("labels")
131-
if splitted := strings.Split(labels, ","); labels != "" && len(splitted) > 0 {
132-
labelIDs, err = models.GetLabelIDsInReposByNames(repoIDs, splitted)
133-
if err != nil {
134-
ctx.Error(http.StatusInternalServerError, "GetLabelIDsInRepoByNames", err)
135-
return
136-
}
137-
}
138-
139131
var isPull util.OptionalBool
140132
switch ctx.Query("type") {
141133
case "pulls":
@@ -146,6 +138,12 @@ func SearchIssues(ctx *context.APIContext) {
146138
isPull = util.OptionalBoolNone
147139
}
148140

141+
labels := strings.TrimSpace(ctx.Query("labels"))
142+
var includedLabelNames []string
143+
if len(labels) > 0 {
144+
includedLabelNames = strings.Split(labels, ",")
145+
}
146+
149147
// Only fetch the issues if we either don't have a keyword or the search returned issues
150148
// This would otherwise return all issues if no issues were found by the search.
151149
if len(keyword) == 0 || len(issueIDs) > 0 || len(labelIDs) > 0 {
@@ -154,13 +152,13 @@ func SearchIssues(ctx *context.APIContext) {
154152
Page: ctx.QueryInt("page"),
155153
PageSize: setting.UI.IssuePagingNum,
156154
},
157-
RepoIDs: repoIDs,
158-
IsClosed: isClosed,
159-
IssueIDs: issueIDs,
160-
LabelIDs: labelIDs,
161-
SortType: "priorityrepo",
162-
PriorityRepoID: ctx.QueryInt64("priority_repo_id"),
163-
IsPull: isPull,
155+
RepoIDs: repoIDs,
156+
IsClosed: isClosed,
157+
IssueIDs: issueIDs,
158+
IncludedLabelNames: includedLabelNames,
159+
SortType: "priorityrepo",
160+
PriorityRepoID: ctx.QueryInt64("priority_repo_id"),
161+
IsPull: isPull,
164162
})
165163
}
166164

0 commit comments

Comments
 (0)