Skip to content

Commit b59b0ca

Browse files
authored
1 parent 3ccebf7 commit b59b0ca

File tree

19 files changed

+341
-145
lines changed

19 files changed

+341
-145
lines changed

models/repo/repo_list.go

+20-10
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,16 @@ func searchRepositoryByCondition(ctx context.Context, opts *SearchRepoOptions, c
593593
return sess, count, nil
594594
}
595595

596+
// SearchRepositoryIDsByCondition search repository IDs by given condition.
597+
func SearchRepositoryIDsByCondition(ctx context.Context, cond builder.Cond) ([]int64, error) {
598+
repoIDs := make([]int64, 0, 10)
599+
return repoIDs, db.GetEngine(ctx).
600+
Table("repository").
601+
Cols("id").
602+
Where(cond).
603+
Find(&repoIDs)
604+
}
605+
596606
// AccessibleRepositoryCondition takes a user a returns a condition for checking if a repository is accessible
597607
func AccessibleRepositoryCondition(user *user_model.User, unitType unit.Type) builder.Cond {
598608
cond := builder.NewCond()
@@ -680,16 +690,16 @@ func AccessibleRepoIDsQuery(user *user_model.User) *builder.Builder {
680690
}
681691

682692
// FindUserCodeAccessibleRepoIDs finds all at Code level accessible repositories' ID by the user's id
683-
func FindUserCodeAccessibleRepoIDs(user *user_model.User) ([]int64, error) {
684-
repoIDs := make([]int64, 0, 10)
685-
if err := db.GetEngine(db.DefaultContext).
686-
Table("repository").
687-
Cols("id").
688-
Where(AccessibleRepositoryCondition(user, unit.TypeCode)).
689-
Find(&repoIDs); err != nil {
690-
return nil, fmt.Errorf("FindUserCodeAccesibleRepoIDs: %v", err)
691-
}
692-
return repoIDs, nil
693+
func FindUserCodeAccessibleRepoIDs(ctx context.Context, user *user_model.User) ([]int64, error) {
694+
return SearchRepositoryIDsByCondition(ctx, AccessibleRepositoryCondition(user, unit.TypeCode))
695+
}
696+
697+
// FindUserCodeAccessibleOwnerRepoIDs finds all repository IDs for the given owner whose code the user can see.
698+
func FindUserCodeAccessibleOwnerRepoIDs(ctx context.Context, ownerID int64, user *user_model.User) ([]int64, error) {
699+
return SearchRepositoryIDsByCondition(ctx, builder.NewCond().And(
700+
builder.Eq{"owner_id": ownerID},
701+
AccessibleRepositoryCondition(user, unit.TypeCode),
702+
))
693703
}
694704

695705
// GetUserRepositories returns a list of repositories of given user.

modules/context/org.go

+1
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ func HandleOrgAssignment(ctx *Context, args ...bool) {
130130
ctx.Data["IsOrganizationOwner"] = ctx.Org.IsOwner
131131
ctx.Data["IsOrganizationMember"] = ctx.Org.IsMember
132132
ctx.Data["IsPackageEnabled"] = setting.Packages.Enabled
133+
ctx.Data["IsRepoIndexerEnabled"] = setting.Indexer.RepoIndexerEnabled
133134
ctx.Data["IsPublicMember"] = func(uid int64) bool {
134135
is, _ := organization.IsPublicMembership(ctx.Org.Organization.ID, uid)
135136
return is

options/locale/locale_en-US.ini

+8
Original file line numberDiff line numberDiff line change
@@ -268,8 +268,11 @@ users = Users
268268
organizations = Organizations
269269
search = Search
270270
code = Code
271+
search.type.tooltip = Search type
271272
search.fuzzy = Fuzzy
273+
search.fuzzy.tooltip = Include results that also matches the search term closely
272274
search.match = Match
275+
search.match.tooltip = Include only results that matches the exact search term
273276
code_search_unavailable = Currently code search is not available. Please contact your site administrator.
274277
repo_no_results = No matching repositories found.
275278
user_no_results = No matching users found.
@@ -507,6 +510,7 @@ activity = Public Activity
507510
followers = Followers
508511
starred = Starred Repositories
509512
watched = Watched Repositories
513+
code = Code
510514
projects = Projects
511515
following = Following
512516
follow = Follow
@@ -1763,8 +1767,11 @@ activity.git_stats_deletion_n = %d deletions
17631767

17641768
search = Search
17651769
search.search_repo = Search repository
1770+
search.type.tooltip = Search type
17661771
search.fuzzy = Fuzzy
1772+
search.fuzzy.tooltip = Include results that also matches the search term closely
17671773
search.match = Match
1774+
search.match.tooltip = Include only results that matches the exact search term
17681775
search.results = Search results for "%s" in <a href="%s">%s</a>
17691776
search.code_no_results = No source code matching your search term found.
17701777
search.code_search_unavailable = Currently code search is not available. Please contact your site administrator.
@@ -2310,6 +2317,7 @@ create_org = Create Organization
23102317
repo_updated = Updated
23112318
people = People
23122319
teams = Teams
2320+
code = Code
23132321
lower_members = members
23142322
lower_repositories = repositories
23152323
create_new_team = New Team

routers/web/explore/code.go

+65-60
Original file line numberDiff line numberDiff line change
@@ -34,86 +34,91 @@ func Code(ctx *context.Context) {
3434

3535
language := ctx.FormTrim("l")
3636
keyword := ctx.FormTrim("q")
37+
38+
queryType := ctx.FormTrim("t")
39+
isMatch := queryType == "match"
40+
41+
ctx.Data["Keyword"] = keyword
42+
ctx.Data["Language"] = language
43+
ctx.Data["queryType"] = queryType
44+
ctx.Data["PageIsViewCode"] = true
45+
46+
if keyword == "" {
47+
ctx.HTML(http.StatusOK, tplExploreCode)
48+
return
49+
}
50+
3751
page := ctx.FormInt("page")
3852
if page <= 0 {
3953
page = 1
4054
}
4155

42-
queryType := ctx.FormTrim("t")
43-
isMatch := queryType == "match"
56+
var (
57+
repoIDs []int64
58+
err error
59+
isAdmin bool
60+
)
61+
if ctx.Doer != nil {
62+
isAdmin = ctx.Doer.IsAdmin
63+
}
4464

45-
if keyword != "" {
46-
var (
47-
repoIDs []int64
48-
err error
49-
isAdmin bool
50-
)
51-
if ctx.Doer != nil {
52-
isAdmin = ctx.Doer.IsAdmin
65+
// guest user or non-admin user
66+
if ctx.Doer == nil || !isAdmin {
67+
repoIDs, err = repo_model.FindUserCodeAccessibleRepoIDs(ctx, ctx.Doer)
68+
if err != nil {
69+
ctx.ServerError("FindUserCodeAccessibleRepoIDs", err)
70+
return
5371
}
72+
}
5473

55-
// guest user or non-admin user
56-
if ctx.Doer == nil || !isAdmin {
57-
repoIDs, err = repo_model.FindUserCodeAccessibleRepoIDs(ctx.Doer)
58-
if err != nil {
74+
var (
75+
total int
76+
searchResults []*code_indexer.Result
77+
searchResultLanguages []*code_indexer.SearchResultLanguages
78+
)
79+
80+
if (len(repoIDs) > 0) || isAdmin {
81+
total, searchResults, searchResultLanguages, err = code_indexer.PerformSearch(ctx, repoIDs, language, keyword, page, setting.UI.RepoSearchPagingNum, isMatch)
82+
if err != nil {
83+
if code_indexer.IsAvailable() {
5984
ctx.ServerError("SearchResults", err)
6085
return
6186
}
87+
ctx.Data["CodeIndexerUnavailable"] = true
88+
} else {
89+
ctx.Data["CodeIndexerUnavailable"] = !code_indexer.IsAvailable()
6290
}
6391

64-
var (
65-
total int
66-
searchResults []*code_indexer.Result
67-
searchResultLanguages []*code_indexer.SearchResultLanguages
68-
)
69-
70-
if (len(repoIDs) > 0) || isAdmin {
71-
total, searchResults, searchResultLanguages, err = code_indexer.PerformSearch(ctx, repoIDs, language, keyword, page, setting.UI.RepoSearchPagingNum, isMatch)
72-
if err != nil {
73-
if code_indexer.IsAvailable() {
74-
ctx.ServerError("SearchResults", err)
75-
return
92+
loadRepoIDs := make([]int64, 0, len(searchResults))
93+
for _, result := range searchResults {
94+
var find bool
95+
for _, id := range loadRepoIDs {
96+
if id == result.RepoID {
97+
find = true
98+
break
7699
}
77-
ctx.Data["CodeIndexerUnavailable"] = true
78-
} else {
79-
ctx.Data["CodeIndexerUnavailable"] = !code_indexer.IsAvailable()
80100
}
81-
82-
loadRepoIDs := make([]int64, 0, len(searchResults))
83-
for _, result := range searchResults {
84-
var find bool
85-
for _, id := range loadRepoIDs {
86-
if id == result.RepoID {
87-
find = true
88-
break
89-
}
90-
}
91-
if !find {
92-
loadRepoIDs = append(loadRepoIDs, result.RepoID)
93-
}
94-
}
95-
96-
repoMaps, err := repo_model.GetRepositoriesMapByIDs(loadRepoIDs)
97-
if err != nil {
98-
ctx.ServerError("SearchResults", err)
99-
return
101+
if !find {
102+
loadRepoIDs = append(loadRepoIDs, result.RepoID)
100103
}
104+
}
101105

102-
ctx.Data["RepoMaps"] = repoMaps
106+
repoMaps, err := repo_model.GetRepositoriesMapByIDs(loadRepoIDs)
107+
if err != nil {
108+
ctx.ServerError("GetRepositoriesMapByIDs", err)
109+
return
103110
}
104111

105-
ctx.Data["Keyword"] = keyword
106-
ctx.Data["Language"] = language
107-
ctx.Data["queryType"] = queryType
108-
ctx.Data["SearchResults"] = searchResults
109-
ctx.Data["SearchResultLanguages"] = searchResultLanguages
110-
ctx.Data["PageIsViewCode"] = true
111-
112-
pager := context.NewPagination(total, setting.UI.RepoSearchPagingNum, page, 5)
113-
pager.SetDefaultParams(ctx)
114-
pager.AddParam(ctx, "l", "Language")
115-
ctx.Data["Page"] = pager
112+
ctx.Data["RepoMaps"] = repoMaps
116113
}
117114

115+
ctx.Data["SearchResults"] = searchResults
116+
ctx.Data["SearchResultLanguages"] = searchResultLanguages
117+
118+
pager := context.NewPagination(total, setting.UI.RepoSearchPagingNum, page, 5)
119+
pager.SetDefaultParams(ctx)
120+
pager.AddParam(ctx, "l", "Language")
121+
ctx.Data["Page"] = pager
122+
118123
ctx.HTML(http.StatusOK, tplExploreCode)
119124
}

routers/web/repo/search.go

+16-6
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,27 @@ func Search(ctx *context.Context) {
2121
ctx.Redirect(ctx.Repo.RepoLink)
2222
return
2323
}
24+
2425
language := ctx.FormTrim("l")
2526
keyword := ctx.FormTrim("q")
27+
28+
queryType := ctx.FormTrim("t")
29+
isMatch := queryType == "match"
30+
31+
ctx.Data["Keyword"] = keyword
32+
ctx.Data["Language"] = language
33+
ctx.Data["queryType"] = queryType
34+
ctx.Data["PageIsViewCode"] = true
35+
36+
if keyword == "" {
37+
ctx.HTML(http.StatusOK, tplSearch)
38+
return
39+
}
40+
2641
page := ctx.FormInt("page")
2742
if page <= 0 {
2843
page = 1
2944
}
30-
queryType := ctx.FormTrim("t")
31-
isMatch := queryType == "match"
3245

3346
total, searchResults, searchResultLanguages, err := code_indexer.PerformSearch(ctx, []int64{ctx.Repo.Repository.ID},
3447
language, keyword, page, setting.UI.RepoSearchPagingNum, isMatch)
@@ -41,13 +54,10 @@ func Search(ctx *context.Context) {
4154
} else {
4255
ctx.Data["CodeIndexerUnavailable"] = !code_indexer.IsAvailable()
4356
}
44-
ctx.Data["Keyword"] = keyword
45-
ctx.Data["Language"] = language
46-
ctx.Data["queryType"] = queryType
57+
4758
ctx.Data["SourcePath"] = ctx.Repo.Repository.HTMLURL()
4859
ctx.Data["SearchResults"] = searchResults
4960
ctx.Data["SearchResultLanguages"] = searchResultLanguages
50-
ctx.Data["PageIsViewCode"] = true
5161

5262
pager := context.NewPagination(total, setting.UI.RepoSearchPagingNum, page, 5)
5363
pager.SetDefaultParams(ctx)

0 commit comments

Comments
 (0)