Skip to content

Commit 0201d40

Browse files
committed
Revert "Reapply "Include public repos in doer's dashboard for issue search (go-gitea#28304)""
This reverts commit 0b4c4e0.
1 parent 0b4c4e0 commit 0201d40

File tree

7 files changed

+221
-46
lines changed

7 files changed

+221
-46
lines changed

models/issues/issue_search.go

-7
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import (
2323
type IssuesOptions struct { //nolint
2424
db.Paginator
2525
RepoIDs []int64 // overwrites RepoCond if the length is not 0
26-
AllPublic bool // include also all public repositories
2726
RepoCond builder.Cond
2827
AssigneeID int64
2928
PosterID int64
@@ -198,12 +197,6 @@ func applyRepoConditions(sess *xorm.Session, opts *IssuesOptions) *xorm.Session
198197
} else if len(opts.RepoIDs) > 1 {
199198
opts.RepoCond = builder.In("issue.repo_id", opts.RepoIDs)
200199
}
201-
if opts.AllPublic {
202-
if opts.RepoCond == nil {
203-
opts.RepoCond = builder.NewCond()
204-
}
205-
opts.RepoCond = opts.RepoCond.Or(builder.In("issue.repo_id", builder.Select("id").From("repository").Where(builder.Eq{"is_private": false})))
206-
}
207200
if opts.RepoCond != nil {
208201
sess.And(opts.RepoCond)
209202
}

modules/indexer/issues/db/options.go

-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ func ToDBOptions(ctx context.Context, options *internal.SearchOptions) (*issue_m
5555
opts := &issue_model.IssuesOptions{
5656
Paginator: options.Paginator,
5757
RepoIDs: options.RepoIDs,
58-
AllPublic: options.AllPublic,
5958
RepoCond: nil,
6059
AssigneeID: convertID(options.AssigneeID),
6160
PosterID: convertID(options.PosterID),

modules/indexer/issues/dboptions.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ func ToSearchOptions(keyword string, opts *issues_model.IssuesOptions) *SearchOp
1212
searchOpt := &SearchOptions{
1313
Keyword: keyword,
1414
RepoIDs: opts.RepoIDs,
15-
AllPublic: opts.AllPublic,
15+
AllPublic: false,
1616
IsPull: opts.IsPull,
1717
IsClosed: opts.IsClosed,
1818
}

modules/indexer/issues/indexer.go

+28
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313

1414
db_model "code.gitea.io/gitea/models/db"
1515
repo_model "code.gitea.io/gitea/models/repo"
16+
"code.gitea.io/gitea/modules/container"
1617
"code.gitea.io/gitea/modules/graceful"
1718
"code.gitea.io/gitea/modules/indexer/issues/bleve"
1819
"code.gitea.io/gitea/modules/indexer/issues/db"
@@ -313,3 +314,30 @@ func CountIssues(ctx context.Context, opts *SearchOptions) (int64, error) {
313314
_, total, err := SearchIssues(ctx, opts)
314315
return total, err
315316
}
317+
318+
// CountIssuesByRepo counts issues by options and group by repo id.
319+
// It's not a complete implementation, since it requires the caller should provide the repo ids.
320+
// That means opts.RepoIDs must be specified, and opts.AllPublic must be false.
321+
// It's good enough for the current usage, and it can be improved if needed.
322+
// TODO: use "group by" of the indexer engines to implement it.
323+
func CountIssuesByRepo(ctx context.Context, opts *SearchOptions) (map[int64]int64, error) {
324+
if len(opts.RepoIDs) == 0 {
325+
return nil, fmt.Errorf("opts.RepoIDs must be specified")
326+
}
327+
if opts.AllPublic {
328+
return nil, fmt.Errorf("opts.AllPublic must be false")
329+
}
330+
331+
repoIDs := container.SetOf(opts.RepoIDs...).Values()
332+
ret := make(map[int64]int64, len(repoIDs))
333+
// TODO: it could be faster if do it in parallel for some indexer engines. Improve it if users report it's slow.
334+
for _, repoID := range repoIDs {
335+
count, err := CountIssues(ctx, opts.Copy(func(o *internal.SearchOptions) { o.RepoIDs = []int64{repoID} }))
336+
if err != nil {
337+
return nil, err
338+
}
339+
ret[repoID] = count
340+
}
341+
342+
return ret, nil
343+
}

routers/web/user/home.go

+138-21
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"code.gitea.io/gitea/modules/container"
2727
"code.gitea.io/gitea/modules/context"
2828
issue_indexer "code.gitea.io/gitea/modules/indexer/issues"
29+
"code.gitea.io/gitea/modules/json"
2930
"code.gitea.io/gitea/modules/log"
3031
"code.gitea.io/gitea/modules/markup"
3132
"code.gitea.io/gitea/modules/markup/markdown"
@@ -349,6 +350,7 @@ func Pulls(ctx *context.Context) {
349350

350351
ctx.Data["Title"] = ctx.Tr("pull_requests")
351352
ctx.Data["PageIsPulls"] = true
353+
ctx.Data["SingleRepoAction"] = "pull"
352354
buildIssueOverview(ctx, unit.TypePullRequests)
353355
}
354356

@@ -362,6 +364,7 @@ func Issues(ctx *context.Context) {
362364

363365
ctx.Data["Title"] = ctx.Tr("issues")
364366
ctx.Data["PageIsIssues"] = true
367+
ctx.Data["SingleRepoAction"] = "issue"
365368
buildIssueOverview(ctx, unit.TypeIssues)
366369
}
367370

@@ -487,13 +490,6 @@ func buildIssueOverview(ctx *context.Context, unitType unit.Type) {
487490
opts.RepoIDs = []int64{0}
488491
}
489492
}
490-
if ctx.Doer.ID == ctxUser.ID && filterMode != issues_model.FilterModeYourRepositories {
491-
// If the doer is the same as the context user, which means the doer is viewing his own dashboard,
492-
// it's not enough to show the repos that the doer owns or has been explicitly granted access to,
493-
// because the doer may create issues or be mentioned in any public repo.
494-
// So we need search issues in all public repos.
495-
opts.AllPublic = true
496-
}
497493

498494
switch filterMode {
499495
case issues_model.FilterModeAll:
@@ -518,6 +514,14 @@ func buildIssueOverview(ctx *context.Context, unitType unit.Type) {
518514
isShowClosed := ctx.FormString("state") == "closed"
519515
opts.IsClosed = util.OptionalBoolOf(isShowClosed)
520516

517+
// Filter repos and count issues in them. Count will be used later.
518+
// USING NON-FINAL STATE OF opts FOR A QUERY.
519+
issueCountByRepo, err := issue_indexer.CountIssuesByRepo(ctx, issue_indexer.ToSearchOptions(keyword, opts))
520+
if err != nil {
521+
ctx.ServerError("CountIssuesByRepo", err)
522+
return
523+
}
524+
521525
// Make sure page number is at least 1. Will be posted to ctx.Data.
522526
page := ctx.FormInt("page")
523527
if page <= 1 {
@@ -542,6 +546,17 @@ func buildIssueOverview(ctx *context.Context, unitType unit.Type) {
542546
}
543547
opts.LabelIDs = labelIDs
544548

549+
// Parse ctx.FormString("repos") and remember matched repo IDs for later.
550+
// Gets set when clicking filters on the issues overview page.
551+
selectedRepoIDs := getRepoIDs(ctx.FormString("repos"))
552+
// Remove repo IDs that are not accessible to the user.
553+
selectedRepoIDs = slices.DeleteFunc(selectedRepoIDs, func(v int64) bool {
554+
return !accessibleRepos.Contains(v)
555+
})
556+
if len(selectedRepoIDs) > 0 {
557+
opts.RepoIDs = selectedRepoIDs
558+
}
559+
545560
// ------------------------------
546561
// Get issues as defined by opts.
547562
// ------------------------------
@@ -562,6 +577,41 @@ func buildIssueOverview(ctx *context.Context, unitType unit.Type) {
562577
}
563578
}
564579

580+
// ----------------------------------
581+
// Add repository pointers to Issues.
582+
// ----------------------------------
583+
584+
// Remove repositories that should not be shown,
585+
// which are repositories that have no issues and are not selected by the user.
586+
selectedRepos := container.SetOf(selectedRepoIDs...)
587+
for k, v := range issueCountByRepo {
588+
if v == 0 && !selectedRepos.Contains(k) {
589+
delete(issueCountByRepo, k)
590+
}
591+
}
592+
593+
// showReposMap maps repository IDs to their Repository pointers.
594+
showReposMap, err := loadRepoByIDs(ctx, ctxUser, issueCountByRepo, unitType)
595+
if err != nil {
596+
if repo_model.IsErrRepoNotExist(err) {
597+
ctx.NotFound("GetRepositoryByID", err)
598+
return
599+
}
600+
ctx.ServerError("loadRepoByIDs", err)
601+
return
602+
}
603+
604+
// a RepositoryList
605+
showRepos := repo_model.RepositoryListOfMap(showReposMap)
606+
sort.Sort(showRepos)
607+
608+
// maps pull request IDs to their CommitStatus. Will be posted to ctx.Data.
609+
for _, issue := range issues {
610+
if issue.Repo == nil {
611+
issue.Repo = showReposMap[issue.RepoID]
612+
}
613+
}
614+
565615
commitStatuses, lastStatus, err := pull_service.GetIssuesAllCommitStatus(ctx, issues)
566616
if err != nil {
567617
ctx.ServerError("GetIssuesLastCommitStatus", err)
@@ -571,7 +621,7 @@ func buildIssueOverview(ctx *context.Context, unitType unit.Type) {
571621
// -------------------------------
572622
// Fill stats to post to ctx.Data.
573623
// -------------------------------
574-
issueStats, err := getUserIssueStats(ctx, ctxUser, filterMode, issue_indexer.ToSearchOptions(keyword, opts))
624+
issueStats, err := getUserIssueStats(ctx, filterMode, issue_indexer.ToSearchOptions(keyword, opts), ctx.Doer.ID)
575625
if err != nil {
576626
ctx.ServerError("getUserIssueStats", err)
577627
return
@@ -584,6 +634,25 @@ func buildIssueOverview(ctx *context.Context, unitType unit.Type) {
584634
} else {
585635
shownIssues = int(issueStats.ClosedCount)
586636
}
637+
if len(opts.RepoIDs) != 0 {
638+
shownIssues = 0
639+
for _, repoID := range opts.RepoIDs {
640+
shownIssues += int(issueCountByRepo[repoID])
641+
}
642+
}
643+
644+
var allIssueCount int64
645+
for _, issueCount := range issueCountByRepo {
646+
allIssueCount += issueCount
647+
}
648+
ctx.Data["TotalIssueCount"] = allIssueCount
649+
650+
if len(opts.RepoIDs) == 1 {
651+
repo := showReposMap[opts.RepoIDs[0]]
652+
if repo != nil {
653+
ctx.Data["SingleRepoLink"] = repo.Link()
654+
}
655+
}
587656

588657
ctx.Data["IsShowClosed"] = isShowClosed
589658

@@ -620,9 +689,12 @@ func buildIssueOverview(ctx *context.Context, unitType unit.Type) {
620689
}
621690
ctx.Data["CommitLastStatus"] = lastStatus
622691
ctx.Data["CommitStatuses"] = commitStatuses
692+
ctx.Data["Repos"] = showRepos
693+
ctx.Data["Counts"] = issueCountByRepo
623694
ctx.Data["IssueStats"] = issueStats
624695
ctx.Data["ViewType"] = viewType
625696
ctx.Data["SortType"] = sortType
697+
ctx.Data["RepoIDs"] = selectedRepoIDs
626698
ctx.Data["IsShowClosed"] = isShowClosed
627699
ctx.Data["SelectLabels"] = selectedLabels
628700

@@ -632,9 +704,15 @@ func buildIssueOverview(ctx *context.Context, unitType unit.Type) {
632704
ctx.Data["State"] = "open"
633705
}
634706

707+
// Convert []int64 to string
708+
reposParam, _ := json.Marshal(opts.RepoIDs)
709+
710+
ctx.Data["ReposParam"] = string(reposParam)
711+
635712
pager := context.NewPagination(shownIssues, setting.UI.IssuePagingNum, page, 5)
636713
pager.AddParam(ctx, "q", "Keyword")
637714
pager.AddParam(ctx, "type", "ViewType")
715+
pager.AddParam(ctx, "repos", "ReposParam")
638716
pager.AddParam(ctx, "sort", "SortType")
639717
pager.AddParam(ctx, "state", "State")
640718
pager.AddParam(ctx, "labels", "SelectLabels")
@@ -645,6 +723,55 @@ func buildIssueOverview(ctx *context.Context, unitType unit.Type) {
645723
ctx.HTML(http.StatusOK, tplIssues)
646724
}
647725

726+
func getRepoIDs(reposQuery string) []int64 {
727+
if len(reposQuery) == 0 || reposQuery == "[]" {
728+
return []int64{}
729+
}
730+
if !issueReposQueryPattern.MatchString(reposQuery) {
731+
log.Warn("issueReposQueryPattern does not match query: %q", reposQuery)
732+
return []int64{}
733+
}
734+
735+
var repoIDs []int64
736+
// remove "[" and "]" from string
737+
reposQuery = reposQuery[1 : len(reposQuery)-1]
738+
// for each ID (delimiter ",") add to int to repoIDs
739+
for _, rID := range strings.Split(reposQuery, ",") {
740+
// Ensure nonempty string entries
741+
if rID != "" && rID != "0" {
742+
rIDint64, err := strconv.ParseInt(rID, 10, 64)
743+
if err == nil {
744+
repoIDs = append(repoIDs, rIDint64)
745+
}
746+
}
747+
}
748+
749+
return repoIDs
750+
}
751+
752+
func loadRepoByIDs(ctx *context.Context, ctxUser *user_model.User, issueCountByRepo map[int64]int64, unitType unit.Type) (map[int64]*repo_model.Repository, error) {
753+
totalRes := make(map[int64]*repo_model.Repository, len(issueCountByRepo))
754+
repoIDs := make([]int64, 0, 500)
755+
for id := range issueCountByRepo {
756+
if id <= 0 {
757+
continue
758+
}
759+
repoIDs = append(repoIDs, id)
760+
if len(repoIDs) == 500 {
761+
if err := repo_model.FindReposMapByIDs(ctx, repoIDs, totalRes); err != nil {
762+
return nil, err
763+
}
764+
repoIDs = repoIDs[:0]
765+
}
766+
}
767+
if len(repoIDs) > 0 {
768+
if err := repo_model.FindReposMapByIDs(ctx, repoIDs, totalRes); err != nil {
769+
return nil, err
770+
}
771+
}
772+
return totalRes, nil
773+
}
774+
648775
// ShowSSHKeys output all the ssh keys of user by uid
649776
func ShowSSHKeys(ctx *context.Context) {
650777
keys, err := db.Find[asymkey_model.PublicKey](ctx, asymkey_model.FindPublicKeyOptions{
@@ -761,15 +888,8 @@ func UsernameSubRoute(ctx *context.Context) {
761888
}
762889
}
763890

764-
func getUserIssueStats(ctx *context.Context, ctxUser *user_model.User, filterMode int, opts *issue_indexer.SearchOptions) (*issues_model.IssueStats, error) {
765-
doerID := ctx.Doer.ID
766-
891+
func getUserIssueStats(ctx *context.Context, filterMode int, opts *issue_indexer.SearchOptions, doerID int64) (*issues_model.IssueStats, error) {
767892
opts = opts.Copy(func(o *issue_indexer.SearchOptions) {
768-
// If the doer is the same as the context user, which means the doer is viewing his own dashboard,
769-
// it's not enough to show the repos that the doer owns or has been explicitly granted access to,
770-
// because the doer may create issues or be mentioned in any public repo.
771-
// So we need search issues in all public repos.
772-
o.AllPublic = doerID == ctxUser.ID
773893
o.AssigneeID = nil
774894
o.PosterID = nil
775895
o.MentionID = nil
@@ -785,10 +905,7 @@ func getUserIssueStats(ctx *context.Context, ctxUser *user_model.User, filterMod
785905
{
786906
openClosedOpts := opts.Copy()
787907
switch filterMode {
788-
case issues_model.FilterModeAll:
789-
// no-op
790-
case issues_model.FilterModeYourRepositories:
791-
openClosedOpts.AllPublic = false
908+
case issues_model.FilterModeAll, issues_model.FilterModeYourRepositories:
792909
case issues_model.FilterModeAssign:
793910
openClosedOpts.AssigneeID = &doerID
794911
case issues_model.FilterModeCreate:
@@ -812,7 +929,7 @@ func getUserIssueStats(ctx *context.Context, ctxUser *user_model.User, filterMod
812929
}
813930
}
814931

815-
ret.YourRepositoriesCount, err = issue_indexer.CountIssues(ctx, opts.Copy(func(o *issue_indexer.SearchOptions) { o.AllPublic = false }))
932+
ret.YourRepositoriesCount, err = issue_indexer.CountIssues(ctx, opts)
816933
if err != nil {
817934
return nil, err
818935
}

routers/web/user/home_test.go

+4
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ func TestArchivedIssues(t *testing.T) {
4545
// Assert: One Issue (ID 30) from one Repo (ID 50) is retrieved, while nothing from archived Repo 51 is retrieved
4646
assert.EqualValues(t, http.StatusOK, ctx.Resp.Status())
4747

48+
assert.EqualValues(t, map[int64]int64{50: 1}, ctx.Data["Counts"])
4849
assert.Len(t, ctx.Data["Issues"], 1)
50+
assert.Len(t, ctx.Data["Repos"], 1)
4951
}
5052

5153
func TestIssues(t *testing.T) {
@@ -58,8 +60,10 @@ func TestIssues(t *testing.T) {
5860
Issues(ctx)
5961
assert.EqualValues(t, http.StatusOK, ctx.Resp.Status())
6062

63+
assert.EqualValues(t, map[int64]int64{1: 1, 2: 1}, ctx.Data["Counts"])
6164
assert.EqualValues(t, true, ctx.Data["IsShowClosed"])
6265
assert.Len(t, ctx.Data["Issues"], 1)
66+
assert.Len(t, ctx.Data["Repos"], 2)
6367
}
6468

6569
func TestPulls(t *testing.T) {

0 commit comments

Comments
 (0)