From 4bcd2f67c838e6644348b7d99f832bb42fb746d8 Mon Sep 17 00:00:00 2001 From: Richard Mahn Date: Tue, 5 Feb 2019 16:46:30 -0700 Subject: [PATCH 1/7] Feature for searching org repos --- models/org.go | 9 +++++++ routers/user/home.go | 60 ++++++++++++++++++++++++++++++++++++----- templates/org/home.tmpl | 3 ++- 3 files changed, 64 insertions(+), 8 deletions(-) diff --git a/models/org.go b/models/org.go index 3f17e3641fd3a..7b7862ee1ca16 100644 --- a/models/org.go +++ b/models/org.go @@ -620,6 +620,7 @@ type AccessibleReposEnvironment interface { RepoIDs(page, pageSize int) ([]int64, error) Repos(page, pageSize int) ([]*Repository, error) MirrorRepos() ([]*Repository, error) + AddKeyword(keyword string) () } type accessibleReposEnv struct { @@ -627,6 +628,7 @@ type accessibleReposEnv struct { userID int64 teamIDs []int64 e Engine + keyword string } // AccessibleReposEnv an AccessibleReposEnvironment for the repositories in `org` @@ -656,6 +658,9 @@ func (env *accessibleReposEnv) cond() builder.Cond { if len(env.teamIDs) > 0 { cond = cond.Or(builder.In("team_repo.team_id", env.teamIDs)) } + if env.keyword != "" { + cond = cond.And(builder.Like{"`repository`.lower_name", strings.ToLower(env.keyword)}) + } return cond } @@ -731,3 +736,7 @@ func (env *accessibleReposEnv) MirrorRepos() ([]*Repository, error) { In("`repository`.id", repoIDs). Find(&repos) } + +func (env *accessibleReposEnv) AddKeyword(keyword string) () { + env.keyword = keyword +} diff --git a/routers/user/home.go b/routers/user/home.go index 99c747e12cf9c..b04cb3e7eea9c 100644 --- a/routers/user/home.go +++ b/routers/user/home.go @@ -1,4 +1,4 @@ -// Copyright 2014 The Gogs Authors. All rights reserved. +// Copyright 2019 The Gogs Authors. All rights reserved. // Use of this source code is governed by a MIT-style // license that can be found in the LICENSE file. @@ -387,6 +387,29 @@ func showOrgProfile(ctx *context.Context) { org := ctx.Org.Organization ctx.Data["Title"] = org.DisplayName() + var orderBy models.SearchOrderBy + ctx.Data["SortType"] = ctx.Query("sort") + switch ctx.Query("sort") { + case "newest": + orderBy = models.SearchOrderByNewest + case "oldest": + orderBy = models.SearchOrderByOldest + case "recentupdate": + orderBy = models.SearchOrderByRecentUpdated + case "leastupdate": + orderBy = models.SearchOrderByLeastUpdated + case "reversealphabetically": + orderBy = models.SearchOrderByAlphabeticallyReverse + case "alphabetically": + orderBy = models.SearchOrderByAlphabetically + default: + ctx.Data["SortType"] = "recentupdate" + orderBy = models.SearchOrderByRecentUpdated + } + + keyword := strings.Trim(ctx.Query("q"), " ") + ctx.Data["Keyword"] = keyword + page := ctx.QueryInt("page") if page <= 0 { page = 1 @@ -403,6 +426,9 @@ func showOrgProfile(ctx *context.Context) { ctx.ServerError("AccessibleReposEnv", err) return } + if len(keyword) != 0 { + env.AddKeyword(keyword) + } repos, err = env.Repos(page, setting.UI.User.RepoPagingNum) if err != nil { ctx.ServerError("env.Repos", err) @@ -416,13 +442,33 @@ func showOrgProfile(ctx *context.Context) { ctx.Data["Repos"] = repos } else { showPrivate := ctx.IsSigned && ctx.User.IsAdmin - repos, err = models.GetUserRepositories(org.ID, showPrivate, page, setting.UI.User.RepoPagingNum, "") - if err != nil { - ctx.ServerError("GetRepositories", err) - return + if len(keyword) == 0 { + repos, err = models.GetUserRepositories(org.ID, showPrivate, page, setting.UI.User.RepoPagingNum, orderBy.String()) + if err != nil { + ctx.ServerError("GetRepositories", err) + return + } + ctx.Data["Repos"] = repos + count = models.CountUserRepositories(org.ID, showPrivate) + ctx.Data["Total"] = count + } else { + repos, count, err = models.SearchRepositoryByName(&models.SearchRepoOptions{ + Keyword: keyword, + OwnerID: org.ID, + OrderBy: orderBy, + Private: showPrivate, + Page: page, + IsProfile: true, + PageSize: setting.UI.User.RepoPagingNum, + }) + if err != nil { + ctx.ServerError("SearchRepositoryByName", err) + return + } + + ctx.Data["Repos"] = repos + ctx.Data["Total"] = count } - ctx.Data["Repos"] = repos - count = models.CountUserRepositories(org.ID, showPrivate) } ctx.Data["Page"] = paginater.New(int(count), setting.UI.User.RepoPagingNum, page, 5) diff --git a/templates/org/home.tmpl b/templates/org/home.tmpl index 13b557bd7a019..745f81bc7e138 100644 --- a/templates/org/home.tmpl +++ b/templates/org/home.tmpl @@ -22,12 +22,13 @@
- {{if .IsOrganizationOwner}} + {{if .IsOrganizationOwner}}
{{end}} + {{template "explore/repo_search" .}} {{template "explore/repo_list" .}} {{template "base/paginate" .}}
From dc5bb0121d164092787d620655fdcfb6fa21bf70 Mon Sep 17 00:00:00 2001 From: Richard Mahn Date: Wed, 6 Feb 2019 15:27:43 -0700 Subject: [PATCH 2/7] Adds newest sort options --- routers/user/home.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/routers/user/home.go b/routers/user/home.go index b04cb3e7eea9c..a11835d0f06dd 100644 --- a/routers/user/home.go +++ b/routers/user/home.go @@ -402,6 +402,14 @@ func showOrgProfile(ctx *context.Context) { orderBy = models.SearchOrderByAlphabeticallyReverse case "alphabetically": orderBy = models.SearchOrderByAlphabetically + case "moststars": + orderBy = models.SearchOrderByStarsReverse + case "feweststars": + orderBy = models.SearchOrderByStars + case "mostforks": + orderBy = models.SearchOrderByForksReverse + case "fewestforks": + orderBy = models.SearchOrderByForks default: ctx.Data["SortType"] = "recentupdate" orderBy = models.SearchOrderByRecentUpdated From be8a58b69396947d7791d2dba4848da062f67e6c Mon Sep 17 00:00:00 2001 From: Richard Mahn Date: Wed, 6 Feb 2019 15:29:16 -0700 Subject: [PATCH 3/7] Formatting fixes --- models/org.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/models/org.go b/models/org.go index 7b7862ee1ca16..daff110cf534c 100644 --- a/models/org.go +++ b/models/org.go @@ -620,7 +620,7 @@ type AccessibleReposEnvironment interface { RepoIDs(page, pageSize int) ([]int64, error) Repos(page, pageSize int) ([]*Repository, error) MirrorRepos() ([]*Repository, error) - AddKeyword(keyword string) () + AddKeyword(keyword string) } type accessibleReposEnv struct { @@ -628,7 +628,7 @@ type accessibleReposEnv struct { userID int64 teamIDs []int64 e Engine - keyword string + keyword string } // AccessibleReposEnv an AccessibleReposEnvironment for the repositories in `org` @@ -737,6 +737,6 @@ func (env *accessibleReposEnv) MirrorRepos() ([]*Repository, error) { Find(&repos) } -func (env *accessibleReposEnv) AddKeyword(keyword string) () { +func (env *accessibleReposEnv) AddKeyword(keyword string) { env.keyword = keyword } From d244381cc95d788e528a5ea1811f4e634546c62a Mon Sep 17 00:00:00 2001 From: Richard Mahn Date: Wed, 6 Feb 2019 15:35:51 -0700 Subject: [PATCH 4/7] Code cleanup --- templates/org/home.tmpl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/org/home.tmpl b/templates/org/home.tmpl index 745f81bc7e138..03bb5252767f7 100644 --- a/templates/org/home.tmpl +++ b/templates/org/home.tmpl @@ -22,7 +22,7 @@
- {{if .IsOrganizationOwner}} + {{if .IsOrganizationOwner}} From 7d70dd5bf5ed944ec723f33fde5e2f375ea70a49 Mon Sep 17 00:00:00 2001 From: Richard Mahn Date: Wed, 6 Feb 2019 15:50:20 -0700 Subject: [PATCH 5/7] Updates copyright header for home.go --- routers/user/home.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/routers/user/home.go b/routers/user/home.go index a11835d0f06dd..73078216c8579 100644 --- a/routers/user/home.go +++ b/routers/user/home.go @@ -1,4 +1,5 @@ -// Copyright 2019 The Gogs Authors. All rights reserved. +// Copyright 2014 The Gogs Authors. All rights reserved. +// Copyright 2019 The Gitea Authors. All rights reserved. // Use of this source code is governed by a MIT-style // license that can be found in the LICENSE file. From c4b394365ffed518ffec704afc913f82e80b20cb Mon Sep 17 00:00:00 2001 From: Richard Mahn Date: Thu, 7 Feb 2019 05:54:54 -0700 Subject: [PATCH 6/7] ctx.Data assignments put together after if/else block --- routers/user/home.go | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/routers/user/home.go b/routers/user/home.go index 73078216c8579..67a882af9c8ff 100644 --- a/routers/user/home.go +++ b/routers/user/home.go @@ -448,7 +448,6 @@ func showOrgProfile(ctx *context.Context) { ctx.ServerError("env.CountRepos", err) return } - ctx.Data["Repos"] = repos } else { showPrivate := ctx.IsSigned && ctx.User.IsAdmin if len(keyword) == 0 { @@ -457,9 +456,7 @@ func showOrgProfile(ctx *context.Context) { ctx.ServerError("GetRepositories", err) return } - ctx.Data["Repos"] = repos count = models.CountUserRepositories(org.ID, showPrivate) - ctx.Data["Total"] = count } else { repos, count, err = models.SearchRepositoryByName(&models.SearchRepoOptions{ Keyword: keyword, @@ -474,19 +471,18 @@ func showOrgProfile(ctx *context.Context) { ctx.ServerError("SearchRepositoryByName", err) return } - - ctx.Data["Repos"] = repos - ctx.Data["Total"] = count } } - ctx.Data["Page"] = paginater.New(int(count), setting.UI.User.RepoPagingNum, page, 5) if err := org.GetMembers(); err != nil { ctx.ServerError("GetMembers", err) return } - ctx.Data["Members"] = org.Members + ctx.Data["Repos"] = repos + ctx.Data["Total"] = count + ctx.Data["Page"] = paginater.New(int(count), setting.UI.User.RepoPagingNum, page, 5) + ctx.Data["Members"] = org.Members ctx.Data["Teams"] = org.Teams ctx.HTML(200, tplOrgHome) From 3974320e7e0a1e456800085b17d883e735f4b486 Mon Sep 17 00:00:00 2001 From: Richard Mahn Date: Thu, 7 Feb 2019 14:30:31 -0700 Subject: [PATCH 7/7] Updates repo_search.tmpl to retain the sort order --- templates/explore/repo_search.tmpl | 1 + 1 file changed, 1 insertion(+) diff --git a/templates/explore/repo_search.tmpl b/templates/explore/repo_search.tmpl index de0b63e077925..62bb5ea3c2813 100644 --- a/templates/explore/repo_search.tmpl +++ b/templates/explore/repo_search.tmpl @@ -23,6 +23,7 @@
+