summaryrefslogtreecommitdiffstats
path: root/routers
diff options
context:
space:
mode:
authorLunny Xiao <xiaolunwen@gmail.com>2018-03-16 22:04:33 +0800
committerGitHub <noreply@github.com>2018-03-16 22:04:33 +0800
commit9e5d0a09eb7370daedb1cf572d25ffa150eacf17 (patch)
treef39cbf6a572bda0e82caec7ef1697ceb70e37f1e /routers
parent4163cdf3ea6d6ca43b694de0c15cc93659d6c2b3 (diff)
downloadgitea-9e5d0a09eb7370daedb1cf572d25ffa150eacf17.tar.gz
gitea-9e5d0a09eb7370daedb1cf572d25ffa150eacf17.zip
Global code search support (#3664)
* add global code search on explore * fix bug when no anyone public repos * change the icon * fix typo and add UnitTypeCode check for login non-admin user * fix ui description when no match
Diffstat (limited to 'routers')
-rw-r--r--routers/home.go116
-rw-r--r--routers/repo/search.go3
-rw-r--r--routers/routes/routes.go1
3 files changed, 119 insertions, 1 deletions
diff --git a/routers/home.go b/routers/home.go
index 74125aa2df..4810eb4e6f 100644
--- a/routers/home.go
+++ b/routers/home.go
@@ -11,6 +11,7 @@ import (
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/base"
"code.gitea.io/gitea/modules/context"
+ "code.gitea.io/gitea/modules/search"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/util"
"code.gitea.io/gitea/routers/user"
@@ -27,6 +28,8 @@ const (
tplExploreUsers base.TplName = "explore/users"
// tplExploreOrganizations explore organizations page template
tplExploreOrganizations base.TplName = "explore/organizations"
+ // tplExploreCode explore code page template
+ tplExploreCode base.TplName = "explore/code"
)
// Home render home page
@@ -49,6 +52,7 @@ func Home(ctx *context.Context) {
}
ctx.Data["PageIsHome"] = true
+ ctx.Data["IsRepoIndexerEnabled"] = setting.Indexer.RepoIndexerEnabled
ctx.HTML(200, tplHome)
}
@@ -124,6 +128,7 @@ func RenderRepoSearch(ctx *context.Context, opts *RepoSearchOptions) {
ctx.Data["Total"] = count
ctx.Data["Page"] = paginater.New(int(count), opts.PageSize, page, 5)
ctx.Data["Repos"] = repos
+ ctx.Data["IsRepoIndexerEnabled"] = setting.Indexer.RepoIndexerEnabled
ctx.HTML(200, opts.TplName)
}
@@ -133,6 +138,7 @@ func ExploreRepos(ctx *context.Context) {
ctx.Data["Title"] = ctx.Tr("explore")
ctx.Data["PageIsExplore"] = true
ctx.Data["PageIsExploreRepositories"] = true
+ ctx.Data["IsRepoIndexerEnabled"] = setting.Indexer.RepoIndexerEnabled
var ownerID int64
if ctx.User != nil && !ctx.User.IsAdmin {
@@ -194,6 +200,7 @@ func RenderUserSearch(ctx *context.Context, opts *models.SearchUserOptions, tplN
ctx.Data["Page"] = paginater.New(int(count), opts.PageSize, opts.Page, 5)
ctx.Data["Users"] = users
ctx.Data["ShowUserEmail"] = setting.UI.ShowUserEmail
+ ctx.Data["IsRepoIndexerEnabled"] = setting.Indexer.RepoIndexerEnabled
ctx.HTML(200, tplName)
}
@@ -203,6 +210,7 @@ func ExploreUsers(ctx *context.Context) {
ctx.Data["Title"] = ctx.Tr("explore")
ctx.Data["PageIsExplore"] = true
ctx.Data["PageIsExploreUsers"] = true
+ ctx.Data["IsRepoIndexerEnabled"] = setting.Indexer.RepoIndexerEnabled
RenderUserSearch(ctx, &models.SearchUserOptions{
Type: models.UserTypeIndividual,
@@ -216,6 +224,7 @@ func ExploreOrganizations(ctx *context.Context) {
ctx.Data["Title"] = ctx.Tr("explore")
ctx.Data["PageIsExplore"] = true
ctx.Data["PageIsExploreOrganizations"] = true
+ ctx.Data["IsRepoIndexerEnabled"] = setting.Indexer.RepoIndexerEnabled
RenderUserSearch(ctx, &models.SearchUserOptions{
Type: models.UserTypeOrganization,
@@ -223,6 +232,113 @@ func ExploreOrganizations(ctx *context.Context) {
}, tplExploreOrganizations)
}
+// ExploreCode render explore code page
+func ExploreCode(ctx *context.Context) {
+ if !setting.Indexer.RepoIndexerEnabled {
+ ctx.Redirect("/explore", 302)
+ return
+ }
+
+ ctx.Data["IsRepoIndexerEnabled"] = setting.Indexer.RepoIndexerEnabled
+ ctx.Data["Title"] = ctx.Tr("explore")
+ ctx.Data["PageIsExplore"] = true
+ ctx.Data["PageIsExploreCode"] = true
+
+ keyword := strings.TrimSpace(ctx.Query("q"))
+ page := ctx.QueryInt("page")
+ if page <= 0 {
+ page = 1
+ }
+
+ var (
+ repoIDs []int64
+ err error
+ isAdmin bool
+ userID int64
+ )
+ if ctx.User != nil {
+ userID = ctx.User.ID
+ isAdmin = ctx.User.IsAdmin
+ }
+
+ // guest user or non-admin user
+ if ctx.User == nil || !isAdmin {
+ repoIDs, err = models.FindUserAccessibleRepoIDs(userID)
+ if err != nil {
+ ctx.ServerError("SearchResults", err)
+ return
+ }
+ }
+
+ var (
+ total int
+ searchResults []*search.Result
+ )
+
+ // if non-admin login user, we need check UnitTypeCode at first
+ if ctx.User != nil && len(repoIDs) > 0 {
+ repoMaps, err := models.GetRepositoriesMapByIDs(repoIDs)
+ if err != nil {
+ ctx.ServerError("SearchResults", err)
+ return
+ }
+
+ var rightRepoMap = make(map[int64]*models.Repository, len(repoMaps))
+ repoIDs = make([]int64, 0, len(repoMaps))
+ for id, repo := range repoMaps {
+ if repo.CheckUnitUser(userID, isAdmin, models.UnitTypeCode) {
+ rightRepoMap[id] = repo
+ repoIDs = append(repoIDs, id)
+ }
+ }
+
+ ctx.Data["RepoMaps"] = rightRepoMap
+
+ total, searchResults, err = search.PerformSearch(repoIDs, keyword, page, setting.UI.RepoSearchPagingNum)
+ if err != nil {
+ ctx.ServerError("SearchResults", err)
+ return
+ }
+ // if non-login user or isAdmin, no need to check UnitTypeCode
+ } else if (ctx.User == nil && len(repoIDs) > 0) || isAdmin {
+ total, searchResults, err = search.PerformSearch(repoIDs, keyword, page, setting.UI.RepoSearchPagingNum)
+ if err != nil {
+ ctx.ServerError("SearchResults", err)
+ return
+ }
+
+ var loadRepoIDs = make([]int64, 0, len(searchResults))
+ for _, result := range searchResults {
+ var find bool
+ for _, id := range loadRepoIDs {
+ if id == result.RepoID {
+ find = true
+ break
+ }
+ }
+ if !find {
+ loadRepoIDs = append(loadRepoIDs, result.RepoID)
+ }
+ }
+
+ repoMaps, err := models.GetRepositoriesMapByIDs(loadRepoIDs)
+ if err != nil {
+ ctx.ServerError("SearchResults", err)
+ return
+ }
+
+ ctx.Data["RepoMaps"] = repoMaps
+ }
+
+ ctx.Data["Keyword"] = keyword
+ pager := paginater.New(total, setting.UI.RepoSearchPagingNum, page, 5)
+ ctx.Data["Page"] = pager
+ ctx.Data["SearchResults"] = searchResults
+ ctx.Data["RequireHighlightJS"] = true
+ ctx.Data["PageIsViewCode"] = true
+ ctx.HTML(200, tplExploreCode)
+}
+
// NotFound render 404 page
func NotFound(ctx *context.Context) {
ctx.Data["Title"] = "Page Not Found"
diff --git a/routers/repo/search.go b/routers/repo/search.go
index ed209f2ddc..95715c30c6 100644
--- a/routers/repo/search.go
+++ b/routers/repo/search.go
@@ -29,7 +29,8 @@ func Search(ctx *context.Context) {
if page <= 0 {
page = 1
}
- total, searchResults, err := search.PerformSearch(ctx.Repo.Repository.ID, keyword, page, setting.UI.RepoSearchPagingNum)
+ total, searchResults, err := search.PerformSearch([]int64{ctx.Repo.Repository.ID},
+ keyword, page, setting.UI.RepoSearchPagingNum)
if err != nil {
ctx.ServerError("SearchResults", err)
return
diff --git a/routers/routes/routes.go b/routers/routes/routes.go
index 1d95bb4c76..da7157080a 100644
--- a/routers/routes/routes.go
+++ b/routers/routes/routes.go
@@ -170,6 +170,7 @@ func RegisterRoutes(m *macaron.Macaron) {
m.Get("/repos", routers.ExploreRepos)
m.Get("/users", routers.ExploreUsers)
m.Get("/organizations", routers.ExploreOrganizations)
+ m.Get("/code", routers.ExploreCode)
}, ignSignIn)
m.Combo("/install", routers.InstallInit).Get(routers.Install).
Post(bindIgnErr(auth.InstallForm{}), routers.InstallPost)