|
| 1 | +package code |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "net/http" |
| 6 | + "net/url" |
| 7 | + "path" |
| 8 | + "slices" |
| 9 | + |
| 10 | + access_model "code.gitea.io/gitea/models/perm/access" |
| 11 | + repo_model "code.gitea.io/gitea/models/repo" |
| 12 | + "code.gitea.io/gitea/modules/container" |
| 13 | + "code.gitea.io/gitea/modules/indexer" |
| 14 | + "code.gitea.io/gitea/modules/indexer/code" |
| 15 | + "code.gitea.io/gitea/modules/log" |
| 16 | + "code.gitea.io/gitea/modules/setting" |
| 17 | + "code.gitea.io/gitea/modules/structs" |
| 18 | + "code.gitea.io/gitea/modules/util" |
| 19 | + "code.gitea.io/gitea/routers/api/v1/utils" |
| 20 | + "code.gitea.io/gitea/services/context" |
| 21 | + "code.gitea.io/gitea/services/convert" |
| 22 | +) |
| 23 | + |
| 24 | +// GlobalSearch search codes in all accessible repositories with the given keyword. |
| 25 | +func GlobalSearch(ctx *context.APIContext) { |
| 26 | + // swagger:operation GET /search/code search GlobalSearch |
| 27 | + // --- |
| 28 | + // summary: Search for repositories |
| 29 | + // produces: |
| 30 | + // - application/json |
| 31 | + // parameters: |
| 32 | + // - name: q |
| 33 | + // in: query |
| 34 | + // description: keyword |
| 35 | + // type: string |
| 36 | + // - name: repo |
| 37 | + // in: query |
| 38 | + // description: multiple repository names to search in |
| 39 | + // type: string |
| 40 | + // collectionFormat: multi |
| 41 | + // - name: mode |
| 42 | + // in: query |
| 43 | + // description: include search of keyword within repository description |
| 44 | + // type: string |
| 45 | + // enum: [exact, words, fuzzy, regexp] |
| 46 | + // - name: language |
| 47 | + // in: query |
| 48 | + // description: filter by programming language |
| 49 | + // type: integer |
| 50 | + // format: int64 |
| 51 | + // - name: page |
| 52 | + // in: query |
| 53 | + // description: page number of results to return (1-based) |
| 54 | + // type: integer |
| 55 | + // - name: limit |
| 56 | + // in: query |
| 57 | + // description: page size of results |
| 58 | + // type: integer |
| 59 | + // responses: |
| 60 | + // "200": |
| 61 | + // "$ref": "#/responses/CodeSearchResults" |
| 62 | + // "422": |
| 63 | + // "$ref": "#/responses/validationError" |
| 64 | + |
| 65 | + if !setting.Indexer.RepoIndexerEnabled { |
| 66 | + ctx.APIError(http.StatusBadRequest, "Repository indexing is disabled") |
| 67 | + return |
| 68 | + } |
| 69 | + |
| 70 | + q := ctx.FormTrim("q") |
| 71 | + if q == "" { |
| 72 | + ctx.APIError(http.StatusUnprocessableEntity, "Query cannot be empty") |
| 73 | + return |
| 74 | + } |
| 75 | + |
| 76 | + var ( |
| 77 | + accessibleRepoIDs []int64 |
| 78 | + err error |
| 79 | + isAdmin bool |
| 80 | + ) |
| 81 | + if ctx.Doer != nil { |
| 82 | + isAdmin = ctx.Doer.IsAdmin |
| 83 | + } |
| 84 | + |
| 85 | + // guest user or non-admin user |
| 86 | + if ctx.Doer == nil || !isAdmin { |
| 87 | + accessibleRepoIDs, err = repo_model.FindUserCodeAccessibleRepoIDs(ctx, ctx.Doer) |
| 88 | + if err != nil { |
| 89 | + ctx.APIErrorInternal(err) |
| 90 | + return |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + repoNames := ctx.FormStrings("repo") |
| 95 | + searchRepoIDs := make([]int64, 0, len(repoNames)) |
| 96 | + if len(repoNames) > 0 { |
| 97 | + var err error |
| 98 | + searchRepoIDs, err = repo_model.GetRepositoriesIDsByFullNames(ctx, repoNames) |
| 99 | + if err != nil { |
| 100 | + ctx.APIErrorInternal(err) |
| 101 | + return |
| 102 | + } |
| 103 | + } |
| 104 | + if len(searchRepoIDs) > 0 { |
| 105 | + for i := 0; i < len(searchRepoIDs); i++ { |
| 106 | + if !slices.Contains(accessibleRepoIDs, searchRepoIDs[i]) { |
| 107 | + searchRepoIDs = append(searchRepoIDs[:i], searchRepoIDs[i+1:]...) |
| 108 | + i-- |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + if len(searchRepoIDs) > 0 { |
| 113 | + accessibleRepoIDs = searchRepoIDs |
| 114 | + } |
| 115 | + |
| 116 | + searchMode := indexer.SearchModeType(ctx.FormString("mode")) |
| 117 | + listOpts := utils.GetListOptions(ctx) |
| 118 | + |
| 119 | + total, results, languages, err := code.PerformSearch(ctx, &code.SearchOptions{ |
| 120 | + Keyword: q, |
| 121 | + RepoIDs: accessibleRepoIDs, |
| 122 | + Language: ctx.FormString("language"), |
| 123 | + SearchMode: searchMode, |
| 124 | + Paginator: &listOpts, |
| 125 | + NoHighlight: true, // Default to no highlighting for performance, we don't need to highlight in the API search results |
| 126 | + }) |
| 127 | + if err != nil { |
| 128 | + ctx.APIErrorInternal(err) |
| 129 | + return |
| 130 | + } |
| 131 | + |
| 132 | + ctx.SetTotalCountHeader(int64(total)) |
| 133 | + searchResults := structs.CodeSearchResults{ |
| 134 | + TotalCount: int64(total), |
| 135 | + } |
| 136 | + |
| 137 | + for _, lang := range languages { |
| 138 | + searchResults.Languages = append(searchResults.Languages, structs.CodeSearchResultLanguage{ |
| 139 | + Language: lang.Language, |
| 140 | + Color: lang.Color, |
| 141 | + Count: lang.Count, |
| 142 | + }) |
| 143 | + } |
| 144 | + |
| 145 | + repoIDs := make(container.Set[int64], len(results)) |
| 146 | + for _, result := range results { |
| 147 | + repoIDs.Add(result.RepoID) |
| 148 | + } |
| 149 | + |
| 150 | + repos, err := repo_model.GetRepositoriesMapByIDs(ctx, repoIDs.Values()) |
| 151 | + if err != nil { |
| 152 | + ctx.APIErrorInternal(err) |
| 153 | + return |
| 154 | + } |
| 155 | + |
| 156 | + permissions := make(map[int64]access_model.Permission) |
| 157 | + for _, repo := range repos { |
| 158 | + permission, err := access_model.GetUserRepoPermission(ctx, repo, ctx.Doer) |
| 159 | + if err != nil { |
| 160 | + ctx.APIErrorInternal(err) |
| 161 | + return |
| 162 | + } |
| 163 | + permissions[repo.ID] = permission |
| 164 | + } |
| 165 | + |
| 166 | + for _, result := range results { |
| 167 | + repo, ok := repos[result.RepoID] |
| 168 | + if !ok { |
| 169 | + log.Error("Repository with ID %d not found for search result: %v", result.RepoID, result) |
| 170 | + continue |
| 171 | + } |
| 172 | + |
| 173 | + apiURL := fmt.Sprintf("%s/contents/%s?ref=%s", repo.APIURL(), util.PathEscapeSegments(result.Filename), url.PathEscape(result.CommitID)) |
| 174 | + htmlURL := fmt.Sprintf("%s/blob/%s/%s", repo.HTMLURL(), url.PathEscape(result.CommitID), util.PathEscapeSegments(result.Filename)) |
| 175 | + ret := structs.CodeSearchResult{ |
| 176 | + Name: path.Base(result.Filename), |
| 177 | + Path: result.Filename, |
| 178 | + Sha: result.CommitID, |
| 179 | + URL: apiURL, |
| 180 | + HTMLURL: htmlURL, |
| 181 | + Language: result.Language, |
| 182 | + Repository: convert.ToRepo(ctx, repo, permissions[repo.ID]), |
| 183 | + } |
| 184 | + for _, line := range result.Lines { |
| 185 | + ret.Lines = append(ret.Lines, structs.CodeSearchResultLine{ |
| 186 | + LineNumber: line.Num, |
| 187 | + RawContent: line.RawContent, |
| 188 | + }) |
| 189 | + } |
| 190 | + searchResults.Items = append(searchResults.Items, ret) |
| 191 | + } |
| 192 | + |
| 193 | + ctx.JSON(200, searchResults) |
| 194 | +} |
0 commit comments