mirror of
https://github.com/gogs/gogs.git
synced 2025-12-20 15:20:01 +01:00
explore: able list and search for private but accessible repositories (#3088)
This commit is contained in:
2
gogs.go
2
gogs.go
@@ -16,7 +16,7 @@ import (
|
|||||||
"github.com/gogits/gogs/modules/setting"
|
"github.com/gogits/gogs/modules/setting"
|
||||||
)
|
)
|
||||||
|
|
||||||
const APP_VER = "0.10.21.0316"
|
const APP_VER = "0.10.22.0317"
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
setting.AppVer = APP_VER
|
setting.AppVer = APP_VER
|
||||||
|
|||||||
@@ -620,16 +620,18 @@ func (prs PullRequestList) loadAttributes(e Engine) (err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Load issues
|
// Load issues
|
||||||
issueIDs := make([]int64, 0, len(prs))
|
set := make(map[int64]*Issue)
|
||||||
for i := range prs {
|
for i := range prs {
|
||||||
issueIDs = append(issueIDs, prs[i].IssueID)
|
set[prs[i].IssueID] = nil
|
||||||
|
}
|
||||||
|
issueIDs := make([]int64, 0, len(prs))
|
||||||
|
for issueID := range set {
|
||||||
|
issueIDs = append(issueIDs, issueID)
|
||||||
}
|
}
|
||||||
issues := make([]*Issue, 0, len(issueIDs))
|
issues := make([]*Issue, 0, len(issueIDs))
|
||||||
if err = e.Where("id > 0").In("id", issueIDs).Find(&issues); err != nil {
|
if err = e.Where("id > 0").In("id", issueIDs).Find(&issues); err != nil {
|
||||||
return fmt.Errorf("find issues: %v", err)
|
return fmt.Errorf("find issues: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
set := make(map[int64]*Issue)
|
|
||||||
for i := range issues {
|
for i := range issues {
|
||||||
set[issues[i].ID] = issues[i]
|
set[issues[i].ID] = issues[i]
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1581,11 +1581,6 @@ type SearchRepoOptions struct {
|
|||||||
// SearchRepositoryByName takes keyword and part of repository name to search,
|
// SearchRepositoryByName takes keyword and part of repository name to search,
|
||||||
// it returns results in given range and number of total results.
|
// it returns results in given range and number of total results.
|
||||||
func SearchRepositoryByName(opts *SearchRepoOptions) (repos []*Repository, _ int64, _ error) {
|
func SearchRepositoryByName(opts *SearchRepoOptions) (repos []*Repository, _ int64, _ error) {
|
||||||
if len(opts.Keyword) == 0 {
|
|
||||||
return repos, 0, nil
|
|
||||||
}
|
|
||||||
opts.Keyword = strings.ToLower(opts.Keyword)
|
|
||||||
|
|
||||||
if opts.Page <= 0 {
|
if opts.Page <= 0 {
|
||||||
opts.Page = 1
|
opts.Page = 1
|
||||||
}
|
}
|
||||||
@@ -1596,15 +1591,16 @@ func SearchRepositoryByName(opts *SearchRepoOptions) (repos []*Repository, _ int
|
|||||||
// this does not include other people's private repositories even if opts.UserID is an admin.
|
// this does not include other people's private repositories even if opts.UserID is an admin.
|
||||||
if !opts.Private && opts.UserID > 0 {
|
if !opts.Private && opts.UserID > 0 {
|
||||||
sess.Join("LEFT", "access", "access.repo_id = repo.id").
|
sess.Join("LEFT", "access", "access.repo_id = repo.id").
|
||||||
Where("repo.lower_name LIKE ? AND (repo.owner_id = ? OR access.user_id = ? OR repo.is_private = ?)",
|
Where("(repo.owner_id = ? OR access.user_id = ? OR repo.is_private = ?)", opts.UserID, opts.UserID, false)
|
||||||
"%"+opts.Keyword+"%", opts.UserID, opts.UserID, false)
|
|
||||||
} else {
|
} else {
|
||||||
sess.Where("repo.lower_name LIKE ?", "%"+opts.Keyword+"%")
|
|
||||||
// Only return public repositories if opts.Private is not set
|
// Only return public repositories if opts.Private is not set
|
||||||
if !opts.Private {
|
if !opts.Private {
|
||||||
sess.And("repo.is_private = ?", false)
|
sess.And("repo.is_private = ?", false)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if len(opts.Keyword) > 0 {
|
||||||
|
sess.And("repo.lower_name LIKE ?", "%"+strings.ToLower(opts.Keyword)+"%")
|
||||||
|
}
|
||||||
if opts.OwnerID > 0 {
|
if opts.OwnerID > 0 {
|
||||||
sess.And("repo.owner_id = ?", opts.OwnerID)
|
sess.And("repo.owner_id = ?", opts.OwnerID)
|
||||||
}
|
}
|
||||||
@@ -1949,7 +1945,7 @@ func (repos RepositoryList) loadAttributes(e Engine) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Load owners.
|
// Load owners
|
||||||
set := make(map[int64]*User)
|
set := make(map[int64]*User)
|
||||||
for i := range repos {
|
for i := range repos {
|
||||||
set[repos[i].OwnerID] = nil
|
set[repos[i].OwnerID] = nil
|
||||||
|
|||||||
@@ -42,6 +42,13 @@ type Context struct {
|
|||||||
Org *Organization
|
Org *Organization
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (ctx *Context) UserID() int64 {
|
||||||
|
if !ctx.IsSigned {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
return ctx.User.ID
|
||||||
|
}
|
||||||
|
|
||||||
// HasError returns true if error occurs in form validation.
|
// HasError returns true if error occurs in form validation.
|
||||||
func (ctx *Context) HasApiError() bool {
|
func (ctx *Context) HasApiError() bool {
|
||||||
hasErr, ok := ctx.Data["HasError"]
|
hasErr, ok := ctx.Data["HasError"]
|
||||||
|
|||||||
@@ -5,13 +5,13 @@
|
|||||||
package admin
|
package admin
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/Unknwon/paginater"
|
||||||
log "gopkg.in/clog.v1"
|
log "gopkg.in/clog.v1"
|
||||||
|
|
||||||
"github.com/gogits/gogs/models"
|
"github.com/gogits/gogs/models"
|
||||||
"github.com/gogits/gogs/modules/base"
|
"github.com/gogits/gogs/modules/base"
|
||||||
"github.com/gogits/gogs/modules/context"
|
"github.com/gogits/gogs/modules/context"
|
||||||
"github.com/gogits/gogs/modules/setting"
|
"github.com/gogits/gogs/modules/setting"
|
||||||
"github.com/gogits/gogs/routers"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -23,14 +23,49 @@ func Repos(ctx *context.Context) {
|
|||||||
ctx.Data["PageIsAdmin"] = true
|
ctx.Data["PageIsAdmin"] = true
|
||||||
ctx.Data["PageIsAdminRepositories"] = true
|
ctx.Data["PageIsAdminRepositories"] = true
|
||||||
|
|
||||||
routers.RenderRepoSearch(ctx, &routers.RepoSearchOptions{
|
page := ctx.QueryInt("page")
|
||||||
Counter: models.CountRepositories,
|
if page <= 0 {
|
||||||
Ranger: models.Repositories,
|
page = 1
|
||||||
Private: true,
|
}
|
||||||
PageSize: setting.UI.Admin.RepoPagingNum,
|
|
||||||
|
var (
|
||||||
|
repos []*models.Repository
|
||||||
|
count int64
|
||||||
|
err error
|
||||||
|
)
|
||||||
|
|
||||||
|
keyword := ctx.Query("q")
|
||||||
|
if len(keyword) == 0 {
|
||||||
|
repos, err = models.Repositories(page, setting.UI.Admin.RepoPagingNum)
|
||||||
|
if err != nil {
|
||||||
|
ctx.Handle(500, "Repositories", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
count = models.CountRepositories(true)
|
||||||
|
} else {
|
||||||
|
repos, count, err = models.SearchRepositoryByName(&models.SearchRepoOptions{
|
||||||
|
Keyword: keyword,
|
||||||
OrderBy: "id ASC",
|
OrderBy: "id ASC",
|
||||||
TplName: REPOS,
|
Private: true,
|
||||||
|
Page: page,
|
||||||
|
PageSize: setting.UI.Admin.RepoPagingNum,
|
||||||
})
|
})
|
||||||
|
if err != nil {
|
||||||
|
ctx.Handle(500, "SearchRepositoryByName", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ctx.Data["Keyword"] = keyword
|
||||||
|
ctx.Data["Total"] = count
|
||||||
|
ctx.Data["Page"] = paginater.New(int(count), setting.UI.Admin.RepoPagingNum, page, 5)
|
||||||
|
|
||||||
|
if err = models.RepositoryList(repos).LoadAttributes(); err != nil {
|
||||||
|
ctx.Handle(500, "LoadAttributes", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
ctx.Data["Repos"] = repos
|
||||||
|
|
||||||
|
ctx.HTML(200, REPOS)
|
||||||
}
|
}
|
||||||
|
|
||||||
func DeleteRepo(ctx *context.Context) {
|
func DeleteRepo(ctx *context.Context) {
|
||||||
|
|||||||
@@ -5,8 +5,6 @@
|
|||||||
package routers
|
package routers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"github.com/Unknwon/paginater"
|
"github.com/Unknwon/paginater"
|
||||||
|
|
||||||
"github.com/gogits/gogs/models"
|
"github.com/gogits/gogs/models"
|
||||||
@@ -45,80 +43,39 @@ func Home(ctx *context.Context) {
|
|||||||
ctx.HTML(200, HOME)
|
ctx.HTML(200, HOME)
|
||||||
}
|
}
|
||||||
|
|
||||||
type RepoSearchOptions struct {
|
|
||||||
Counter func(bool) int64
|
|
||||||
Ranger func(int, int) ([]*models.Repository, error)
|
|
||||||
Private bool
|
|
||||||
PageSize int
|
|
||||||
OrderBy string
|
|
||||||
TplName base.TplName
|
|
||||||
}
|
|
||||||
|
|
||||||
func RenderRepoSearch(ctx *context.Context, opts *RepoSearchOptions) {
|
|
||||||
page := ctx.QueryInt("page")
|
|
||||||
if page <= 0 {
|
|
||||||
page = 1
|
|
||||||
}
|
|
||||||
|
|
||||||
var (
|
|
||||||
repos []*models.Repository
|
|
||||||
count int64
|
|
||||||
err error
|
|
||||||
)
|
|
||||||
|
|
||||||
keyword := ctx.Query("q")
|
|
||||||
if len(keyword) == 0 {
|
|
||||||
repos, err = opts.Ranger(page, opts.PageSize)
|
|
||||||
if err != nil {
|
|
||||||
ctx.Handle(500, "opts.Ranger", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
count = opts.Counter(opts.Private)
|
|
||||||
} else {
|
|
||||||
var ctxUserID int64
|
|
||||||
if ctx.IsSigned {
|
|
||||||
ctxUserID = ctx.User.ID
|
|
||||||
}
|
|
||||||
repos, count, err = models.SearchRepositoryByName(&models.SearchRepoOptions{
|
|
||||||
Keyword: keyword,
|
|
||||||
UserID: ctxUserID,
|
|
||||||
OrderBy: opts.OrderBy,
|
|
||||||
Private: opts.Private,
|
|
||||||
Page: page,
|
|
||||||
PageSize: opts.PageSize,
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
ctx.Handle(500, "SearchRepositoryByName", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
ctx.Data["Keyword"] = keyword
|
|
||||||
ctx.Data["Total"] = count
|
|
||||||
ctx.Data["Page"] = paginater.New(int(count), opts.PageSize, page, 5)
|
|
||||||
|
|
||||||
for _, repo := range repos {
|
|
||||||
if err = repo.GetOwner(); err != nil {
|
|
||||||
ctx.Handle(500, "GetOwner", fmt.Errorf("%d: %v", repo.ID, err))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
ctx.Data["Repos"] = repos
|
|
||||||
|
|
||||||
ctx.HTML(200, opts.TplName)
|
|
||||||
}
|
|
||||||
|
|
||||||
func ExploreRepos(ctx *context.Context) {
|
func ExploreRepos(ctx *context.Context) {
|
||||||
ctx.Data["Title"] = ctx.Tr("explore")
|
ctx.Data["Title"] = ctx.Tr("explore")
|
||||||
ctx.Data["PageIsExplore"] = true
|
ctx.Data["PageIsExplore"] = true
|
||||||
ctx.Data["PageIsExploreRepositories"] = true
|
ctx.Data["PageIsExploreRepositories"] = true
|
||||||
|
|
||||||
RenderRepoSearch(ctx, &RepoSearchOptions{
|
page := ctx.QueryInt("page")
|
||||||
Counter: models.CountRepositories,
|
if page <= 0 {
|
||||||
Ranger: models.GetRecentUpdatedRepositories,
|
page = 1
|
||||||
PageSize: setting.UI.ExplorePagingNum,
|
}
|
||||||
|
|
||||||
|
keyword := ctx.Query("q")
|
||||||
|
repos, count, err := models.SearchRepositoryByName(&models.SearchRepoOptions{
|
||||||
|
Keyword: keyword,
|
||||||
|
UserID: ctx.UserID(),
|
||||||
OrderBy: "updated_unix DESC",
|
OrderBy: "updated_unix DESC",
|
||||||
TplName: EXPLORE_REPOS,
|
Page: page,
|
||||||
|
PageSize: setting.UI.ExplorePagingNum,
|
||||||
})
|
})
|
||||||
|
if err != nil {
|
||||||
|
ctx.Handle(500, "SearchRepositoryByName", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
ctx.Data["Keyword"] = keyword
|
||||||
|
ctx.Data["Total"] = count
|
||||||
|
ctx.Data["Page"] = paginater.New(int(count), setting.UI.ExplorePagingNum, page, 5)
|
||||||
|
|
||||||
|
if err = models.RepositoryList(repos).LoadAttributes(); err != nil {
|
||||||
|
ctx.Handle(500, "LoadAttributes", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
ctx.Data["Repos"] = repos
|
||||||
|
|
||||||
|
ctx.HTML(200, EXPLORE_REPOS)
|
||||||
}
|
}
|
||||||
|
|
||||||
type UserSearchOptions struct {
|
type UserSearchOptions struct {
|
||||||
|
|||||||
@@ -1 +1 @@
|
|||||||
0.10.21.0316
|
0.10.22.0317
|
||||||
Reference in New Issue
Block a user