From b7fb20e73e63b8edc9b90c52073e248bef428fcc Mon Sep 17 00:00:00 2001
From: Anbraten <6918444+anbraten@users.noreply.github.com>
Date: Tue, 29 Oct 2024 10:20:49 +0100
Subject: [PATCH] Suggestions for issues (#32327)
closes #16872
---
 package-lock.json                         | 23 ++++--
 package.json                              |  3 +-
 routers/web/repo/issue_suggestions.go     | 93 +++++++++++++++++++++++
 routers/web/web.go                        |  1 +
 templates/shared/combomarkdowneditor.tmpl |  2 +-
 web_src/js/components/ContextPopup.vue    | 33 +-------
 web_src/js/features/comp/TextExpander.ts  | 44 ++++++++++-
 web_src/js/features/issue.ts              | 32 ++++++++
 web_src/js/utils/match.ts                 | 19 ++++-
 9 files changed, 202 insertions(+), 48 deletions(-)
 create mode 100644 routers/web/repo/issue_suggestions.go
 create mode 100644 web_src/js/features/issue.ts
diff --git a/package-lock.json b/package-lock.json
index 3da344a23c..a2bde7e083 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -11,7 +11,7 @@
         "@citation-js/plugin-software-formats": "0.6.1",
         "@github/markdown-toolbar-element": "2.2.3",
         "@github/relative-time-element": "4.4.3",
-        "@github/text-expander-element": "2.7.1",
+        "@github/text-expander-element": "2.8.0",
         "@mcaptcha/vanilla-glue": "0.1.0-alpha-3",
         "@primer/octicons": "19.11.0",
         "@silverwind/vue3-calendar-heatmap": "2.0.6",
@@ -40,6 +40,7 @@
         "monaco-editor": "0.51.0",
         "monaco-editor-webpack-plugin": "7.1.0",
         "pdfobject": "2.3.0",
+        "perfect-debounce": "1.0.0",
         "postcss": "8.4.41",
         "postcss-loader": "8.1.1",
         "postcss-nesting": "13.0.0",
@@ -3115,13 +3116,13 @@
       "license": "MIT"
     },
     "node_modules/@github/text-expander-element": {
-      "version": "2.7.1",
-      "resolved": "https://registry.npmjs.org/@github/text-expander-element/-/text-expander-element-2.7.1.tgz",
-      "integrity": "sha512-CWxfYxJRkeWVCUhJveproLs6pHsPrWtK8TsjL8ByYVcSCs8CJmNzF8b7ZawrUgfai0F2jb4aIdw2FoBTykj9XA==",
+      "version": "2.8.0",
+      "resolved": "https://registry.npmjs.org/@github/text-expander-element/-/text-expander-element-2.8.0.tgz",
+      "integrity": "sha512-kkS2rZ/CG8HGKblpLDQ8vcK/K7l/Jsvzi/N4ovwPAsFSOImcIbJh2MgCv9tzqE3wAm/qXlscvh3Ms4Hh1vtZvw==",
       "license": "MIT",
       "dependencies": {
         "@github/combobox-nav": "^2.0.2",
-        "dom-input-range": "^1.1.6"
+        "dom-input-range": "^1.2.0"
       }
     },
     "node_modules/@humanwhocodes/config-array": {
@@ -7409,9 +7410,9 @@
       }
     },
     "node_modules/dom-input-range": {
-      "version": "1.1.6",
-      "resolved": "https://registry.npmjs.org/dom-input-range/-/dom-input-range-1.1.6.tgz",
-      "integrity": "sha512-4o/SkTpscD0n81BeErrrtmE58lG8vTks++92vk//ld0NmkQTb4AVJ2rexh2yor6rtBf5IMte26u+fF3EgCppPQ==",
+      "version": "1.2.0",
+      "resolved": "https://registry.npmjs.org/dom-input-range/-/dom-input-range-1.2.0.tgz",
+      "integrity": "sha512-8HVA5Oy5Vt872S7IXsjjp6/5Hqsm5YZLhurxwwQXp80T9qVsj8/mEUH3sQlFujLLUoWfxiaThHHuJ3/q1MHVuA==",
       "license": "MIT",
       "workspaces": [
         "demos"
@@ -12460,6 +12461,12 @@
       "integrity": "sha512-w/9pXDXTDs3IDmOri/w8lM/w6LHR0/F4fcBLLzH+4csSoyshQ5su0TE7k0FLHZO7aOjVLDGecqd1M89+PVpVAA==",
       "license": "MIT"
     },
+    "node_modules/perfect-debounce": {
+      "version": "1.0.0",
+      "resolved": "https://registry.npmjs.org/perfect-debounce/-/perfect-debounce-1.0.0.tgz",
+      "integrity": "sha512-xCy9V055GLEqoFaHoC1SoLIaLmWctgCUaBaWxDZ7/Zx4CTyX7cJQLJOok/orfjZAh9kEYpjJa4d0KcJmCbctZA==",
+      "license": "MIT"
+    },
     "node_modules/picocolors": {
       "version": "1.1.1",
       "resolved": "https://registry.npmjs.org/picocolors/-/picocolors-1.1.1.tgz",
diff --git a/package.json b/package.json
index 27b63cb2eb..73cb9d3fa5 100644
--- a/package.json
+++ b/package.json
@@ -10,7 +10,7 @@
     "@citation-js/plugin-software-formats": "0.6.1",
     "@github/markdown-toolbar-element": "2.2.3",
     "@github/relative-time-element": "4.4.3",
-    "@github/text-expander-element": "2.7.1",
+    "@github/text-expander-element": "2.8.0",
     "@mcaptcha/vanilla-glue": "0.1.0-alpha-3",
     "@primer/octicons": "19.11.0",
     "@silverwind/vue3-calendar-heatmap": "2.0.6",
@@ -39,6 +39,7 @@
     "monaco-editor": "0.51.0",
     "monaco-editor-webpack-plugin": "7.1.0",
     "pdfobject": "2.3.0",
+    "perfect-debounce": "1.0.0",
     "postcss": "8.4.41",
     "postcss-loader": "8.1.1",
     "postcss-nesting": "13.0.0",
diff --git a/routers/web/repo/issue_suggestions.go b/routers/web/repo/issue_suggestions.go
new file mode 100644
index 0000000000..361da0ee60
--- /dev/null
+++ b/routers/web/repo/issue_suggestions.go
@@ -0,0 +1,93 @@
+// Copyright 2024 The Gitea Authors. All rights reserved.
+// SPDX-License-Identifier: MIT
+
+package repo
+
+import (
+	"net/http"
+
+	"code.gitea.io/gitea/models/db"
+	issues_model "code.gitea.io/gitea/models/issues"
+	"code.gitea.io/gitea/models/unit"
+	issue_indexer "code.gitea.io/gitea/modules/indexer/issues"
+	"code.gitea.io/gitea/modules/optional"
+	"code.gitea.io/gitea/services/context"
+)
+
+type issueSuggestion struct {
+	ID          int64  `json:"id"`
+	Title       string `json:"title"`
+	State       string `json:"state"`
+	PullRequest *struct {
+		Merged bool `json:"merged"`
+		Draft  bool `json:"draft"`
+	} `json:"pull_request,omitempty"`
+}
+
+// IssueSuggestions returns a list of issue suggestions
+func IssueSuggestions(ctx *context.Context) {
+	keyword := ctx.Req.FormValue("q")
+
+	canReadIssues := ctx.Repo.CanRead(unit.TypeIssues)
+	canReadPulls := ctx.Repo.CanRead(unit.TypePullRequests)
+
+	var isPull optional.Option[bool]
+	if canReadPulls && !canReadIssues {
+		isPull = optional.Some(true)
+	} else if canReadIssues && !canReadPulls {
+		isPull = optional.Some(false)
+	}
+
+	searchOpt := &issue_indexer.SearchOptions{
+		Paginator: &db.ListOptions{
+			Page:     0,
+			PageSize: 5,
+		},
+		Keyword:  keyword,
+		RepoIDs:  []int64{ctx.Repo.Repository.ID},
+		IsPull:   isPull,
+		IsClosed: nil,
+		SortBy:   issue_indexer.SortByUpdatedDesc,
+	}
+
+	ids, _, err := issue_indexer.SearchIssues(ctx, searchOpt)
+	if err != nil {
+		ctx.ServerError("SearchIssues", err)
+		return
+	}
+	issues, err := issues_model.GetIssuesByIDs(ctx, ids, true)
+	if err != nil {
+		ctx.ServerError("FindIssuesByIDs", err)
+		return
+	}
+
+	suggestions := make([]*issueSuggestion, 0, len(issues))
+
+	for _, issue := range issues {
+		suggestion := &issueSuggestion{
+			ID:    issue.ID,
+			Title: issue.Title,
+			State: string(issue.State()),
+		}
+
+		if issue.IsPull {
+			if err := issue.LoadPullRequest(ctx); err != nil {
+				ctx.ServerError("LoadPullRequest", err)
+				return
+			}
+			if issue.PullRequest != nil {
+				suggestion.PullRequest = &struct {
+					Merged bool `json:"merged"`
+					Draft  bool `json:"draft"`
+				}{
+					Merged: issue.PullRequest.HasMerged,
+					Draft:  issue.PullRequest.IsWorkInProgress(ctx),
+				}
+			}
+		}
+
+		suggestions = append(suggestions, suggestion)
+	}
+
+	ctx.JSON(http.StatusOK, suggestions)
+}
diff --git a/routers/web/web.go b/routers/web/web.go
index a6ccb7a792..83d116babd 100644
--- a/routers/web/web.go
+++ b/routers/web/web.go
@@ -1178,6 +1178,7 @@ func registerRoutes(m *web.Router) {
 				})
 			})
 		}, context.RepoRef())
+		m.Get("/issues/suggestions", repo.IssueSuggestions)
 	}, ignSignIn, context.RepoAssignment, reqRepoIssuesOrPullsReader)
 	// end "/{username}/{reponame}": view milestone, label, issue, pull, etc
 
diff --git a/templates/shared/combomarkdowneditor.tmpl b/templates/shared/combomarkdowneditor.tmpl
index a0145ab297..0a01dd9b1d 100644
--- a/templates/shared/combomarkdowneditor.tmpl
+++ b/templates/shared/combomarkdowneditor.tmpl
@@ -44,7 +44,7 @@ Template Attributes:
 				
 			
 		
-		
+