Skip to content

Commit 61e2015

Browse files
committed
avoid duplicate label queries
1 parent 3c71376 commit 61e2015

File tree

4 files changed

+32
-48
lines changed

4 files changed

+32
-48
lines changed

routers/web/org/projects.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -343,14 +343,14 @@ func ViewProject(ctx *context.Context) {
343343
return
344344
}
345345

346-
labelIDs := issue.PrepareFilterIssueLabels(ctx, project.RepoID, project.Owner)
346+
preparedLabelFilter := issue.PrepareFilterIssueLabels(ctx, project.RepoID, project.Owner)
347347
if ctx.Written() {
348348
return
349349
}
350350
assigneeID := ctx.FormString("assignee")
351351

352352
opts := issues_model.IssuesOptions{
353-
LabelIDs: labelIDs,
353+
LabelIDs: preparedLabelFilter.SelectedLabelIDs,
354354
AssigneeID: assigneeID,
355355
Owner: project.Owner,
356356
Doer: ctx.Doer,
@@ -406,8 +406,8 @@ func ViewProject(ctx *context.Context) {
406406
}
407407

408408
// Get the exclusive scope for every label ID
409-
labelExclusiveScopes := make([]string, 0, len(labelIDs))
410-
for _, labelID := range labelIDs {
409+
labelExclusiveScopes := make([]string, 0, len(preparedLabelFilter.SelectedLabelIDs))
410+
for _, labelID := range preparedLabelFilter.SelectedLabelIDs {
411411
foundExclusiveScope := false
412412
for _, label := range labels {
413413
if label.ID == labelID || label.ID == -labelID {
@@ -422,7 +422,7 @@ func ViewProject(ctx *context.Context) {
422422
}
423423

424424
for _, l := range labels {
425-
l.LoadSelectedLabelsAfterClick(labelIDs, labelExclusiveScopes)
425+
l.LoadSelectedLabelsAfterClick(preparedLabelFilter.SelectedLabelIDs, labelExclusiveScopes)
426426
}
427427
ctx.Data["Labels"] = labels
428428
ctx.Data["NumLabels"] = len(labels)

routers/web/repo/issue_list.go

Lines changed: 9 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -463,32 +463,14 @@ func UpdateIssueStatus(ctx *context.Context) {
463463
ctx.JSONOK()
464464
}
465465

466-
func renderExclusiveLabelScopes(ctx *context.Context) {
467-
labels, err := issues_model.GetLabelsByRepoID(ctx, ctx.Repo.Repository.ID, "", db.ListOptions{})
468-
if err != nil {
469-
ctx.ServerError("GetAllRepoLabels", err)
470-
return
471-
}
472-
473-
if ctx.Repo.Owner.IsOrganization() {
474-
orgLabels, err := issues_model.GetLabelsByOrgID(ctx, ctx.Repo.Owner.ID, "", db.ListOptions{})
475-
if err != nil {
476-
ctx.ServerError("GetAllRepoLabels", err)
477-
return
478-
}
479-
480-
labels = append(labels, orgLabels...)
481-
}
482-
483-
// This treats org- and repo-level label scopes equivalently.
484-
scopeSet := make(map[string]bool, 0)
485-
for _, label := range labels {
466+
func prepareIssueFilterExclusiveOrderScopes(ctx *context.Context, allLabels []*issues_model.Label) {
467+
scopeSet := make(map[string]bool)
468+
for _, label := range allLabels {
486469
scope := label.ExclusiveScope()
487470
if len(scope) > 0 && label.ExclusiveOrder > 0 {
488471
scopeSet[scope] = true
489472
}
490473
}
491-
492474
scopes := slices.Collect(maps.Keys(scopeSet))
493475
sort.Strings(scopes)
494476
ctx.Data["ExclusiveLabelScopes"] = scopes
@@ -556,16 +538,18 @@ func prepareIssueFilterAndList(ctx *context.Context, milestoneID, projectID int6
556538
mileIDs = []int64{milestoneID}
557539
}
558540

559-
labelIDs := issue.PrepareFilterIssueLabels(ctx, repo.ID, ctx.Repo.Owner)
541+
preparedLabelFilter := issue.PrepareFilterIssueLabels(ctx, repo.ID, ctx.Repo.Owner)
560542
if ctx.Written() {
561543
return
562544
}
563545

546+
prepareIssueFilterExclusiveOrderScopes(ctx, preparedLabelFilter.AllLabels)
547+
564548
var keywordMatchedIssueIDs []int64
565549
var issueStats *issues_model.IssueStats
566550
statsOpts := &issues_model.IssuesOptions{
567551
RepoIDs: []int64{repo.ID},
568-
LabelIDs: labelIDs,
552+
LabelIDs: preparedLabelFilter.SelectedLabelIDs,
569553
MilestoneIDs: mileIDs,
570554
ProjectID: projectID,
571555
AssigneeID: assigneeID,
@@ -657,7 +641,7 @@ func prepareIssueFilterAndList(ctx *context.Context, milestoneID, projectID int6
657641
ProjectID: projectID,
658642
IsClosed: isShowClosed,
659643
IsPull: isPullOption,
660-
LabelIDs: labelIDs,
644+
LabelIDs: preparedLabelFilter.SelectedLabelIDs,
661645
SortType: sortType,
662646
IssueIDs: keywordMatchedIssueIDs,
663647
}))
@@ -761,7 +745,7 @@ func prepareIssueFilterAndList(ctx *context.Context, milestoneID, projectID int6
761745
ctx.Data["IssueStats"] = issueStats
762746
ctx.Data["OpenCount"] = issueStats.OpenCount
763747
ctx.Data["ClosedCount"] = issueStats.ClosedCount
764-
ctx.Data["SelLabelIDs"] = labelIDs
748+
ctx.Data["SelLabelIDs"] = preparedLabelFilter.SelectedLabelIDs
765749
ctx.Data["ViewType"] = viewType
766750
ctx.Data["SortType"] = sortType
767751
ctx.Data["MilestoneID"] = milestoneID
@@ -812,11 +796,6 @@ func Issues(ctx *context.Context) {
812796
return
813797
}
814798

815-
renderExclusiveLabelScopes(ctx)
816-
if ctx.Written() {
817-
return
818-
}
819-
820799
ctx.Data["CanWriteIssuesOrPulls"] = ctx.Repo.CanWriteIssuesOrPulls(isPullList)
821800

822801
ctx.HTML(http.StatusOK, tplIssues)

routers/web/repo/projects.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -313,13 +313,13 @@ func ViewProject(ctx *context.Context) {
313313
return
314314
}
315315

316-
labelIDs := issue.PrepareFilterIssueLabels(ctx, ctx.Repo.Repository.ID, ctx.Repo.Owner)
316+
preparedLabelFilter := issue.PrepareFilterIssueLabels(ctx, ctx.Repo.Repository.ID, ctx.Repo.Owner)
317317

318318
assigneeID := ctx.FormString("assignee")
319319

320320
issuesMap, err := project_service.LoadIssuesFromProject(ctx, project, &issues_model.IssuesOptions{
321321
RepoIDs: []int64{ctx.Repo.Repository.ID},
322-
LabelIDs: labelIDs,
322+
LabelIDs: preparedLabelFilter.SelectedLabelIDs,
323323
AssigneeID: assigneeID,
324324
})
325325
if err != nil {
@@ -381,8 +381,8 @@ func ViewProject(ctx *context.Context) {
381381
}
382382

383383
// Get the exclusive scope for every label ID
384-
labelExclusiveScopes := make([]string, 0, len(labelIDs))
385-
for _, labelID := range labelIDs {
384+
labelExclusiveScopes := make([]string, 0, len(preparedLabelFilter.SelectedLabelIDs))
385+
for _, labelID := range preparedLabelFilter.SelectedLabelIDs {
386386
foundExclusiveScope := false
387387
for _, label := range labels {
388388
if label.ID == labelID || label.ID == -labelID {
@@ -397,7 +397,7 @@ func ViewProject(ctx *context.Context) {
397397
}
398398

399399
for _, l := range labels {
400-
l.LoadSelectedLabelsAfterClick(labelIDs, labelExclusiveScopes)
400+
l.LoadSelectedLabelsAfterClick(preparedLabelFilter.SelectedLabelIDs, labelExclusiveScopes)
401401
}
402402
ctx.Data["Labels"] = labels
403403
ctx.Data["NumLabels"] = len(labels)

routers/web/shared/issue/issue_label.go

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,18 @@ import (
1414
)
1515

1616
// PrepareFilterIssueLabels reads the "labels" query parameter, sets `ctx.Data["Labels"]` and `ctx.Data["SelectLabels"]`
17-
func PrepareFilterIssueLabels(ctx *context.Context, repoID int64, owner *user_model.User) (labelIDs []int64) {
17+
func PrepareFilterIssueLabels(ctx *context.Context, repoID int64, owner *user_model.User) (ret struct {
18+
AllLabels []*issues_model.Label
19+
SelectedLabelIDs []int64
20+
},
21+
) {
1822
// 1,-2 means including label 1 and excluding label 2
1923
// 0 means issues with no label
2024
// blank means labels will not be filtered for issues
2125
selectLabels := ctx.FormString("labels")
2226
if selectLabels != "" {
2327
var err error
24-
labelIDs, err = base.StringsToInt64s(strings.Split(selectLabels, ","))
28+
ret.SelectedLabelIDs, err = base.StringsToInt64s(strings.Split(selectLabels, ","))
2529
if err != nil {
2630
ctx.Flash.Error(ctx.Tr("invalid_data", selectLabels), true)
2731
}
@@ -32,7 +36,7 @@ func PrepareFilterIssueLabels(ctx *context.Context, repoID int64, owner *user_mo
3236
repoLabels, err := issues_model.GetLabelsByRepoID(ctx, repoID, "", db.ListOptions{})
3337
if err != nil {
3438
ctx.ServerError("GetLabelsByRepoID", err)
35-
return nil
39+
return ret
3640
}
3741
allLabels = append(allLabels, repoLabels...)
3842
}
@@ -41,14 +45,14 @@ func PrepareFilterIssueLabels(ctx *context.Context, repoID int64, owner *user_mo
4145
orgLabels, err := issues_model.GetLabelsByOrgID(ctx, owner.ID, "", db.ListOptions{})
4246
if err != nil {
4347
ctx.ServerError("GetLabelsByOrgID", err)
44-
return nil
48+
return ret
4549
}
4650
allLabels = append(allLabels, orgLabels...)
4751
}
4852

4953
// Get the exclusive scope for every label ID
50-
labelExclusiveScopes := make([]string, 0, len(labelIDs))
51-
for _, labelID := range labelIDs {
54+
labelExclusiveScopes := make([]string, 0, len(ret.SelectedLabelIDs))
55+
for _, labelID := range ret.SelectedLabelIDs {
5256
foundExclusiveScope := false
5357
for _, label := range allLabels {
5458
if label.ID == labelID || label.ID == -labelID {
@@ -63,9 +67,10 @@ func PrepareFilterIssueLabels(ctx *context.Context, repoID int64, owner *user_mo
6367
}
6468

6569
for _, l := range allLabels {
66-
l.LoadSelectedLabelsAfterClick(labelIDs, labelExclusiveScopes)
70+
l.LoadSelectedLabelsAfterClick(ret.SelectedLabelIDs, labelExclusiveScopes)
6771
}
6872
ctx.Data["Labels"] = allLabels
6973
ctx.Data["SelectLabels"] = selectLabels
70-
return labelIDs
74+
ret.AllLabels = allLabels
75+
return ret
7176
}

0 commit comments

Comments
 (0)