From b658f2c61bc70f0ce79d1be7eb1d7654273716e3 Mon Sep 17 00:00:00 2001
From: Jason Song <i@wolfogre.com>
Date: Fri, 18 Aug 2023 01:42:17 +0800
Subject: [PATCH] Return empty when searching issues with no repos (#26545)

---
 routers/api/v1/repo/issue.go |  4 ++++
 routers/web/repo/issue.go    |  4 ++++
 routers/web/user/home.go     | 20 +++++++++++++++-----
 3 files changed, 23 insertions(+), 5 deletions(-)

diff --git a/routers/api/v1/repo/issue.go b/routers/api/v1/repo/issue.go
index 861e63a9b8..a08fdf5940 100644
--- a/routers/api/v1/repo/issue.go
+++ b/routers/api/v1/repo/issue.go
@@ -193,6 +193,10 @@ func SearchIssues(ctx *context.APIContext) {
 			ctx.Error(http.StatusInternalServerError, "SearchRepositoryIDs", err)
 			return
 		}
+		if len(repoIDs) == 0 {
+			// no repos found, don't let the indexer return all repos
+			repoIDs = []int64{0}
+		}
 	}
 
 	keyword := ctx.FormTrim("q")
diff --git a/routers/web/repo/issue.go b/routers/web/repo/issue.go
index 565687defb..ede0ceed3b 100644
--- a/routers/web/repo/issue.go
+++ b/routers/web/repo/issue.go
@@ -2503,6 +2503,10 @@ func SearchIssues(ctx *context.Context) {
 			ctx.Error(http.StatusInternalServerError, "SearchRepositoryIDs", err.Error())
 			return
 		}
+		if len(repoIDs) == 0 {
+			// no repos found, don't let the indexer return all repos
+			repoIDs = []int64{0}
+		}
 	}
 
 	keyword := ctx.FormTrim("q")
diff --git a/routers/web/user/home.go b/routers/web/user/home.go
index bf19cc513d..8c1447f707 100644
--- a/routers/web/user/home.go
+++ b/routers/web/user/home.go
@@ -454,10 +454,21 @@ func buildIssueOverview(ctx *context.Context, unitType unit.Type) {
 		AllPublic:  false,
 		AllLimited: false,
 	}
-
 	if team != nil {
 		repoOpts.TeamID = team.ID
 	}
+	{
+		ids, _, err := repo_model.SearchRepositoryIDs(repoOpts)
+		if err != nil {
+			ctx.ServerError("SearchRepositoryIDs", err)
+			return
+		}
+		opts.RepoIDs = ids
+		if len(opts.RepoIDs) == 0 {
+			// no repos found, don't let the indexer return all repos
+			opts.RepoIDs = []int64{0}
+		}
+	}
 
 	switch filterMode {
 	case issues_model.FilterModeAll:
@@ -541,15 +552,13 @@ func buildIssueOverview(ctx *context.Context, unitType unit.Type) {
 	// Parse ctx.FormString("repos") and remember matched repo IDs for later.
 	// Gets set when clicking filters on the issues overview page.
 	repoIDs := getRepoIDs(ctx.FormString("repos"))
-	if len(repoIDs) == 0 {
-		repoIDs = accessibleRepos.Values()
-	} else {
+	if len(repoIDs) > 0 {
 		// Remove repo IDs that are not accessible to the user.
 		repoIDs = util.SliceRemoveAllFunc(repoIDs, func(v int64) bool {
 			return !accessibleRepos.Contains(v)
 		})
+		opts.RepoIDs = repoIDs
 	}
-	opts.RepoIDs = repoIDs
 
 	// ------------------------------
 	// Get issues as defined by opts.
@@ -609,6 +618,7 @@ func buildIssueOverview(ctx *context.Context, unitType unit.Type) {
 	var issueStats *issues_model.IssueStats
 	{
 		statsOpts := issues_model.IssuesOptions{
+			RepoIDs:    repoIDs,
 			User:       ctx.Doer,
 			IsPull:     util.OptionalBoolOf(isPullList),
 			IsClosed:   util.OptionalBoolOf(isShowClosed),